Solved

VBA ForNext Loop to cycle through collection of Word Fields

Posted on 2004-03-30
3
3,294 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
[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
  • 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
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…
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…
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…

695 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