Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Word Copy and Paste Bookmark

Status
Not open for further replies.

JimLes

IS-IT--Management
Feb 27, 2006
119
US
This code works great but I need the text to be formatted when I paste it. Any ideas?

Private Sub CopyButton_Click()
Dim myRanges As Range
Dim newsection As String
Set myRanges = ActiveDocument.Bookmarks("Interview").Range
newsection = myRanges.FormattedText

With ActiveDocument
If .ProtectionType <> wdNoProtection Then
Unprotect Password:=""
End If

If ActiveDocument.Bookmarks.Exists("NewPage") Then
Set myRanges = ActiveDocument.Bookmarks("NewPage").Range
myRanges = newsection '.FormattedText
ActiveDocument.Bookmarks.Add "NewPage", myRanges
End If

Protect Type:=wdAllowOnlyFormFields, _
NoReset:=True, Password:=""


End With

End Sub
 
Hi JimLes,

Try:
Code:
Private Sub CopyButton_Click()
  With ActiveDocument
    If .ProtectionType <> wdNoProtection Then .Unprotect Password:=""
    If .Bookmarks.Exists("NewPage") Then
      .Bookmarks("Interview").Range.Copy
      .Bookmarks("NewPage").Range.Paste
    End If
    .Protect Type:=wdAllowOnlyFormFields, NoReset:=True, Password:=""
  End With
End Sub

PS: when posting code, please use the code tags.


Cheers
[MS MVP - Word]
 
I keep getting a message "Code Execution Has Been Interrupted". The code below runs if I keep hitting the "Continue" button. Any Ideas??

Here is my tweaked code to retain the bookmark:

Private Sub CopyButton_Click()

With ActiveDocument
If .ProtectionType <> wdNoProtection Then .Unprotect Password:=""
If .Bookmarks.Exists("NewPage") Then
.Bookmarks("Interview").Range.Copy
Selection.GoTo What:=wdGoToBookmark, Name:="NewPage"
Selection.TypeParagraph
Selection.PasteAndFormat (wdPasteDefault)
End If
.Protect Type:=wdAllowOnlyFormFields, NoReset:=True, Password:=""
End With
End Sub
 
Hi JimLes,

Please use code tags when posting code!

You don't need to select anything. Try:
Code:
Private Sub CopyButton_Click()
Dim oRng As Range
  With ActiveDocument
    If .ProtectionType <> wdNoProtection Then .Unprotect Password:=""
    If .Bookmarks.Exists("NewPage") Then
      .Bookmarks("Interview").Range.Copy
      Set oRng = .Bookmarks("NewPage").Range
      oRng.Paste
      .Bookmarks.Add "NewPage", oRng
    End If
    .Protect Type:=wdAllowOnlyFormFields, NoReset:=True, Password:=""
  End With
End Sub


Cheers
[MS MVP - Word]
 
Why are you using Bookmarks, when clearly you are dealing with formfields?? Otherwise why is there turning protection on/off.

If you are dealing with formfields, USE formfields.


unknown
 
Hi Gerry,

Maybe Jim wants to replicate some boilerplate text.


Cheers
[MS MVP - Word]
 
So, I see, you have formfields in the document, so you turn off the protection required to use formfields, so you can test if a bookmark exists, and then regardless of its content copy its range into another bookmark, which delets that bookmark so you need to re-create it.

Ah.

it uses:
Code:
If .Bookmarks.Exists("NewPage")
not:
Code:
If .Bookmarks("NewPage").Range.Text <> ""


unknown
 
Hi Gerry,

As I see it, the .Bookmarks.Exists("NewPage") test is a safeguard. If something had caused it to be deleted from the document, the macro would error-out.

OTOH, it's not clear to me that there's a need for the copying, pasting & bookmark updating at all, since all that seems to be happening is that the "NewPage" range is being kept in synch with the "Interview" range. On the face of it, the only things required are:
1. A cross-reference field at the "NewPage" range pointing to the "Interview" bookmark; and, perhaps
2. Macro code to ensure the cross-reference field gets updated.
Still, I don't know enough about the OP's project to say whether this is so - there may be a lot more going on that we don't know about.


Cheers
[MS MVP - Word]
 
As I see it, the .Bookmarks.Exists("NewPage") test is a safeguard. If something had caused it to be deleted from the document, the macro would error-out."

Oh no it doesn't.

If you execute .Bookmarks.Exists("NewPage"), and it does NOT exist, there is NO error. The instruction returns a False, and that is all. NO error.

Plus, there is no Else instruction. Sooooo.
Code:
Private Sub CopyButton_Click()
Dim oRng As Range
  With ActiveDocument
    If .ProtectionType <> wdNoProtection Then .Unprotect Password:=""
    If .Bookmarks.Exists("NewPage") Then
      .Bookmarks("Interview").Range.Copy
      Set oRng = .Bookmarks("NewPage").Range
      oRng.Paste
      .Bookmarks.Add "NewPage", oRng
    End If
    .Protect Type:=wdAllowOnlyFormFields, NoReset:=True, Password:=""
  End With
End Sub
If there is no Bookmark "NewPage", protection is turned off, then on again, with nothing else happening.


"Still, I don't know enough about the OP's project to say whether this is so - there may be a lot more going on that we don't know about. "

I suspect so.

I also still suspect there are formfields affected.


unknown
 
Hi Gerry,

If you delete the 'If .Bookmarks.Exists("NewPage")' test and the 'NewPage' doesn't exist, you'll get an error. You'll also get an error if the 'Interview' bookmark is empty or non-existent...


Cheers
[MS MVP - Word]
 
But your comment:

"As I see it, the .Bookmarks.Exists("NewPage") test is a safeguard. If something had caused it to be deleted from the document, the macro would error-out."

DID have the test. So i am not sure what:

"If you delete the 'If .Bookmarks.Exists("NewPage")' test "

means. Other than, well...yes, true.

If you try to execute:
Code:
Set oRng = .Bookmarks("NewPage").Range
all by its lonesome, yeah, sure, THAT gets an error. But your comment was: "As I see it, the .Bookmarks.Exists("NewPage") test is a safeguard. If something had caused it to be deleted from the document, the macro would error-out."

With, one would assume, the "something" deleted being the bookmark.

Anyway, I agree that it looks like the OP wants to synch NewPage and Interview, and yes, it would be better to do just that via a reference, rather than any copy/paste operation.


unknown
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top