[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

VBA ForNext Loop to cycle through collection of Word Fields

Posted on 2004-03-30
3
Medium Priority
?
3,413 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 2000 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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
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 process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Suggested Courses

831 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