Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Access Word Fill-In fields through name

Posted on 2004-10-19
8
Medium Priority
?
578 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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 750 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…
Suggested Courses

730 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