Option Explicit
'the document
Dim Inv_doc As Object
'the application
Dim WD As Object
Dim FName As String
Dim DesktopB As String
Option Base 1
Sub AutoNameEdit()
Const wdReplaceAll = 2
Dim objSelection
Dim WDarray As Variant
Dim WDcnt As Long, myCnt As Long, i As Long
Dim cmdPrice_Click As Long
Const wdExportFormatPDF = 2
i = 1
WDarray = Array("txtCompName", "txtCompNo", "txtCompRef", "txtRefTitle", _
"txtCompName", "txtStorage", "txtSpecial", "txtCompRef", "txtSafeRef", "txtStreet", "txtStreetNo", "txtPostNo", "txtCity")
FName = ActiveWorkbook.Sheets("Ark3").RAnge("B1").Value
DesktopB = CreateObject("WScript.Shell").SpecialFolders("Desktop") & Application.PathSeparator
Dim which_document As String
which_document = DesktopB & "\SalesTools\Intro.docx"
'need an instance of word
Set WD = CreateObject("Word.Application") 'Set objWord = CreateObject("Word.Application")
WD.Visible = True
Set Inv_doc = WD.Documents.Open(which_document) 'Set objDoc = objWord.Documents.Open("C:\Scripts\Test.doc")
Set objSelection = WD.Selection
For myCnt = 1 To UBound(WDarray)
objSelection.Find.Text = WDarray(myCnt)
objSelection.Find.Forward = True
objSelection.Find.MatchWholeWord = True
If objSelection.Find.Execute Then
objSelection.Find.Replacement.Text = Sheets("Ark3").Cells(i, 2).Text
objSelection.Find.Execute , , , , , , , , , , wdReplaceAll
End If
i = i + 1
Next
WD.Activate
Inv_doc.ExportAsFixedFormat DesktopB & "\SalesTools\" & FName & " - " & Date & ".PDF", wdExportFormatPDF
'Inv_doc.SaveAs DesktopB & "\SalesTools\" & FName & " - " & Date & ".docx"
Inv_doc.Close
WD.Quit
Set Inv_doc = Nothing
Set WD = Nothing
End Sub