Hi,
I created a form with a ListBox and a Command Button. The users selects the values in the listbox and then click the button to create word documents. I've written VBA code to accomplish this. But it's not working properly. It opens multiple word documents but all for the same one. I'm new to VBA. Any help will be greatly appreciated!
Private Sub Command6_Click()
Dim appWord As Object
Dim varItem As Variant
Dim strPathToTemplateFile As String
Dim strPathToProspectiveFile As String
Dim strPreferredFileName As String
For Each varItem In Me.List0.ItemsSelected
strPathToTemplateFile = "C:\Temp\test.docx"
Set appWord = CreateObject("Word.Application")
appWord.Documents.Add strPathToTemplateFile 'opens the template as Document1
appWord.Visible = True
'Replace bookmarks with record data
With appWord.ActiveDocument.Bookmarks
![SchoolName].Range.Text = Me.Survey_Results_Q1.Form!SchoolName
![Date].Range.Text = Me.Survey_Results_Q1.Form!Date
![Q1a1Per].Range.Text = Format(Me.Survey_Results_Q1.Form!Q1a1Per, "#0.00%")
![Q1a2Per].Range.Text = Format(Me.Survey_Results_Q1.Form!Q1a2Per, "#0.00%")
![Q1a3Per].Range.Text = Format(Me.Survey_Results_Q1.Form!Q1a3Per, "#0.00%")
![Q1a4Per].Range.Text = Format(Me.Survey_Results_Q1.Form!Q1a4Per, "#0.00%")
![Q1b1Per].Range.Text = Format(Me.Survey_Results_Q1.Form!Q1b1Per, "#0.00%")
![Q1b2Per].Range.Text = Format(Me.Survey_Results_Q1.Form!Q1b2Per, "#0.00%")
![Q1b3Per].Range.Text = Format(Me.Survey_Results_Q1.Form!Q1b3Per, "#0.00%")
![Q1b4Per].Range.Text = Format(Me.Survey_Results_Q1.Form!Q1b4Per, "#0.00%")
![Q1c1Per].Range.Text = Format(Me.Survey_Results_Q1.Form!Q1c1Per, "#0.00%")
![Q1c2Per].Range.Text = Format(Me.Survey_Results_Q1.Form!Q1c2Per, "#0.00%")
![Q1c3Per].Range.Text = Format(Me.Survey_Results_Q1.Form!Q1c3Per, "#0.00%")
![Q1c4Per].Range.Text = Format(Me.Survey_Results_Q1.Form!Q1c4Per, "#0.00%")
End With
Next varItem
I created a form with a ListBox and a Command Button. The users selects the values in the listbox and then click the button to create word documents. I've written VBA code to accomplish this. But it's not working properly. It opens multiple word documents but all for the same one. I'm new to VBA. Any help will be greatly appreciated!
Private Sub Command6_Click()
Dim appWord As Object
Dim varItem As Variant
Dim strPathToTemplateFile As String
Dim strPathToProspectiveFile As String
Dim strPreferredFileName As String
For Each varItem In Me.List0.ItemsSelected
strPathToTemplateFile = "C:\Temp\test.docx"
Set appWord = CreateObject("Word.Application")
appWord.Documents.Add strPathToTemplateFile 'opens the template as Document1
appWord.Visible = True
'Replace bookmarks with record data
With appWord.ActiveDocument.Bookmarks
![SchoolName].Range.Text = Me.Survey_Results_Q1.Form!SchoolName
![Date].Range.Text = Me.Survey_Results_Q1.Form!Date
![Q1a1Per].Range.Text = Format(Me.Survey_Results_Q1.Form!Q1a1Per, "#0.00%")
![Q1a2Per].Range.Text = Format(Me.Survey_Results_Q1.Form!Q1a2Per, "#0.00%")
![Q1a3Per].Range.Text = Format(Me.Survey_Results_Q1.Form!Q1a3Per, "#0.00%")
![Q1a4Per].Range.Text = Format(Me.Survey_Results_Q1.Form!Q1a4Per, "#0.00%")
![Q1b1Per].Range.Text = Format(Me.Survey_Results_Q1.Form!Q1b1Per, "#0.00%")
![Q1b2Per].Range.Text = Format(Me.Survey_Results_Q1.Form!Q1b2Per, "#0.00%")
![Q1b3Per].Range.Text = Format(Me.Survey_Results_Q1.Form!Q1b3Per, "#0.00%")
![Q1b4Per].Range.Text = Format(Me.Survey_Results_Q1.Form!Q1b4Per, "#0.00%")
![Q1c1Per].Range.Text = Format(Me.Survey_Results_Q1.Form!Q1c1Per, "#0.00%")
![Q1c2Per].Range.Text = Format(Me.Survey_Results_Q1.Form!Q1c2Per, "#0.00%")
![Q1c3Per].Range.Text = Format(Me.Survey_Results_Q1.Form!Q1c3Per, "#0.00%")
![Q1c4Per].Range.Text = Format(Me.Survey_Results_Q1.Form!Q1c4Per, "#0.00%")
End With
Next varItem