Solved

Access Word Fill-In fields through name

Posted on 2004-10-19
8
574 Views
Last Modified: 2011-04-14
Hey,

I have a Word Document Template that contains fill-in fields. Now, I'm able to access these fields through VB using the Word Object Library. The only problem I have, is that of what I can see on the WORD DOM, I can only access those fields using there numerical index (Document.Fields(1).Range.Text). Problem is that looking at the docuemtn with Word, there is no way for me to determine what is each of the field's index. I thought they were index by the way they were disposed but no.

I'm looking for a way in VB to access those fields using there name or Bookmark or something.

Thanks
0
Comment
Question by:dabitbol
  • 4
  • 4
8 Comments
 
LVL 16

Expert Comment

by:jimbobmcgee
ID: 12346871
I'm sure you already have but, in case you haven't, try:

     Document.Fields("myfieldname").Range.Text

as you would with, say, the Excel Sheets collection...

J.
0
 
LVL 3

Author Comment

by:dabitbol
ID: 12346919
No, actually it's Document.Fields.Item(i).Range.Text and it can only be an integer. I tried already thanks anyway.
0
 
LVL 16

Expert Comment

by:jimbobmcgee
ID: 12347058
>> Document.Fields("myfieldname").Range.Text

Scratch that, the fields don't appear to have Name properties.  You could always try this workaround function:

    Function GetFieldNumber(TextInPrompt As String) as Long
   
        'THIS FUNCTION GETS THE FIRST FIELD WITH THE SPECIFIED PROMPT CONTENT
       
        For fld = 1 To ThisDocument.Fields.Count
       
            checkcode = ThisDocument.Fields(fld).Code
            If InStr(1, checkcode, TextInPrompt) <> 0 Then GetFieldNumber = fld
           
        Next fld
   
    End Function

Now you could get the index number of a field that contained 'myPrompt01' in its code, with

    fieldnum = GetFieldNumber("myPrompt01")

or use it inline, with

    ThisDocument.Fields(GetFieldNumber("myPrompt01")).Result

HTH

J.
0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
LVL 3

Author Comment

by:dabitbol
ID: 12347077
I'll try that in about an hour, I have to leave, thanks, looks good!
0
 
LVL 16

Accepted Solution

by:
jimbobmcgee earned 250 total points
ID: 12347187
Another implementation of the same thing:

    Function GetFieldNumber(TextInCode As String) As Long
   
        'THIS FUNCTION GETS THE LAST FIELD WITH THE SPECIFIED PROMPT CONTENT
       
        Dim checkcode, fld        

        For Each fld In ThisDocument.Fields
       
            checkcode = fld.Code
           
            If InStr(1, checkcode, TextInCode) <> 0 Then
                GetFieldNumber = fld.Index
            End If
           
        Next fld
   
    End Function

If you prefer to use For Each..., instead of For...

J

(NB:  Both functions will actually return the LAST field to contain that prompt in its code.  If you want the first one, place an Exit Function command before End If...)
0
 
LVL 3

Author Comment

by:dabitbol
ID: 12348213
Thanks, I didn't do it quite like this, but I am giving you the points for your prompt response and effort.

Thanks!
0
 
LVL 16

Expert Comment

by:jimbobmcgee
ID: 12348279
Thanks for the points; how did you do it, in the end?

J.
0
 
LVL 3

Author Comment

by:dabitbol
ID: 12348290
You have to use FormFields and not Fields. FormFields can be accessed with there bookmark name.

0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question