Link to home
Start Free TrialLog in
Avatar of govinda051198
govinda051198

asked on

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

Avatar of ture
ture

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
Avatar of govinda051198

ASKER

Ture,

I tried, but no luck.

Thanks for considering the question!
ASKER CERTIFIED SOLUTION
Avatar of rasmusj2
rasmusj2

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Good thinkin' rasmusj2!

/Ture
Hope that works!  Or, I will have to keep thinking....