Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 10-20-2017, 08:49 AM
Formd Formd is offline VBA and Bookmarks Windows 7 32bit VBA and Bookmarks Office 2007
Advanced Beginner
VBA and Bookmarks
 
Join Date: Feb 2015
Location: TX
Posts: 46
Formd is on a distinguished road
Default 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:
The Lending Institution

Plaintiff

vs.

{Style1}John doe, Jane doe as his/her spouse, Mary Jane,
Jack Spratt, ABC Company, The Home Owners Association,
Unknown tenants, Unknown Spouse.{Style2}

Defendant

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
Reply With Quote
  #2  
Old 10-20-2017, 02:18 PM
macropod's Avatar
macropod macropod is offline VBA and Bookmarks Windows 7 64bit VBA and Bookmarks Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,963
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

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]
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
VBA and Bookmarks 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
VBA and Bookmarks 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

Other Forums: Access Forums

All times are GMT -7. The time now is 11:33 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft