Disabling Copy Text Macro

B

boredofcities

I have a macro that copies the text from one text formfield to another in a
document that I have created. I am using this code below.

Sub CopyText()

With ActiveDocument
.FormFields("Bookmark1").Result = .FormFields("Bookmark2").Result
End With

End Sub

The problem I'm having is when the macro has run, I want it to be disabled,
so it doesn't overwrite the amendments that anyone makes to the formfield.

How would I go about doing this?

Using Office 2007, with protected for filling in forms.

Thanks for your time.

Matt.
 
K

Karl E. Peterson

boredofcities said:
I have a macro that copies the text from one text formfield to another in a
document that I have created. I am using this code below.

Sub CopyText()

With ActiveDocument
.FormFields("Bookmark1").Result = .FormFields("Bookmark2").Result
End With

End Sub

The problem I'm having is when the macro has run, I want it to be disabled,
so it doesn't overwrite the amendments that anyone makes to the formfield.

How would I go about doing this?

One common way to prevent code from running more than once is with a
Static flag. Using your example...

Sub CopyText()
Static BeenHere As Boolean
If Not BeenHere Then
With ActiveDocument
.FormFields("Bookmark1").Result =
..FormFields("Bookmark2").Result
End With
BeenHere = True
End If
End Sub

Of course, that only prevents it from happening more than once per
execution. If you want to do it based on a given field content, you'd
have to test for that, I suppose.
 
K

Karl E. Peterson

boredofcities said:
I have a macro that copies the text from one text formfield to another in a
document that I have created. I am using this code below.

Sub CopyText()

With ActiveDocument
.FormFields("Bookmark1").Result = .FormFields("Bookmark2").Result
End With

End Sub

The problem I'm having is when the macro has run, I want it to be disabled,
so it doesn't overwrite the amendments that anyone makes to the formfield.

How would I go about doing this?

One common way to prevent code from running more than once is with a
Static flag. Using your example...

Sub CopyText()
Static BeenHere As Boolean
If Not BeenHere Then
With ActiveDocument
.FormFields("Bookmark1").Result =
..FormFields("Bookmark2").Result
End With
BeenHere = True
End If
End Sub

Of course, that only prevents it from happening more than once per
execution. If you want to do it based on a given field content, you'd
have to test for that, I suppose.
 
B

boredofcities

Thanks Karl, it seems to do the trick. I will do some further testing but
from what I can tell it solves the initial problem that I was having.

Thanks again,

Matt.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top