PDA

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



GoldServe
04-25-2007, 11:36 AM
I have the following text:

text Some more text
Some more more text

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!

mdmackillop
04-25-2007, 01:57 PM
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

GoldServe
04-25-2007, 04:42 PM
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?


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

mdmackillop
04-25-2007, 11:44 PM
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

GoldServe
04-26-2007, 07:15 AM
Thanks. I got it now. One more quick question. How do I select from the end of a bookmark instead:

text Some more text
Some more more text

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

mdmackillop
04-26-2007, 10:55 AM
How about
St = ActiveDocument.Bookmarks("BM1").End
Ed = ActiveDocument.Bookmarks("BM2").End

fumei
04-27-2007, 08:29 PM
Just curious:
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 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?