triggering formfield formatting

E

Eric

I have a legal document set up as protected for filling out forms, and some
of the field forms have formatting applied to dates & currencies. When I
populate the document programmatically, the field formats I need
aren't applied until I manually tab through the doc. I tried the code below
but it doesn't do the trick.

TIA,
Eric

Private Sub TabThruForm(doc As Word.Document)
Dim k As Integer
Dim frmFlds As Word.FormFields
Set frmFlds = doc.FormFields
For k = 1 To frmFlds.Count
If k <> frmFlds.Count Then frmFlds(k).Next.Select
Next k
frmFlds(1).Select
End Sub
 
D

Doug Robbins - Word MVP

Why not use the Format() function to format the data that is being inserted
as the .Result of the formfield.

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 
E

Eric

I have different formats for different data. If I format the data first I
have to then know which piece of data I have, whereas now I am blissfully
ignorant of anything more than matching the data source (Excel) name to the
data target (Word bookmarked form field). The idea of formatting the data in
Excel isn't as appetizing either, as I don't always want the data in the
same format as when it is in the document.

Thanks,
Eric
 
D

Doug Robbins - Word MVP

How are you populating the formfields? Show use the code.

Sometimes a state of bliss will not get you what you want.

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 
E

Eric

Public Sub PopulateContractFormFields()
On Error GoTo PopulateContractFormFields_Error

Dim frmFields As Word.FormFields
Set frmFields = MContractDoc.ContractDocument.FormFields
Dim bkMarks As Word.Bookmarks
Set bkMarks = MContractDoc.ContractDocument.Bookmarks
Dim ff As Word.FormField
Dim thisNamedRange As String
Dim thisValue As Variant
For Each ff In frmFields
' every bookmark name shoulld have a corresponding excel name as
well
' use bookmarks on errors from setting form field result with very
long text
thisNamedRange = ff.Name
If thisNamedRange = "" Then
thisValue = Empty
Else
thisValue = ProjectData.Range(thisNamedRange)
If IsEmpty(thisValue) Then
thisValue = ""
Else
On Error Resume Next
ff.result = thisValue
bkMarks(thisNamedRange).Range.Fields(1).result.Text =
thisValue
On Error GoTo 0
End If
End If
Next ff

On Error GoTo 0
Set frmFields = Nothing
Set bkMarks = Nothing
Exit Sub

PopulateContractFormFields_Error:
Stop
Resume
End Sub
 
E

Eric

Doug

Philisophy aside, isn't there some way to trigger formfield formatting via
vba?

Thanks,
Eric
 
D

Doug Robbins - Word MVP

If I use the following code to populate a formfield with the bookmark name
of Text2 that is formatted as currency, the result is $100.00

With ActiveDocument
.FormFields("Text2").Result = 100
End With

I am not sure why you are doing both

ff.result = thisValue

and

bkMarks(thisNamedRange).Range.Fields(1).result.Text = thisValue


--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 
E

Eric

Thank you!! This is what I was trying to accomplish, and now can do:

If Len(thisValue) < 255 Then
' let Word doc field set format
ff.result = thisValue
Else
' avoid error (design flaw) that field.result gives on a string that's
too long
bkMarks(thisNamedRange).Range.Fields(1).result.Text = thisValue
End If

- Eric
 

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