Solved

VBA ForNext Loop to cycle through collection of Word Fields

Posted on 2004-03-30
3
3,180 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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
VBA saving file message display 5 63
VB6 - Convert HH:MM into Decimal 8 54
Recommendation vb6 to vb.net or others 14 142
Export Data to Different .csv Files 26 103
Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

785 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