Solved

Access Word Fill-In fields through name

Posted on 2004-10-19
8
570 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
 
LVL 3

Author Comment

by:dabitbol
ID: 12347077
I'll try that in about an hour, I have to leave, thanks, looks good!
0
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

707 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now