Attempting SaveAs function with Merged Word docx file - Help

CS Wright

New Member
Joined
Feb 12, 2021
Messages
2
Office Version
  1. 2010
Platform
  1. Windows
Need help please; somewhat new to VBA. Absolutely struggling with this issue.

Issue - Data stored in an excel file; press button; data is transferred to a Word (Merge) doc file. This works fine. Now I struggle in attempting to automate the SaveAs option to save the document. In the code below the string Path when a MSGBOX is inserted is perfect. However, reading and reading I can not get the save to work.

Public Sub openDPIRoleChange()
Dim wd As Object
Dim wdocSource As Object
Dim Answer As String
Dim strWorkbookName As String
Dim Path As String

On Error Resume Next
Set wd = GetObject(, "Word.Application")
If wd Is Nothing Then
Set wd = CreateObject("Word.Application")
End If
On Error GoTo 0

Answer = MsgBox("Is request for Z-Fuel", vbQuestion + vbYesNo, "???")

'Set path to save document
Path = "J:\FPS\FIN\TRAN\BSIP\00 CSWright\DPI Role Change Forms\DPI Role Change Form - " & Range("C4")
If Answer = vbNo Then
Path = Path & ".docx"
Else
Path = Path & " - ZFuel.docx"
End If

If Answer = vbNo Then
Set wdocSource = wd.Documents.Open("J:\FPS\FIN\TRAN\BSIP\00 CSWright\DPI Role Change Forms\DPI Role Change Form - Merge.doc")
Else
Set wdocSource = wd.Documents.Open("J:\FPS\FIN\TRAN\BSIP\00 CSWright\DPI Role Change Forms\DPI Role Change Form - Merge - ZFuel.doc")
End If

strWorkbookName = ThisWorkbook.Path & "\" & ThisWorkbook.Name

wdocSource.MailMerge.MainDocumentType = wdFormLetters

wdocSource.MailMerge.OpenDataSource _
Name:=strWorkbookName, _
AddToRecentFiles:=False, _
Revert:=False, _
Format:=wdOpenFormatAuto, _
Connection:="Data Source=" & strWorkbookName & ";Mode=Read", _
SQLStatement:="SELECT * FROM `DPI Role Change$`"

With wdocSource.MailMerge
.Destination = wdSendToNewDocument
.SuppressBlankLines = True
With .DataSource
.FirstRecord = wdDefaultFirstRecord
.LastRecord = wdDefaultLastRecord
End With
.Execute Pause:=False
End With

wd.Visible = True
wdocSource.Close SaveChanges:=False


'Save file
' ActiveDocument.SaveAs2 Filename:=Path, _
' FileFormat:=wdFormatXMLDocument, LockComments:=False, Password:="", _
' AddToRecentFiles:=True, WritePassword:="", ReadOnlyRecommended:=False, _
' EmbedTrueTypeFonts:=False, SaveNativePictureFormat:=False, SaveFormsData _
' :=False, SaveAsAOCELetter:=False, CompatibilityMode:=15

Set wdocSource = Nothing
Set wd = Nothing
End Sub
 

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.

Watch MrExcel Video

Forum statistics

Threads
1,129,374
Messages
5,635,888
Members
416,886
Latest member
coreyalaurence37

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Top