Hello

Thanks in advance for your kind support

I have been working with WORD via Excel VBA (mailmerge), I have been trying to open a word file programmatically in excel vba and adding/editing contents in it using bookmarks. However, I find that on alternate runs, I get the 'Error 462: The remote server does not exist" error. I researched a lot and understood this has something to do 'Unqualified references;.

However I don't understand how to correct the below snippet of code to qualified references? Can someone please help?

Also is there a positbility that I can protect the generated letter with password from macro, if yes can you guide me on it.


Option Explicit
Sub Generate_Letter()
Application.DisplayAlerts = False
Dim wd As Object
Dim wdocSource As Object
Dim strWorkbookName As String
Dim wdmain, wdtable As String

Dim clausea, clauseb, clausec As String
ActiveWorkbook.save

If Range("PSD_PL").Value = "" Or Range("Cont_TY").Value = "" _
Or Range("S_O_B").Value = "" Or Range("BO_PL").Value = "" Or Range("RL_PL").Value = "" _
Or Range("OF_PL").Value = "" Or Range("NCC_PL").Value = "" Then

MsgBox "Please fill in all the fileds", vbCritical
Exit Sub
End If

On Error Resume Next
Set wd = GetObject(, "Word.Application")
'If wd Is Nothing Then
If wd Is Nothing Then 'Word isn't already running
Set wd = CreateObject("Word.Application")
'Set wd = New Word.Application
End If
On Error GoTo 0
'On Error GoTo aa

wdmain = Range("T_Path").Value 'Actual Template Path
wdtable = "PLC"

Set wdocSource = wd.Documents.Open(wdmain)
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 [" & wdtable & "]"

'conditions if any--------------------


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
Set wdocSource = Nothing
Set wd = Nothing


Application.DisplayAlerts = True

MsgBox "Please Save the Word File", vbInformation + vbOKOnly
End Sub