Solved

VBA ForNext Loop to cycle through collection of Word Fields

Posted on 2004-03-30
3
3,140 Views
Last Modified: 2008-03-06
I am trying to cycle through the list of word fields I have in my document.  I have bookmarked the fields with the equivalent fieldname in my active database.  What I want to do is to go through all fields on the document using a fornext loop to pull out the name of the field to use in my update routine. I want it to do something like this

For i = 1 to ActiveDocument.Fields.Count
     fldupd = ActiveDocument.Fields.i.fieldname
     --Line of code to update database -- (ActiveDocument.Field.i.fieldname, ActiveDocument.Bookmarks("fieldname").Range.Text)
     ActiveDocument.Fields.Next
Next

I could also use a DoWhile command.  I just want to be able to have it parse the fields and update the database in whatever order they appear in the collection.  At the moment, I can't think of a way to do it.  If you've got an answer, I'd love to hear it.

The reason I want to do it this way is so a Jr. Programmer can just add a field with the correct name to the form as necessay and update the database.

Thanks!
0
Comment
Question by:vivekpara
  • 2
3 Comments
 
LVL 14

Accepted Solution

by:
Tommy Kinard earned 500 total points
ID: 10718534
Hi vivekpara,

The below sub will give you the name of each bookmark.

Sub UpdateDBWrdFields()
    Dim A As Word.Field
    Dim FldUpd As String
    For Each A In ActiveDocument.Fields
        If A.Result.Bookmarks.Count > 0 Then
            FldUpd = A.Result.Bookmarks(1).Name
        End If
    Next
End Sub

HTH
dragontooth

0
 
LVL 2

Author Comment

by:vivekpara
ID: 10727546
Great solution...worked like a charm.
0
 
LVL 14

Expert Comment

by:Tommy Kinard
ID: 10727589
Glad I could help!

dragontooth

0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

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

762 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

23 Experts available now in Live!

Get 1:1 Help Now