Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2011-09-27
8
Medium Priority
?
5,446 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
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

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 2000 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
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.
This video walks the viewer through the process of creating a watermark for their document, customizing it, and saving it for viewing/printing needs.
Learn how to create and modify your own paragraph styles in Microsoft Word. This can be helpful when wanting to make consistently referenced styles throughout a document or template.

972 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