Form automation with VBA

M

melon

How do I select and update an individual "textbox"? I tried to access
it with bookmark, then do a Range.Insert but it ends up removing the
"textbox" (the new text simply ioverride the empty textbox in the
template) for me.
 
M

melon

Alright, it seems that I can use Document.Fields, however I can only
use a numeric index for it. Is there anyway to search it through the
"bookmark name" given in Word?
 
P

Pesach Shelnitz

The Document.Fields property retrieves the collection of all fields in the
main text story. If you have DOCVARIABLE fields in your doc, they can be
identified by the variable name that you add when you create the field. They
do not have or need bookmarks. For example, if you create DOCVARIABLE fields
with the identifiers CustomerName and OrderNumber, they will appear as
{DOCVARIABLE CustomerName} and {DOCVARIABLE OrderNumber} when reveal codes is
enabled. Note that identical fields can be repeated as many times as you like
in the doc. If you have fields like these, you can use the following code to
feed text into the automatically created variables that are associated with
them and then update the fields with the values of these variables without
knowing anything about their indexes.

ActiveDocument.Variables("CustomerName").Value = "John Doe"
ActiveDocument.Variables("OrderNumber").Value = "1234"
ActiveDocument.Fields.Update

These fields are convenient to use when you have a separate form with text
boxes and other controls, in which the user can supply the values. In that
case, the following code can be used to populate the fields.

ActiveDocument.Variables("CustomerName").Value = MyForm.TextBox1.Value
ActiveDocument.Variables("OrderNumber").Value = MyForm.TextBox2.Value
ActiveDocument.Fields.Update

I have only briefly described a part of the overall process of using
controls to obtain information for populating fields in a document. Let
everyone on this newsgroup know if you need more details to accomplish what
you are trying to do.
 
G

Gordon Bentley-Mix on news.microsoft.com

Assuming that by "textbox" you mean a 'Text' type formfield, rather than
inserting the desired content into the range of the formfield, set the
..Result property of the formfield. This will allow you to reference the
formfield by bookmark name using something like:

ActiveDocument.FormFields("Text1").Result = "I like cheese!"

You may also want to add some error handling to ensure that the formfield
exists before trying to set the .Result property. This can be done by
evaluating the .Exists property of the specified bookmark; e.g.:

If ActiveDocument.Bookmarks.Exist("Text1") = True Then
ActiveDocument.FormFields("Text1").Result = "I like cheese!"
End If

Note that there are alternate methods which may work better, and in my
experience, it is best to use formfield only when a formfield is truly
required. For example, Pesach has briefly described one method using
DOCVARIABLE fields. I regularly use just plain ol' bookmarks with a bit of
code to "restore" the bookmark after inserting the content. My code (which is
a "generic" sub that accept arguments) looks like this:

Public Sub InsertBookmarkValue(BkmkName As String, ByVal InsertValue As
String)
With ActiveDocument
If .Bookmarks.Exists(BkmkName) Then
Dim myRange As Range
Set myRange = .Bookmarks(BkmkName).Range
myRange.Text = InsertValue
.Bookmarks.Add BkmkName, myRange
End If
End With
End Sub

If you'd like a bit more explanation on the above just ask.
--
Cheers!

Gordon Bentley-Mix
Word MVP

Please post all follow-ups to the newsgroup.

Read the original version of this post in the Office Discussion Groups - no
membership required!
 
M

melon

Thanks for your reply.

I found that your method work very good on plain text. However I am
having problem when using InsertFile: ( I need to use InsertFile as
the data source is a rtf). Code is in C#:

if (doc.Bookmarks.Exists((string)b)) {
object r = doc.Bookmarks.get_Item(ref b).Range;
((Range)r).InsertFile(f, ref Missing, ref Missing, ref Missing, ref
Missing);
doc.Bookmarks.Add((string)b, ref r);
}

The problem is InsertFile doesn't seems to insert the file in the
range, rather it insert the file AFTER the range (similar to how
InsertAfter works)

A more visual illustration:

using range.text = "something"
[something] <-- this is good

using range.InsertFile
[]something <-- this is not good

If I run the code again, instead of replacing the existing text, it
will just insert the new data after it.
 

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