After Golom corrected my syntax in an earlier post, I'm having a different problem. The code below is supposed to open Word, opens a .txt document into Word, search for "+++" and replace with a hard page break, print the file to a selected printer, then close Word without saving the document.
It does everything except replace the "+++" with "^m". The first instance of "+++" is selected but not replaced. The code continues to the print and quit clauses without throwing an error. I made a macro of the process in Word and it's pretty much the same code I'm using. What am I doing wrong?
It does everything except replace the "+++" with "^m". The first instance of "+++" is selected but not replaced. The code continues to the print and quit clauses without throwing an error. I made a macro of the process in Word and it's pretty much the same code I'm using. What am I doing wrong?
Code:
Private Sub WordTest_Click()
Dim oApp As Object
Dim activePrinterTxt As String
'Create an instance of MS Word
Set oApp = CreateObject(Class:="Word.Application")
oApp.Visible = False
'Open the Document
oApp.Documents.Open Filename:="c:\export.txt"
'Replace +++ with page breaks
oApp.Selection.Find.ClearFormatting
oApp.Selection.Find.Replacement.ClearFormatting
With oApp.Selection.Find
.Text = "+++"
.Replacement.Text = "^m"
.Forward = True
'.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
oApp.Selection.Find.Execute Replace:=wdReplaceAll
'Print to the virtual printer
oApp.Options.PrintBackground = False
With oApp.ActiveDocument
.Saved = False
activePrinterTxt = oApp.ActivePrinter
oApp.ActivePrinter = "\\server1\hplj1"
.PrintOut
oApp.ActivePrinter = activePrinterTxt
.Close
End With
'Close Word
oApp.Quit
End Sub