Solved

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

Posted on 2011-09-27
8
3,779 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
  • 4
  • 4
8 Comments
 
LVL 76

Expert Comment

by:GrahamSkan
Comment Utility
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
Comment Utility
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
Comment Utility
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
 

Author Comment

by:pm-archtect
Comment Utility
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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
LVL 76

Accepted Solution

by:
GrahamSkan earned 500 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Execellent help and really quick response - thank you
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
In this video, we show how to convert an image-only PDF file into a PDF Searchable Image file, that is, a file with both the image (typically from scanning) and text, which is created in an automated fashion with Optical Character Recognition (OCR) …
The viewer will learn how to make their project stand out over others by learning how to change colors and shapes, add spaces, change directions, and add bullets to their charts.

772 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now