Oh, the frustration!

B

bw

http://word.mvps.org/FAQs/MacrosVBA/InsertingTextAtBookmark.htm

The site above has provided me with the following code (notice the error on
line 5):

Sub AddPeriod()
Dim BMRange As Range
'Identify current Bookmark range and insert text
Set BMRange = ActiveDocument.Bookmarks("Text1").Range
BMRange.Text = "Hello world" 'I get an error on this line: "Run-time
error '6028': The range cannot be deleted."
'Re-insert the bookmark
ActiveDocument.Bookmarks.Add "Text1", BMRange
End Sub

I simply put two field forms on a "1TestForm" template, the first of which
executes (on exit) the "AddPeriod" macro above, locked the form, saved the
template, then open a new document based on "1TestForm" template.

I have tried this with code that unprotects and protects the form (and still
leaves the data intact), but I always get this error.

Well, the frustration comes from the fact that I made it work ONE TIME, but
I now don't know how!!!

Help would be appreciated..
Thanks,
Bernie
 
B

bw

Thanks for the reply Anne,
No, I'm not sure the bookmark name is still "Text1", and I don't know how to
check if it is. The code I have shown here I have seen many times in
different locations on the internet. So why wouldn't it work for me?
Thanks again,
Bernie
 
A

Anne Troy

Bernie: If I create a new document and insert a text form field, by default
its bookmark name is Text1. So that's what they used in the sample, much
like we'd use A1 to provide a sample Excel formula, if that makes sense.
Well, you said it worked for you the first time. Now, suppose I delete that
form field and create another IN THE SAME document. That new form field
will, by default, be named Text2. Hence the code would no longer work. To
find the bookmark name of a form field, just double-click it and look. Not
all will have bookmark names, either.
************
Anne Troy
www.OfficeArticles.com
 
B

bw

Oh, I thought you meant the same after the code execution. Yes, the
bookmark name is Text1. I copied that code from the link I provided. I
used a new workbook/worksheet, and made two form fields Text1 and Text2.
That field was Text1 before and after execution of the Macro.

I didn't say it worked the FIRST time. I said it worked after adding all
kinds of code and messing with it to try different things to get it to work.

Please add two form fields to your own document/template, then add this code
to see if it works. If it does, I'd appreciate it if you would send me the
template.

Please, please, please?
Thanks,
Bernie
 
T

Tony Jollans

What program are you using - Word, or Excel? Word does not have Workbooks
and Worksheets.

Assuming you are in Word, the code does not work for FormFields - rather for
'normal'Bookmarks in a document. What are you trying to achieve?
 
B

bw

Thanks Tony!

Yea, I know...I'm not making any sense at this point. Sorry about that!!!
I'm using WORD.

I'm glad that I now know that the code DOES NOT work for FormFields. Thanks
so much for that. I will not be so frustrated now.

When I exit a FormField, I want to run a macro that will add a "." (period)
at the end of the string entered, if a period was not entered. Simple!! (or
so I thought).

I hope you have a solution for this.

Bernie
 
T

Tony Jollans

Hi Bernie,

Attach an Exit Macro to the FormField, something like this ..

Sub ExitText1()

With ActiveDocument.FormFields("Text1")
If Right(.Result, 1) <> "." Then
.Result = .Result & "."
End If
End With

End Sub
 
B

bw

Hooray!

FormFields, not Bookmarks!!!
Thanks so much Tony. I knew this was would be easy, but it wasn't for me,
as I didn't know where to begin.
Your little bit of code will now help me to learn and expand on my
introduction to VB for Word.
I'm cooling down now. BP is now only 200/140. You're a life saver. :eek:)

I really appreciate your help...
Bernie
 
T

Tony Jollans

My pleasure!

--
Enjoy,
Tony


bw said:
Hooray!

FormFields, not Bookmarks!!!
Thanks so much Tony. I knew this was would be easy, but it wasn't for me,
as I didn't know where to begin.
Your little bit of code will now help me to learn and expand on my
introduction to VB for Word.
I'm cooling down now. BP is now only 200/140. You're a life saver. :eek:)

I really appreciate your help...
Bernie
 

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

Similar Threads

Underline 2
inserted page referencing a bookmark 2
Date Field Clear 1
Insert file contents within an enclosed bookmark. 4
VB Runtime Error 91 6
Runtime Error 6028 0
Bookmarks 6
Textbox Changes 1

Top