Solved

Access Word Fill-In fields through name

Posted on 2004-10-19
8
573 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
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 
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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
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…

776 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