Problem with VBA code and editing Word docs

%3CLINGO-SUB%20id%3D%22lingo-sub-3296921%22%20slang%3D%22en-US%22%3EProblem%20with%20VBA%20code%20and%20editing%20Word%20docs%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-3296921%22%20slang%3D%22en-US%22%3E%3CP%3EThe%20following%20code%20has%20run%20well%20for%20over%20a%20year%20and%20recently%20stopped%20working.%26nbsp%3B%20It%20opens%20the%20Word%20doc%2C%20does%20a%20find%20and%20replace%2C%20saves%20the%20modified%20doc%20as%20a%20PDF%20then%20closed%20the%20original%20doc%20without%20changing%20it.%26nbsp%3B%20All%20this%20works%20except%20the%20find%20and%20replace%20-%20for%20some%20reason%20the%20doc%20cannot%20be%20edited.%26nbsp%3B%20Anyone%20have%20any%20ideas%3F%26nbsp%3B%20There%20are%20no%20errors%20during%20the%20run.%26nbsp%3B%20Thx%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ESet%20wrdDoc%20%3D%20wrdApp.Documents.Open(%22c%3A%5CJIBE%5Ctesting.docx%22)%3CBR%20%2F%3EWith%20WordApp%3CBR%20%2F%3EWith%20wrdDoc.Content.Find%3CBR%20%2F%3E.ClearFormatting%3CBR%20%2F%3E.Text%20%3D%20%22**VesselName**%22%3CBR%20%2F%3E.Replacement.Text%20%3D%20cls!VesselName%3CBR%20%2F%3E.Execute%20Replace%3A%3DwdReplaceAll%2C%20Forward%3A%3DTrue%2C%20Wrap%3A%3DwdFindContinue%3CBR%20%2F%3EEnd%20With%3CBR%20%2F%3EWith%20wrdDoc.Content.Find%3CBR%20%2F%3E.ClearFormatting%3CBR%20%2F%3E.Text%20%3D%20%22**CustomsDecal**%22%3CBR%20%2F%3E.Replacement.Text%20%3D%20cls!CustomsDecal%3CBR%20%2F%3E.Execute%20Replace%3A%3DwdReplaceAll%2C%20Forward%3A%3DTrue%2C%20Wrap%3A%3DwdFindContinue%3CBR%20%2F%3EEnd%20With%3CBR%20%2F%3EWith%20wrdDoc.Content.Find%3CBR%20%2F%3E.ClearFormatting%3CBR%20%2F%3E.Text%20%3D%20%22**DocNumber**%22%3CBR%20%2F%3E.Replacement.Text%20%3D%20cls!DocNumber%3CBR%20%2F%3E.Execute%20Replace%3A%3DwdReplaceAll%2C%20Forward%3A%3DTrue%2C%20Wrap%3A%3DwdFindContinue%3CBR%20%2F%3EEnd%20With%3CBR%20%2F%3EEnd%20With%3CBR%20%2F%3EwrdDoc.SaveAs2%20%22c%3A%5CPDFtemp%5CJIBETemp%20-%20Customs.PDF%22%2C%2017%3CBR%20%2F%3EwrdDoc.Close%20SaveChanges%3A%3DFalse%3CBR%20%2F%3EShellAndWait%20%22c%3A%5CPDFtemp%5CJIBETemp%20-%20Customs.PDF%22%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-3296921%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EAccess%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-3297721%22%20slang%3D%22en-US%22%3ERe%3A%20Problem%20with%20VBA%20code%20and%20editing%20Word%20docs%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-3297721%22%20slang%3D%22en-US%22%3EYes%2C%20when%20run%20in%20debug%20mode%20the%20cls%20data%20fields%20all%20have%20the%20correct%20data.%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-3297258%22%20slang%3D%22en-US%22%3ERe%3A%20Problem%20with%20VBA%20code%20and%20editing%20Word%20docs%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-3297258%22%20slang%3D%22en-US%22%3Eif%20it%20is%20working%20sometime%20ago%2C%20you%20check%20the%20recordset%2C%20cls%2C%20if%20it%20has%20any%20records%20on%20it.%3C%2FLINGO-BODY%3E
New Contributor

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
if it is working sometime ago, you check the recordset, cls, if it has any records on it.
Yes, when run in debug mode the cls data fields all have the correct data.