PDA

View Full Version : Solved: How to use Bookmarks as variables



Humbled
07-15-2008, 10:33 AM
Advance apologies on such a basic question, but until I strip myself away from the keyboard and hit the bookstore (or maybe Amazon) for a VBA reference guide, I'm stuck. Along those lines, any suggestions for a good beginner's guide to VBA? Tons of coding experience, just nothing recent (all mainframe-based).

My predicament: I have a Word document that uses form fields (from the Forms toolbar, I'm not going to try and tackle UserForms this week).

Each field has been assigned a bookmark name. I'm having trouble extracting the value of the field to use throughout the remainder of the macro.

Yesterday - with the help of this board - I learned I can extract a text field using:

Dim Store as String
Store = ActiveDocument.Bookmarks("PHCY").Range.Text


I can't find anything that makes sense for a date field. The field in question has a bookmark name = "dtUAT", and is formatted to display as "DDD, M/d/yy". I've tried the same as above (.range.text), as well as (.range.select) plus a few others with no success.

I chose my username ("Humbled") with good reason, thanks for your help. Maybe one day I'll be able to help out around here.

fumei
07-16-2008, 09:00 AM
"Each field has been assigned a bookmark name. "

Very good. Most people do not realize that the bookmark matching a formfield is assigned. They treat them as if they are equivalent...as you do in your code.

Your code gets the bookmark text via .Range.Text.

However, you can also get the formfield text with its Result:
Dim Store As String
Store = ActiveDocument.FormFields("PHCY").Result
Try using .Result of the formfield, rather than .Range.Text of the bookmark.

"I can't find anything that makes sense for a date field. The field in question has a bookmark name = "dtUAT", and is formatted to display as "DDD, M/d/yy". I've tried the same as above (.range.text), as well as (.range.select) plus a few others with no success."

What does "no success" mean? You are getting nothing at all? It is not formatted the way you think it is supposed to be?

CreganTur
07-16-2008, 09:09 AM
I've tried the same as above (.range.text), as well as (.range.select) plus a few others with no success.

What do you mean by 'with no success'? Are you getting an error when you're trying to make the value of your variable be the .text of the formfield?

I've attached a document that has an example formfield that is formatted to Date, using the exact DDD, M/d/yy formatting you cite above.
-Go into the VBE, put your cursor anywhere in the procedure for the document, and press F5- it should print the value of the formfield (and another bookmark) to the immediate window.

NinjaEdithttp://img293.imageshack.us/img293/9060/ninja3od8.gif: Beat me to the punch, gerry :neener:
-really good to see you back!

Humbled
07-18-2008, 02:22 PM
Thanks everyone for the responses.

I should have offered a better explanation than "with no success" ... I know better.

The code as I had it written (in various forms) was not returning the value of the field. Since I've since fixed it, I can't describe the values - or in some cases errors - I was receiving. To that end, I'm marking this thread solved.