|
-
Feb 4th, 2008, 01:34 PM
#1
Thread Starter
New Member
[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.
-
Feb 4th, 2008, 03:23 PM
#2
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:
ActiveDocument.Characters(ActiveDocument.Characters.Count).Select 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
-
Feb 5th, 2008, 01:56 PM
#3
Thread Starter
New Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|