Macro to Copy and Paste Multiple Bookmark Contents into a New Document

I have a document with four separate bookmarks through out the document. Using a vba macro, I need to copy all the contents (text and tables) in each of the bookmarks, open a new Word document and paste these bookmarks into a new document when a macro is executed. The bookmarks each have titles to identify them by. I can copy each bookmark separately but I am unsure how to automatically paste the bookmark contents when the new document is opened/created, nor how to copy multiple bookmarks at a time.

Currently I am using Document.Add to add a new Word Document. I'm not sure if this is relevant but I am currently using 'ActiveDocument' throughout the VBA coding as the document will become a template for others to fill out information in and the file name will not be constant.

Thank you for your help
BAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

NorieVBA ExpertCommented:
Can you post your current code?
0
BAuthor Commented:
Sorry I should have added my code before! It's a bit all over the place as I'm new to VBA but basically so far I am adding all the sections in an additional bookmark at the bottom of the document and then aiming to copy that one bookmark across to the new document

Sub ExportABCD()

ActiveDocument.Content.Select 'Select Document
Selection.Collapse Direction:=wdCollapseEnd 'Go to end of document
ActiveDocument.Bookmarks.Add "START" 'Add a new bookmark called 'START'

ActiveDocument.Content.Select 'Select Document
Selection.Collapse Direction:=wdCollapseEnd 'Go to end of document (after 'START' bookmark)
ActiveDocument.Bookmarks.Add "Export_ABCD" 'Add a new bookmark called 'Export_ABCD'

Dim BMRange As Range
Set BMRange = ActiveDocument.Bookmarks("Export_ABCD").Range 'Start range of 'Export_Report' Bookmark
    
    Selection.TypeText "Title" & Chr(11)
    
    Selection.TypeText Chr(11) & "Title A"
    ActiveDocument.Bookmarks("Section A").Range.Copy: Selection.Paste
    
    Selection.TypeText Chr(11) & "Title B"
    ActiveDocument.Bookmarks("Section B").Range.Copy: Selection.Paste

ActiveDocument.Bookmarks.Add "Export_ABCD", BMRange 'End range of 'Export_Report' Bookmark


ActiveDocument.Content.Select 'Select Document
Selection.Collapse Direction:=wdCollapseEnd 'Go to end of document (after 'Export_Report' bookmark)
ActiveDocument.Bookmarks.Add "END" 'Add a new bookmark called 'END'
          
Dim rngStart As Range, rngEnd As Range
Set rngStart = ActiveDocument.Bookmarks("Start").Range 'Set the start range to copy
Set rngEnd = ActiveDocument.Bookmarks("End").Range 'Set the end range to copy

ActiveDocument.Range(rngStart.Start, rngEnd.End).Copy 'Copy from start to end range (entire 'Export_Report' bookmark)

Dim mytemplate
Set mytemplate = ActiveDocument.AttachedTemplate
Documents.Add 'Template:=mytemplate?

End Sub

Open in new window

0
GrahamSkanRetiredCommented:
Several things you might like to know.

Firstly, declare (Dim) all your variables. Otherwise misspelt variables can give unexpected results, whose cause can be hard to trace.
You can make the compiler report undeclared variables by having 'Option Explicit' at the top of each code module. There is an option to do this automatically when a new module is created: Tools>Options>Editor tab, Require Variable declaration checkbox.  (While I'm there, I always uncheck Auto Syntax Check, because dealing with the warning message that appears when you try to leave an unfinished line of code gets annoying.)

Secondly, try to use the a Range object, instead of the one Selection object. It's faster, there is less document jumping about, and you can use as many Range objects as you need,

Thirdly, there is a built-it 'virtual' bookmark called "\EndOfDoc" which means that you probably don't need to keep adding extra bookmarks at the end.

See how this matches what you are trying to do
Sub ExportABCD2()
    Dim docA As Document
    Dim docNew As Document
    Dim mytemplate As Word.Template
    
    Set docA = ActiveDocument
    
    
    Set mytemplate = ActiveDocument.AttachedTemplate
    Set docNew = Documents.Add(mytemplate)
    
    
    docNew.Bookmarks("\EndOfDoc").Text = "Title" & Chr(11)
    docNew.Bookmarks("\EndOfDoc").Text = Chr(11) & "Title A"
    docA.Bookmarks("Section A").Range.Copy
    docNew.Bookmarks("\EndOfDoc").Range.Paste
    
    docNew.Bookmarks("\EndOfDoc").Text = Chr(13) & "Title B"
    docA.Bookmarks("Section B").Range.Copy
    docNew.Bookmarks("\EndOfDoc").Range.Paste
    docA.Range.Copy
    docNew.Bookmarks("\EndOfDoc").Range.Paste
End Sub

Open in new window

2

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

BAuthor Commented:
Hi Graham, thank you for your response. I have added Option Explicit to the beginning of the module.

I have tried running the code but at line 13 it doesn't like the
.text = "title"

Open in new window

and highlights this with the error message method or data member not found. But this does seem to work instead:

docNew.Bookmarks("\EndOfDoc").Select
    Selection.TypeText "Title" & Chr(11)

Open in new window


Is there a reason why it wasn't working before? And is there a better way to get around this error message?
0
GrahamSkanRetiredCommented:
Sorry. With no sample document to work with, I posted the code untested.

In all the cases where I have put something like objBookMark.Text = "Something", it should be objBookMark.Range.Text = "Something".

The message means that 'Text is not a member of a bookmark object. Range is a member and Text is a member of a Range.
You code will work, but it is longer and makes unnecessary use of the Selection object.
0
BAuthor Commented:
That's ok, sorry about that!

Thank you for your response
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
VBA

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.