Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Fine & Replace code not working 2

Status
Not open for further replies.

MarjiS

Programmer
Jul 5, 2005
13
US
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?

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
 
What happens if you replace this:
oApp.Selection.Find.Execute Replace:=wdReplaceAll
with this ?
oApp.Selection.Find.Execute Replace:=2 '2=wdReplaceAll

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
It certainly did! Thank you so very much!
So why will it take a number and not the code text, do you think?

Thanks again.
 
Tip: use the Option Explicit instruction.

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
PHV said:
Tip: use the Option Explicit instruction.

Number one tip for any VBA development IMO.

----------------------------------------------
Ben O'Hara
David W. Fenton said:
We could be confused in exactly the same way, but confusion might be like Nulls, and not comparable.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top