Replace Paragraph char with <p> in Word document

Setup
OS: NT
VB: 6.0
References in VB Project:
1. MS Word 9.0 Object libray
2. OLE Automation
3. VB Runtime objects and procedures
4. VB for applications
5. VB objects and procedures

Problem: I have written a function to replace paragraph char (^p) in word document to <p>. In my system it works fine but in other VB environment it does not work. Functionality is replace and save word doc in text format. I have attached replace method code and portion of the method calling replace method.

Please help me. Thanks in advance

*************

Set objDoc = Application.Documents.Open(FileArray(I), AddToRecentFiles:=False)

replace
   
sNewName = NameWithoutExtension(objDoc.Name)
           
objDoc.SaveAs FileName:=TargetPath & sNewName, FileFormat:=wdFormatTextLineBreaks
        objDoc.Close wdDoNotSaveChanges
        Set objDoc = Nothing

****************

Sub replace()

On Error GoTo err_handle2

    Word.Selection.Find.ClearFormatting
    Word.Selection.Find.Replacement.ClearFormatting
    With Word.Selection.Find
        .Text = "^p"
        .Replacement.Text = "<p></p>^p"
        .Forward = True
        .Wrap = wdFindContinue
        .Format = False
        .MatchCase = False
        .MatchWholeWord = False
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
    End With
    Word.Selection.Find.Execute replace:=wdReplaceAll
    Exit Sub
err_handle2:
End Sub

govinda051198Asked:
Who is Participating?
 
rasmusj2Connect With a Mentor Commented:
I would suspect that "replace" might be a keyword in the other OS.  Maybe you could rename the Sub replace () to Sub ReplaceParagraphMarks ().  It is certainly worth a try.
0
 
tureCommented:
govinda,

Just an untested idea... I suspect the line
  .Wrap = wdFindContinue

Try this instead, and see if it helps:
  .Wrap = wdFindStop

Otherwise, word might keep looping as long as there are paragraph characters in the document, and there will always be some, because they are included in the replacement text.

And... not important, but shouldn't the replacement text be:
  .Replacement.Text = "</p>^p<p>"   ???

Ture Magnusson
Karlstad, Sweden
0
 
govinda051198Author Commented:
Ture,

I tried, but no luck.

Thanks for considering the question!
0
 
tureCommented:
Good thinkin' rasmusj2!

/Ture
0
 
rasmusj2Commented:
Hope that works!  Or, I will have to keep thinking....
0
All Courses

From novice to tech pro — start learning today.