-
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!
-
[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'
-
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]
-
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'
-
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
-
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'
-
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
-
Forum Rules