PDA

View Full Version : Help with setting form field width please...



Tecnik
05-10-2007, 06:34 AM
Hi there,

Please can someone help me with this query. I've read a number of posts trying to find an answer but as yet haven't found an answer.

I've a small macro that adds a formfield at the position of the cursor which is usually in the cell of a table. What I'm trying to do, but failing, is then set the width of the formfield just added.

I'm adding the field like this:-
Selection.FormFields.Add Selection.Range, wdFieldFormTextInput

And then trying to set the width like this:-

ActiveDocument.FormFields.Item(1).Name = "fldName"
ActiveDocument.FormFields.Item("fldName").TextInput.Width = 2

It's going wrong here:-
ActiveDocument.FormFields.Item(1).Name = "fldName"

I think it's because the index is incorrect.

Please can someone point me in the right direction.

Thank you,

Nick

fumei
05-10-2007, 08:40 AM
Can you describe "failing"? Selection.FormFields.Add Range:=Selection.Range, Type:= _
wdFieldFormTextInput
With ActiveDocument.FormFields.Item(1)
.Name = "fldName"
.TextInput.Width = 2
End Withworks fine for me.

But...it only works if the inserted formfield IS, in fact, Item(1). If, say, there are two formfields before the newly inserted one, the first one of THOSE will be actioned with the name, and TextInput.

Note also that the formfield made a length = 2 will NOT look that way immediately.

You may also want to try: Selection.FormFields.Add Range:=Selection.Range, Type:= _
wdFieldFormTextInput
Selection.MoveStart Unit:=wdCharacter, Count:=-1
With Selection.FormFields(1)
.Name = "fldName"
With .TextInput
.EditType Type:=wdRegularText, Default:="", Format:=""
.Width = 2
End With
End With

Tecnik
05-11-2007, 12:36 AM
Hi Gerry,

Thank you for your help with this query and for the code.

Having set the properties of the field is there a way of getting the field to display correctly? You did mention that the field won't display correctly immediately. Is there a way of specifying the width in the line below rather than 'selecting' the new field and applying properties to it?

Selection.FormFields.Add Range:=Selection.Range, Type:= _
wdFieldFormTextInput

The reason I ask is because I'm trying to add some date fields that look like 'xx/xx/xx' where the x's are the formfields with a two character width. The forward slashes are just normal text.

Thanks again,

Nick

fumei
05-11-2007, 05:40 AM
Is there a way of specifying the width in the line below rather than 'selecting' the new field and applying properties to it?I don't understand this.

Why not use a text formfield set as Date?

And what do you mean by "line below"? Below??

As for not displaying correctly, as long as the userform has the focus, the formfield is created, but the screen will not show correctly. As soon as focus returns to the document itself, the formfield will show correctly.

Tecnik
05-11-2007, 05:54 AM
Thanks for your reply Gerry,

I didn't explain properly, when I said 'line below' I meant the line of code below. What I was trying to say was, rather than add the field, select it and then apply properties to it, is it possible to add a field with these properties?

As for the focus thing, that doesn't seem to affect it. When the field is created the shaded area is about 5 chars long (going by the invisibles). If I then go into the properties I can see that maximum length has been set to 2. Only when I click ok does the shaded area display correctly?

I'd looked at making the field a date format but it wasn't quite what was wanted display wise.

Thanks again for the help Gerry,

Regards,

Nick

fumei
05-11-2007, 10:16 PM
rather than add the field, select it and then apply properties to it, is it possible to add a field with these properties?Apparently not.

An alternative to using Selection would be:Dim oFF As FormField
Set oFF = ActiveDocument.FormFields.Add _
(Range:=Selection.Range, Type:= _
wdFieldFormTextInput)
With oFF
.Name = "fldName"
With .TextInput
.EditType Type:=wdRegularText, Default:="", Format:=""
.Width = 2
End With
.Enabled = True
End WithBut you still have to grab the object after it has been inserted/created.

As for making it show as 2....ummm, I would have thought there would be, but I can't find it.

ScreenRefresh does not do it. Still trying.

Tecnik
05-14-2007, 01:33 AM
Hi Gerry,

Thanks for your reply and for the new code and trying to find an answer to the query. Entering the properties dialog box and then exiting it must be firing something that updates the redraw of the form field but that's getting a little deep for me.

Thanks again for your time,

Nick

fumei
05-14-2007, 11:01 AM
It is not too deep for me, if I can find the darn thing.

Even if you record a macro doing just that - entering the properties dialong, pressing Ok - and tack it on the end...it does NOT update/redraw the formfield.

Wait a sec.....hmmmmmm

fumei
05-14-2007, 11:03 AM
Dim oFF As FormField
Set oFF = ActiveDocument.FormFields.Add _
(Range:=Selection.Range, Type:= _
wdFieldFormTextInput)
With oFF
.Name = "fldName"
With .TextInput
.EditType Type:=wdRegularText, Default:="", Format:=""
.Width = 2
End With
.Enabled = True
End With
ActiveDocument.Fields.UpdateThere you go. It works.

Tecnik
05-16-2007, 12:45 AM
Hi Gerry,

Sorry for not replying sooner, had a day off yesterday.

Thanks for your reply and for the new code which works perfectly.
Just that one line makes all the difference.

Thank you for the time you've spent on this query. :)

Regards,

Nick