PDA

View Full Version : Bookmarks in a Word Document-Copy & Paste



rsrasc
01-30-2023, 09:55 AM
Hi all,

I would like to know if there is a possibility to copy all the bookmarks in a document (long document) and paste all of them into a new document.

I searched the database here but couldn't find anything on it.

I found a vba code to do this but the output was not what I was looking for.


Thank you for your assistance.

Cheers!

gmayor
01-30-2023, 10:18 PM
Without knowing what the bookmarks contain, or how you want the output the following should point the way.
Sub Macro1()
Dim oRng As Range, oNewRng As Range
Dim oBM As Bookmark
Dim oDoc As Document, oNew As Document
Set oDoc = ActiveDocument
Set oNew = Documents.Add
Set oNewRng = oNew.Range
For Each oBM In oDoc.Bookmarks
Set oRng = oBM.Range
oNewRng.FormattedText = oRng.FormattedText
oNewRng.Collapse 0
oNewRng.Text = vbCr & vbCr
oNewRng.Collapse 0
Next oBM
End Sub

rsrasc
01-31-2023, 02:22 AM
Hello GMayor,

Thank you for taking the time to do this. Like you said, not knowing what the bookmarks contains I'm taking this opportunity to attach a sample of the file that I'm using. The file that I'm using to run the code has over 2000 pages but this one is only a sample. BTW, your code runs really fast.


With this file, you may be able to see how it is structure and maybe help me in the process with the final out.

Thank you in advance for your assistance.

Cheers!

gmayor
01-31-2023, 03:06 AM
The following should be close

Sub Macro1()
Dim oRng As Range, oNewRng As Range
Dim oBM As Bookmark
Dim oDoc As Document, oNew As Document
Set oDoc = ActiveDocument
Set oNew = Documents.Add
Set oNewRng = oNew.Range
For Each oBM In oDoc.Bookmarks
Set oRng = oBM.Range
oRng.MoveStartWhile Chr(13)
oRng.MoveEndWhile Chr(13), wdBackward
oNewRng.FormattedText = oRng.FormattedText
oNewRng.Collapse 0
oNewRng.Text = vbCr
oNewRng.Collapse 0
Next oBM
End Sub

rsrasc
01-31-2023, 05:26 AM
Thanks for the code. If possible, two things. First, would it be possible to exclude just the text information? Second, would it be possible to capture any info that is in parentheses--especially in italics (showing in blue) ? That will be awesome.

rsrasc
02-01-2023, 02:49 AM
Hello Mr. Mayor,

Sorry to bother you. I have a small request about your code. Would it be possible to update your code to reflect or capture any bookmark in brackets? Just an idea and I think it will work. Thoughts? Thank you! Cheers!