Consulting

Results 1 to 7 of 7

Thread: Help: How to copy text between bookmarks and paste into new file

  1. #1

    Help: How to copy text between bookmarks and paste into new file

    I have the following text:

    [bookmark1]text[/bookmark1] Some more text
    [bookmark2] Some more more text [/bookmark2]

    Is there some code I can select all text from the beginning of bookmark1 to the end of bookmark2 including "Some more text" in the middle?

    I then want to past this into a new word document.

    Thanks!

  2. #2
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    [VBA]Option Explicit
    Sub CopyBM()
    Dim Rng As Range, St As Long, Ed As Long
    St = ActiveDocument.Bookmarks("BM1").Start
    Ed = ActiveDocument.Bookmarks("BM2").End

    Set Rng = ActiveDocument.Range(Start:=St, End:=Ed)
    Rng.Copy

    Documents.Add
    Selection.PasteAndFormat (wdPasteDefault)

    End Sub[/VBA]
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  3. #3
    Thanks for your reply. Basically I have a series of checkboxes and I want to scan over then and copy to a new document.

    The code works for the first control and copies to a new document just fine but when it loops, the focus is not on the active document so the bookmarks are not found. How do I work around this?

    [VBA]
    For i = 1 To 28
    control = "Check" & i
    checkbox = "checkbox" & i
    If ActiveDocument.FormFields(control).Result = True Then
    St = ActiveDocument.Bookmarks(control).Start
    Ed = ActiveDocument.Bookmarks(checkbox).End

    Set Rng = ActiveDocument.range(Start:=St, End:=Ed)
    Rng.Copy

    Documents.Add
    Selection.PasteAndFormat (wdPasteDefault)
    End If
    Next i
    [/VBA]

  4. #4
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Add the new document first, outwith the loop.
    Create Document variables for the Source and Destination documents.
    Use Source instead of ActiveDocument for the bookmark location.
    Use Destination to define/activate the paste location
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  5. #5
    Thanks. I got it now. One more quick question. How do I select from the end of a bookmark instead:

    [bookmark1]text[/bookmark1] Some more text
    [bookmark2] Some more more text [/bookmark2]

    I want to select "Some more text" to the end of bookmark2

  6. #6
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    How about
    [VBA]St = ActiveDocument.Bookmarks("BM1").End
    Ed = ActiveDocument.Bookmarks("BM2").End [/VBA]
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  7. #7
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    Just curious:
    [vba]For i = 1 To 28
    control = "Check" & i
    checkbox = "checkbox" & i
    If ActiveDocument.FormFields(control).Result = True Then
    St = ActiveDocument.Bookmarks(control).Start
    Ed = ActiveDocument.Bookmarks(checkbox).End

    Set Rng = ActiveDocument.range(Start:=St, End:=Ed)
    Rng.Copy

    Documents.Add
    Selection.PasteAndFormat (wdPasteDefault)
    End If
    Next i [/vba]will create 28 documents if each control is true. A new document is added for every iteration (if control is true). Is this really what you want?

    Also, are these really bookmarks? Or are they formfields?

Posting Permissions

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