Log in

View Full Version : Solved: Insert text into bookmark or form field?



rob0923
09-18-2009, 08:23 PM
Hi,

I am using word to do small calculations using formfields. The user only needs to insert the first number and the second number is auto transfered from an excel document. I am having trouble inserting this the range I need transfered in the formfield area. It does have an enclosed bookmark and the activedocument("bookmark").range.insertbefore is not placing it into the form field area. But it does place it just before the formfield area. So my question being, is there a way to insert the range into the form field area?

Or if it isn't possible. Is it possible to replace the formfield area with a new form text and give it a specific name and insert the value into this area?

Thanks in advance!

macropod
09-19-2009, 05:11 AM
Hi Rob,

You can update a firmfield's value using code like:

Sub UpdateFormField(FFName As String, FFText As String)
ActiveDocument.Bookmarks(FFName).Range.Fields(1).Result.Text = FFText
End Sub
where the calling routine passes both the formfield's name (FFName) and content (FFText) as parameters.

Alternatively, you can update a bookmarked range's contents with code like:

Sub UpdateBookmark (BmkNm as string, NewTxt as string)
Dim BmkRng as Range
With ActiveDocument
Set BmkRng =.Bookmarks(BmkNm).Range
BmkRng.Text = NewTxt
.Bookmarks.Add BmkNm, BmkRng
End With
Set BmkRng = Nothing
End Subwhere the calling routine passes both the bookmark's name (BmkNm) and content (NewTxt) as parameters.

Use/modify as necessary to suit your requirements.

rob0923
09-19-2009, 06:15 AM
THis wors perfect. The only ohter thing would be is there anyway to make the calculation auto update?

For example Number 1 is inserted into the template and Number two is inserted by exce when it is populatedl, but once excel places the value in the form it doesn'tt update the remaing fields. Is it possible to force this update the remaining fields?

I do have the form area set up to display as a number $#,##,0.00, but it comes up as a general number after being inserted, maybe I will only need to force it to update the one field?

Thanks again!

macropod
09-20-2009, 05:11 PM
Hi Rob,

For the updates, you could insert a line like:
Activedocument.Fields.Update

For the formatting, you need to pass the formatted string to whichever block of code you're using to insert the data into Word and, if you want calculations based on that value to be properly formatted, you may need to add a numeric picture switch to the relevant fields (see Word's help file for details).

fumei
09-21-2009, 08:28 AM
I just want to point out that:
ActiveDocument.Bookmarks(FFName).Range.Fields(1).Result.Text = FFText


can also be:

ActiveDocument.FormField(FFName).Result = FFText
which puts the text FFText directly into the formfield, rather than the range of the formfield's bookmark. They are two different things.

macropod
09-21-2009, 04:01 PM
Hi fumei,

That's true, but you can't programmatically insert a string of over 255 characters into a formfield with:
ActiveDocument.FormFields(FFName).Result = FFText
Conversely, you need to have the document unprotected to use:

ActiveDocument.Bookmarks(FFName).Range.Fields(1).Result.Text = FFText

rob0923
09-21-2009, 04:57 PM
Thanks for your input. I ended up using a user form because when I updated the fields it reset my two main values that I needed to calculate textforms. It then forced the remaining calculations to a value of 0.

Thanks again!

fumei
09-22-2009, 09:56 AM
Considering the question was one of "calculation" - of numbers one would assume - I did not think the 255 character limit was relevant, but of course you are correct, it could be.