Any ideas... Iterating through fields...

J

JGM

Hi all,

I wrote the following code to scan fields in a target document, looking for
a DATABASE field that matches one in a source document. If the field is
found, then I check for the presence of a second field next to it, a PRINT
field so that I can insert one if necessary. The goal is to automate the
insertion of PRINT fields that will allow me to automatically generates PDF
links after I convert the Word document.

The problem:

The target document can have anywhere from 300 to 1000 fields, if the one I
need is the the 12th, it flies, OTH, if it is the 958th, then it takes a
quite a long time to find it... I tried two versions:

'_______________________________________
For i = 1 To TargetDoc.Fields.Count
If InStr(TargetDoc.Fields(i).Code.Text, FullTextKey) <> 0 Then
TargetTextFound = True
Exit For
End If
Application.StatusBar = CLng((100 * _
i) / TargetDoc.Fields.Count) & " % des fiches examinées..."
Next i
'_______________________________________

or

'_______________________________________
Dim myTargetField As Field
For Each myTargetField In TargetDoc.Fields
i = myTargetField.Index
If InStr(myTargetField.Code.Text, FullTextKey) <> 0 Then
TargetTextFound = True
Exit For
End If
Application.StatusBar = CLng((100 * _
i) / TargetDoc.Fields.Count) & " % des fiches examinées..."
Next myTargetField
'_______________________________________

Is it the best Word VBA can do? Or is there another way to go tjhrough the
field collection?
Is it advisable to set up the target document so that all field codes are
toggled on, and then use the Find method instead?

TIA
 
J

JGM

Hey, don't waste your time...

I checked my theory and I managed to go from 5- 60 seconds (Depending on
the field position in the target document) to a flat 10 seconds.

I used this code instead:
'_______________________________________
'Look for target field
With Windows(TargetDoc.Name)
.Selection.Find.ClearFormatting
With .Selection.Find
.Text = FullTextKey
.Forward = True
.Wrap = wdFindStop
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
.Selection.Find.Execute
.Selection.Find.ClearFormatting
If .Selection.Find.Found Then
TargetTextFound = True
End If
End With
'_______________________________________

Of course, to help speed it up, I have to toggle to normal view, show field
codes, show all, but if done in the right sequence you avoid major
renumbering of pages, and I do it while the document is invisible...

So I'll take a flat 10 seconds (including checking the source document for
suitability, opening the target document, searching it, saving and closing
it, and resetting the source document to its original state...) to an
average of 40 seconds or so...

Have a great weekend !
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top