#1
|
|||
|
|||
VBA and Bookmarks
Hello everyone,
I am not a pro with vba so I hope I word this correct. I am using vba in Word 2016 to merge a document. In my legal style of the document, the combined defendants sometimes has "as his/her spouse" at the end of the 2nd defendant's name. I insert a bookmark at the beginning of the legal style and another at the end of the style to search for the words "as his/her spouse. And, if found place it in other areas of the document. This done with additional bookmarks. Below is the vba code I tried using. Problem is that, it inserts as his/her spouse even if it is not found between the bookmarks. What am I doing wrong? Legal Style Example Quote:
This is the code I used Code:
Sub SelectTextBetweenBookmarks() Dim mark As Range Set mark = ActiveDocument.Range mark.Start = mark.Bookmarks("Style1").Range.End mark.End = mark.Bookmarks("Style2").Range.Start mark.Select Selection.Find.ClearFormatting With Selection.Find .Text = "as his/her spouse" .Replacement.Text = "" .Forward = True .Wrap = wdFindContinue .Format = False .MatchCase = False .MatchWholeWord = False .MatchWildcards = False .MatchSoundsLike = False .MatchAllWordForms = False End With Selection.Find.Execute Selection.Copy Selection.GoTo What:=wdGoToBookmark, Name:="Spouse1" With ActiveDocument.Bookmarks .DefaultSorting = wdSortByLocation .ShowHidden = True End With Selection.PasteAndFormat (wdFormatOriginalFormatting) Selection.GoTo What:=wdGoToBookmark, Name:="Spouse2" With ActiveDocument.Bookmarks .DefaultSorting = wdSortByLocation .ShowHidden = True End With Selection.PasteAndFormat (wdFormatOriginalFormatting) End Sub Thanks in advance for your assistance. Last edited by macropod; 10-20-2017 at 02:12 PM. Reason: Added quote & code tags |
#2
|
||||
|
||||
The insertion of this conditional "as his/her spouse" text would be better handled during the merge itself, so as to obviate the need for post-merge processing. In the Mailmerge Tips and Tricks 'Sticky' thread:
https://www.msofficeforums.com/mail-...ps-tricks.html is a field code to Conditionally Merge Spouse Data that could be adapted for that purpose.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
Thread Tools | |
Display Modes | |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Deleting only all bookmarks contents not the bookmarks | adilprodigy | Word VBA | 1 | 10-11-2017 01:31 PM |
Bookmarks | Schildkröte | Word VBA | 8 | 06-28-2015 11:13 AM |
Form updating Bookmarks - writes to the bookmarks multiple times | PeterPlys | Word VBA | 13 | 01-14-2015 06:41 AM |
Can't See Bookmarks | bobmayo | Word | 21 | 06-04-2013 07:37 AM |
Bookmarks for a PDF? | Ownaholic | Word | 0 | 10-30-2010 12:27 AM |