Results 1 to 3 of 3

Thread: [RESOLVED] [Word] wdReplaceOne is not working when searched backward

  1. #1

    Thread Starter
    New Member
    Join Date
    Feb 2008
    Posts
    2

    Resolved [RESOLVED] [Word] wdReplaceOne is not working when searched backward

    Hello,

    I'm new here and it's been a long time sice I used VB and I have a problem with VB macro in Word. What I am trying to do is to find the last sentence from a paragragh with unknown number of sentences and create a new paragraph. So, I was going to find the last period from the end of a paragraph and replace it with a paragraph marker. However, this code replace all occasions of "period". It works when Forward is set to True, but somehow it doesn't when Forward is set to False. What am I doing wrong?

    Here is the code:

    ' Move to the end of a paragraph
    With Selection
    .ClearFormatting
    .MoveDown Unit:=wdParagraph, Count:=1, Extend:=wdMove
    .EndOf Unit:=wdParagraph, Extend:=wdMove
    .Collapse Direction:=wdCollapseEnd
    End With

    ' Find the last period replace with a paragraph marker
    With Selection.Find
    .ClearFormatting
    .Forward = False
    .Text = ". "
    .Replacement.ClearFormatting
    .Replacement.Text = "^p"
    .Execute Replace:=wdReplaceNone
    End With


    I appreciate any advice.

  2. #2
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: [Word] wdReplaceOne is not working when searched backward

    ta add a paragrapoh at the end of the document, you can try like this
    vb Code:
    1. ActiveDocument.Characters(ActiveDocument.Characters.Count).Select
    2. ActiveDocument.Paragraphs.Add
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  3. #3

    Thread Starter
    New Member
    Join Date
    Feb 2008
    Posts
    2

    Re: [Word] wdReplaceOne is not working when searched backward

    Pete,

    Thank you for the post. Though I could not use your code as is, but it lead me to an alternative way to complete the task using Sentences.Last property.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width