Solved

Access Word Fill-In fields through name

Posted on 2004-10-19
8
577 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
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…
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…
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…
Suggested Courses
Course of the Month10 days, 1 hour left to enroll

624 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