Forum Discussion

MikeHuston's avatar
MikeHuston
Copper Contributor
Apr 27, 2022

Problem with VBA code and editing Word docs

The following code has run well for over a year and recently stopped working.  It opens the Word doc, does a find and replace, saves the modified doc as a PDF then closed the original doc without changing it.  All this works except the find and replace - for some reason the doc cannot be edited.  Anyone have any ideas?  There are no errors during the run.  Thx

 

Set wrdDoc = wrdApp.Documents.Open("c:\JIBE\testing.docx")
With WordApp
With wrdDoc.Content.Find
.ClearFormatting
.Text = "**VesselName**"
.Replacement.Text = cls!VesselName
.Execute Replace:=wdReplaceAll, Forward:=True, Wrap:=wdFindContinue
End With
With wrdDoc.Content.Find
.ClearFormatting
.Text = "**CustomsDecal**"
.Replacement.Text = cls!CustomsDecal
.Execute Replace:=wdReplaceAll, Forward:=True, Wrap:=wdFindContinue
End With
With wrdDoc.Content.Find
.ClearFormatting
.Text = "**DocNumber**"
.Replacement.Text = cls!DocNumber
.Execute Replace:=wdReplaceAll, Forward:=True, Wrap:=wdFindContinue
End With
End With
wrdDoc.SaveAs2 "c:\PDFtemp\JIBETemp - Customs.PDF", 17
wrdDoc.Close SaveChanges:=False
ShellAndWait "c:\PDFtemp\JIBETemp - Customs.PDF"

2 Replies

  • arnel_gp's avatar
    arnel_gp
    Iron Contributor
    if it is working sometime ago, you check the recordset, cls, if it has any records on it.
    • MikeHuston's avatar
      MikeHuston
      Copper Contributor
      Yes, when run in debug mode the cls data fields all have the correct data.

Resources