Have the below Excel VBA Code:
Challenge: if the Word Template is already "sourced" to a different spreadsheet (say the spreadsheet from last month which has a different name), the user has to go through the steps to select the "newer" spreadsheet which I am calling the Word Template from.
Question: is there a way within the Excel VBA (or other) to open the word doc so that the source is changed to the current spreadsheet?
Set wd = GetObject(, "Word.Application")
If wd Is Nothing Then
Set wd = CreateObject("Word.Application")
End If
Dim Letter_File As FileDialog
Set Letter_File = Application.FileDialog(msoFileDialogFilePicker)
With Letter_File
.Filters.Clear
.AllowMultiSelect = False
.InitialFileName = "\\Nasprod-5\gbop\Customer Service\RMDs\Process Improvements\"
If .Show = -1 Then
Fileaddress = .SelectedItems(1)
Else
Exit Sub
End If
End With
If MsgBox(Fileaddress, vbOKCancel) = vbCancel Then
MsgBox ("Canceled")
Exit Sub
End If
Set wdocSource = wd.Documents.Open(Fileaddress)
The following is code I use in Word vba.
Dim strFileName As String
strFileName = WorkGroupPath & "Parts\Merge Data\Clients_Merge.xlsm"
'
' Attach Merge list
With ActiveDocument.MailMerge
.MainDocumentType = wdFormLetters
.OpenDataSource Name:=strFileName, _
SQLStatement:="SELECT * FROM `Clients$`" ', _
'
' Show merge data
.ViewMailMergeFieldCodes = False
End With
' Find client
Application.Dialogs(wdDialogMailMergeFindRecipient).Show
This is run upon opening/creating the primary merge document. You could assign the long name of your data source to strFilename.
You might have to change the With to:
With wd.ActiveDocument.MailMerge
The following came from recording a MailMerge in Word.
.OpenDataSource Name:=strFileName, _
SQLStatement:="SELECT * FROM `Clients$`" ', _
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.