Consulting

Results 1 to 6 of 6

Thread: Bookmarks in a Word Document-Copy & Paste

  1. #1
    VBAX Regular
    Joined
    Apr 2011
    Posts
    72
    Location

    Bookmarks in a Word Document-Copy & Paste

    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!

  2. #2
    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
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  3. #3
    VBAX Regular
    Joined
    Apr 2011
    Posts
    72
    Location
    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!
    Attached Files Attached Files

  4. #4
    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
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  5. #5
    VBAX Regular
    Joined
    Apr 2011
    Posts
    72
    Location
    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.

  6. #6
    VBAX Regular
    Joined
    Apr 2011
    Posts
    72
    Location
    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!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •