Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VBA Access - Find and Replace text in Word Document

I have successfully written some VBA code in Excel which opens an existing Word document, finds and replaces a string based on information in the Excel worksheet.

As the source data exists comes from an Access Database, I thought I would try and move the VBA code into Access and run it from there.

The updated code works mostly but strangely, the part of the code which finds and replaces the text string doesn't work when I run it in access.

Sub CreateFormsPDF()

'   Creates Garda Vetting Forms NVB1 in Word and saves as PDF
    Dim WordApp As Object
    Dim WordDoc As Object
    Dim db As Database
    Dim rs As Recordset
    Dim Records As Integer
    Dim IDAnchor As String
    Dim ID As String
    Dim FilePath As String, SaveAsName As String

    FilePath = "N:\"

'   Start Word and create an object (late binding)
'   Document already exists so reference this
    Set WordApp = CreateObject("Word.Application")
    Set WordDoc = WordApp.Documents.Open(FilePath & "Form1.docx")

    WordApp.Application.Visible = True

'   Point to the relevant table in the Current Database
    Set db = CurrentDb
    Set rs = db.OpenRecordset("qryMailingList", dbOpenDynaset, dbSeeChanges)
    Records = rs.RecordCount

'   Cycle through all records in MailingList Query
    Do Until rs.EOF

'   Define IDAnchor
    IDAnchor = "$$ID$$"

'   Assign current data to variables
    ID = rs!StudentID

'   Determine the filename
    SaveAsName = FilePath & ID & ".pdf"

'   Send commands to Word
    With WordApp
        With WordDoc.Content.Find
            .Text = IDAnchor
            .Replacement.Text = ID
            .Wrap = wdFindContinue
            .Execute Replace:=wdReplaceAll
        End With
        .ActiveDocument.SaveAs2 FileName:=SaveAsName, FileFormat:=17
    End With

    IDAnchor = ID

            rs.MoveNext
    Loop

    WordApp.Quit savechanges:=wdDoNotSaveChanges
    Set WordApp = Nothing
    Set WordDoc = Nothing
    Set rs = Nothing
    Set db = Nothing

    MsgBox Records & " Forms Created"

End Sub

The code executes fine, with one exception which is the Find and Replace element in Word i.e.

'   Send commands to Word
    With WordApp
        With WordDoc.Content.Find
            .Text = IDAnchor
            .Replacement.Text = ID
            .Wrap = wdFindContinue
            .Execute Replace:=wdReplaceAll
        End With
        .ActiveDocument.SaveAs2 FileName:=SaveAsName, FileFormat:=17
    End With

What is even stranger is that I have a version of this code running via Excel and this runs without any problems at all and I've lifted this section of code from that subroutine exactly as is. So this works in Excel, but not in Access but I've no idea why.

Would really appreciate any help that might be available

Many thanks...

like image 303
Steve O'Dea Avatar asked Oct 18 '22 15:10

Steve O'Dea


1 Answers

Actually I've just figured it out myself...I hadn't referenced the Word object library under tools.

Always something simple!

like image 63
Steve O'Dea Avatar answered Dec 27 '22 04:12

Steve O'Dea