Solved

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

Posted on 2011-09-27
8
4,072 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
Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

 

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

Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

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

Suggested Solutions

A few years ago I was very much a beginner at VBA, and that very much remains the case today.  I'll do my best to explain things as I go in the hope that other beginners can follow.  If you just want to check out a tool that creates a Select Case fu…
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.
This video walks the viewer through the process of creating an MLA formatted document, as well as a bibliography with citations.
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.

810 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