Excel userform (uf4_works) with 24 toggle buttons (tglb_xxx_xxxx where xxx_xxxx is variable based on selection) allowing user to select any number of reports for creation (via Word mail merge) or viewing (previously created)
Here is code from one of the 24 button presses.
Private Sub tglb_cue_diar_Click()
If mbEvents Then Exit Sub
With Me
Set oBP = .tglb_cue_diar
sRPT = "CUE-DR.docx"
button_check ' oBP, sRPT
End With
End Sub
Here is the 'button_check' routine
Sub button_check() '(ByVal oBP As Object, sRPT As String)
'Stop
'oBP = userform toggle button name (from toggle button click event)
'sRPT = report (.docx) filename (from toggle button click event)
Debug.Print oBP.Name
If bpb = 0 Then 'bypass if bypass flag = 1
'With uf4_works 'the userform
With oBP
'this allows a previouly created report to be viewed.
If .BackColor = RGB(0, 153, 211) And .Value = True Then
If DocOpen(sRPT) = False Then
Set WordApp = CreateObject("word.Application")
WordApp.Documents.Open path_name & sRPT
WordApp.Visible = True
Set WordApp = Nothing
.Value = True
End If
'.BackColor = RGB(102, 0, 204) 'purple
Exit Sub
ElseIf .BackColor = RGB(0, 153, 211) And .Value = False Then
Application.DisplayAlerts = False
With GetObject(path_name & sRPT)
.Save
'objWord.Quit '
'Set objWord = Nothing '
.Application.Quit
End With
Application.DisplayAlerts = True
.BackColor = RGB(0, 153, 211)
.Value = False
' Exit Sub
ElseIf .BackColor = RGB(229, 38, 38) And .Value = True Then
mbEvents = True
.BackColor = RGB(0, 153, 211)
.Value = False
mbEvents = False
Exit Sub
Else
If .Value = True Then
uf4_works.tb_of_rpt.Value = uf4_works.tb_of_rpt.Value + 1
Else
uf4_works.tb_of_rpt.Value = uf4_works.tb_of_rpt.Value - 1
End If
End If
End With
'End With
End If
End Sub
Once the user has selected the report(s) they wish to create they can proceed with the creation of the mail merge document creation process by pressing a button on the userform ([PROCEED]) toi launch the code which processes the selections and prepares them for the merge process. Here is that code (greatly editted to exclude the repetitiveness of 24 reports, I have shown only one of those 24)
Private Sub cb_preview_Click()
Dim path As String, dirname As String, dest1 As String
Dim riq As Double
Dim objWord As Object
Dim objDoc As Object
Dim oFS As Object
Dim dfn As String, rtq As String
Dim ui1 As VbMsgBoxResult
Dim rptbtn As Object
Dim cas As Long
Dim lrow As Long, screw As String
Set objWord = CreateObject("Word.Application")
Set oFS = CreateObject("Scripting.FileSystemObject")
cas = 0
If FormIsLoaded("uf1_create_wo1") Then Unload uf1_create_wo1
'Unload uf1_create_wo1
Application.ScreenUpdating = False
If Me.tb_of_rpt = 0 Then
MsgBox "No reports have been selected.", vbExclamation, "ERROR"
Exit Sub
End If
'populate print que
With ws_th
.Cells.Clear
'Reg Diamonds
If Me.tglb_cue_diar.Value = True And Me.tglb_cue_diar.BackColor = RGB(176, 196, 222) Then 'button state is true, color represents file creation is selected for creation
last_cell
.Range("A" & lj) = "CUE-DR"
Else
'23 other different toggle buttons
End If
End With
'create directory
path = "u:\---\-----\----------------\------\--------\WORKORDERS\"
dirname = format(ws_vh.Range("B17"), "ddd dd-mmm-yy")
dest1 = path & dirname
On Error Resume Next
MkDir dest1
On Error GoTo 0
'how many reports in que
riq = WorksheetFunction.CountA(ws_th.Range("A2:A49"))
'prepare for report creation (mail merge)
For i = 2 To riq + 1
Me.tb_cur_rpt = Me.tb_cur_rpt + 1
rpt_od = ws_th.Range("A" & i)
If rpt_od = "CUE-DR" Then
Me.tglb_cue_diar.BackColor = RGB(229, 38, 38) 'button state is true, color represents file creation is in progress
Set oBP = Me.tglb_cue_diar
sRPT = rpt_od
Else
'23 other different toggle buttons
End If
dfn = rpt_od & ".docx"
rtq = dest1 & "\" & dfn
If Dir(rtq) <> "" Then 'option to view or print previously created selected report
ui1 = MsgBox("This report has been previously created on " & Chr(13) & format(oFS.GetFile(rtq).DateCreated, "ddd dd-mm-yy hh:mm:ss") & "." & _
Chr(13) & "Press [YES] to recreate the report, or [NO] to view it.", vbQuestion + vbYesNoCancel, dfn & " EXISTS")
If ui1 = vbNo Then
Set objDoc = objWord.Documents.Open(rtq)
objWord.Visible = True
ElseIf ui1 = vbCancel Then
MsgBox "Cancel me."
Else
dest = 1 'send to new document
merge2 i, rpt_od, objWord, dest ', pr, pn, path_name '[module 39]
cas = cas + 1
End If
Else 'create new report
dest = 1 'send to new document
merge2 i, rpt_od, objWord, dest ', pr, pn, path_name '[module 39]
cas = cas + 1
End If
'not sure what this does. Perhaps open the document after creation? Might be the root of my problem?
On Error Resume Next
fn99 = work_fn
Set wBook = Workbooks(fn99)
If wBook Is Nothing Then
'open workbook after having been closed during report making
Workbooks.Open ("u:\---\-----\----------------\------\--------\DATA\" & fn99)
'ActiveWorkbook.Windows(1).Visible = False
Set wBook = Nothing
On Error GoTo 0
End If
On Error GoTo 0
'Following creation, reset selected button to FALSE state and change colour format to reflect the condition that report has been created, saved and accessible by clicking same button in the future
If rpt_od = "CUE-DR" Then
With Me.tglb_cue_diar
bpb = 1
'.BackColor = RGB(0, 153, 211)
.ForeColor = RGB(0, 52, 98)
'.Value = False
End With
Else
'23 other different toggle buttons
End If
ws_th.Range("A" & i) = ""
Next i
'bpb = 0 ' reset bypass button
If cas > 0 Then
fn = ws_vh.Range("B23").Value
Set wb_nwb = Workbooks(fn)
Set ws_dev = wb_nwb.Worksheets("DEV")
ws_dev.Range("B6").Value = Now
MsgBox cas & " report(s) created and saved.", vbInformation, "SUCCESS " & Now
End If
'counters
Me.tb_cur_rpt.Value = Me.tb_cur_rpt.Value - 1
Me.tb_of_rpt.Value = Me.tb_of_rpt.Value - 1
ws_th.Columns(1).Clear
'testing purposes, self teaching, ineffewctive at solving posted prioblem
'objWord.Quit
Set objWord = Nothing
Application.ScreenUpdating = True
End Sub
... to reduce the length of the post, it's continued in the next reply.
Bookmarks