fill in text fields

B

Bart

Hello,


I have a document with a form, which has a few text-fields on it. The
document is protected from changes, except the text-fields.

I made a dialog in VBA where you can select certain data that should be
filled in in the selected text-field. I thought I could do this with:

Selection.TypeText Text:="filled data"

But I get error 4605 , stating this method or property is not available
because the document is locked from changes.


My question is: how can I get a text in the selected textfield from VBA?



Any help will be appreciated.

Greetings!
 
G

Greg Maxey

Bart,

I don't follow exactly what you are trying to do, but if you need to access
a protected form to do something you can use:

ActiveDocument.Unprotect
....Do something
Active Document.Protect Type:=wdAllowOnlyFormFields, NoReset:=True
 
B

Bart

The document is protected, except for the fields.

I need to fill in the field that is currently selected.

How can I do that?
 
J

Jay Freedman

Hi Bart,

If the length of the data from the dialog is less than 256 characters,
you can fill the formfield with it without unprotecting the form. It
takes some convoluted syntax, though:

Selection.Bookmarks(1).Range.Fields(1).Result.Text = "filled data"

If the string is 256 characters or more, you'll have to unprotect and
reprotect -- see
http://word.mvps.org/FAQs/MacrosVBA/SetLongFmFldResult.htm.

There's absolutely no way to use Selection.TypeText to do this. The
Selection includes not just the "fillable" portion of the formfield,
but also the formfield itself (represented by the braces when you
display field codes). When you try to use TypeText, VBA thinks you're
trying to replace the whole field with plain text, which isn't allowed
when the form is protected.
 
G

Greg Maxey

Bart,

I still don't understand precisely what it is that you are trying to do, but
if you are trying to put some text using VBA in a selected formfield and
then move immediately to the next field then I suppose that you could use
something like this:

Dim oFld
Dim oFormFld As String
Set oFld = ActiveDocument.FormFields

Selection.Bookmarks(1).Range.Fields(1).Result.Text = "Filled Data"
oFormFld = Selection.Bookmarks(1).Name
If oFld(oFormFld).Name <> oFld(oFld.Count).Name Then
oFld(oFormFld).Next.Select
Else
oFld(1).Select
End If

This means of course that you are not going to get a chance to edit the
Filled Data
 

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