Solved

VBA Word Macro - Copy contents of existing bookmark and paste inside another bookmark

Posted on 2011-09-27
8
4,542 Views
Last Modified: 2013-11-26
Hi

I am trying to figure out how to copy and paste a bookmark in the same document.  

I have some hidden paragraphs that are bookmarked selections and I want to copy one and paste
one to another location within the same document.  The newly pasted text needs to be inside a
new bookmark (created programatically) that I can find later and update or delete as necessary.

Pseudo code would be something like:

Find existing bookmarked selection
Copy bookmarked selection
Jump to another location in the document
Paste the selection inside a new bookmark (with formatting) so it can be found and deleted later if necessary

Where I keep falling down is around pasting the formatted text inside a new bookmark that doesn't either

1. End up corresponding to an existing bookmark
2. Replacing an existing bookmark
3 Being just outside of the bookmark.

Hope the above makes sense and any help would be much appreciated
0
Comment
Question by:pm-archtect
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
8 Comments
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 36712072
The trick is to re-add the bookmark after the paste
Sub CopyToBookmark()
    Dim rng As Range
    
    ActiveDocument.Bookmarks("source").Range.Copy
    Set rng = ActiveDocument.Bookmarks("dest").Range
    rng.Paste
    ActiveDocument.Bookmarks.Add "dest", rng
End Sub

Open in new window

0
 

Author Comment

by:pm-archtect
ID: 36712665
Thank you for such a prompt response - however, it doesn't seem to work.  

As it stands, the above code only appears to copy some of the bookmark selection. so I modified it slightly.  The final outcome is still that the pasted text seems to be outside of the bookmark.  If I may explain:

Using Word, if I choose an existing bookmark and click "Goto" the section of text inside the bookmark is found and highlighted.

If I try running the code, the existing bookmark is copied and pasted ok and the new bookmark exists, but when I use Word and click "Goto", the cursor lands just before the pasted block.  The code is below:

    Selection.GoTo What:=wdGoToBookmark, Name:="bmExistingBookmark"
    Selection.Copy
   
    Selection.GoTo What:=wdGoToBookmark, Name:="bmAnchor1"
    ActiveDocument.Bookmarks.Add ("Test")
   
    Set rng = ActiveDocument.Bookmarks("bmNewBookmark").Range
    rng.PasteAndFormat (wdFormatOriginalFormatting)
    rng.Font.Hidden = False
    ActiveDocument.Bookmarks.Add "Test", rng

Any further help would be appreciated.
0
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 36713133
I am confused. You seem to be using four bookmarks, not two. You are getting the range of 'bmNewBookmark' and pasting there, but re-adding a different bookmark.
0
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 

Author Comment

by:pm-archtect
ID: 36713247
My apologies for any confusion - I changed the bookmark names to hide the real code and made a mistake whilst doing so - it should read;

    Selection.GoTo What:=wdGoToBookmark, Name:="bmExistingBookmark"
    Selection.Copy
   
    Selection.GoTo What:=wdGoToBookmark, Name:="bmAnchor1"
    ActiveDocument.Bookmarks.Add ("bmNewBookmark")
   
    Set rng = ActiveDocument.Bookmarks("bmNewBookmark").Range
    rng.PasteAndFormat (wdFormatOriginalFormatting)
    rng.Font.Hidden = False
    ActiveDocument.Bookmarks.Add "bmNewBookmark", rng

To the best of my knowledge, the code locates the existing bookmark (bkExistingBookmark) and copies the text.  It then jumps to a known location (bmAnchor), adds "bmNewBookmark" as a new bookmark, it then creates a range object relating to "bmNewBookmark" and then pastes into the range and lastly, recreates "bmNewBookmark".

As mentioned, the result is the pasted text still resides outside of the bookmark selection when you use the "Goto" button.

Apologies once again
0
 
LVL 76

Accepted Solution

by:
GrahamSkan earned 500 total points
ID: 36713384
Is this any better?
Sub CopyBetweenBookMarks()
    Dim rng As Range
    ActiveDocument.Bookmarks("bmExistingBookmark").Range.Copy
    ActiveDocument.Bookmarks.Add "bmNewBookmark", ActiveDocument.Bookmarks("bmAnchor1").Range
   
    Set rng = ActiveDocument.Bookmarks("bmNewBookmark").Range
    rng.Paste
    rng.Font.Hidden = False
    ActiveDocument.Bookmarks.Add "bmNewBookmark", rng
End Sub

Open in new window

0
 

Author Comment

by:pm-archtect
ID: 36713478
This works perfectly thank you :-)

Would you be able to explain the difference between the first and second attempt please?

One final question if I may (and I appreciate this might be unrelated).

The initial bookmark text is hidden (font.hidden = true).  If I unhide the text and run the code, it all works fine.  If I hide all the text and then run the code, only the table gets pasted to the new bookmark (the initial bookmark consists of some text followed by a table) ... Sigh.

Thank you again.
0
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 36713639
I don't like using Selection and GoTo. It is easy to lose the position and to get a character or two or a line or two in the wrong position. I find it better to work on the objects in the Word Object Model.

When I tried PasteAndFormat, the resulting Range object (rng) was set to to start of the new range instead of the total span, so if you can live with Paste instead of PasteAndFormat, that is good. If not, we'll have to look to copying the formatting.
0
 

Author Closing Comment

by:pm-archtect
ID: 36714791
Execellent help and really quick response - thank you
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Preface: When I started this series, I used the term CommandBars because that is the Office Object class that it discusses. Unfortunately, when Microsoft introduced Office 2007, they replaced the standard Commandbar menus with "The Ribbon" and rem…
Using Word 2013, I was experiencing some incredible lag when typing.  Here's what worked for me....
This video shows the viewer how to set up and create Footnotes in their document. Click on the References tab: Select "Insert Footnote": Type in desired text:
This video shows where to find the word count, how to display it, and what it breaks down to in Microsoft Word.

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question