Solved

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

Posted on 2011-09-27
8
4,214 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
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

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

 

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: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

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

Suggested Solutions

Title # Comments Views Activity
copy the content of word to outlook 3 67
Child Form in front 4 49
Microsoft Word startup Issue 7 34
VBA code in Word 2017 will no longer insert picture 8 26
A theme is a collection of property settings that allow you to define the look of pages and controls, and then apply the look consistently across pages in an application. Themes can be made up of a set of elements: skins, style sheets, images, and o…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
This video shows where to find templates, what they are used for, and how to create and save a custom template using Microsoft Word.
In a previous video Micro Tutorial here at Experts Exchange (http://www.experts-exchange.com/videos/1358/How-to-get-a-free-trial-of-Office-365-with-the-Office-2016-desktop-applications.html), I explained how to get a free, one-month trial of Office …

856 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