Is there a workaround for a bug with automation related to StoryRanges

P

PromisedOyster

The following code iterates through all merge fields:

foreach (Word.Range range in _wordApp.ActiveDocument.StoryRanges)
{
Word.Range story = range;
while (story != null)
{
foreach (Word.Field field in story.Fields)
{
if (field.Type == Word.WdFieldType.wdFieldMergeField)
{
ProcessField(field.Code.Text);
}
}
story = story.NextStoryRange;
}
}

Unfortunately, if section 1's Header is blank but there is a section 2
Header, the above code doesn't work. It does not pick up the existence
of the section 2's Header
 
G

Greg Maxey

While I have never seen code like yours, you might try:
Sub ScratchMacro()
Dim pRange As Word.Range
Dim oFld As Word.Field
Dim iLink As Long
iLink = ActiveDocument.Sections(1).Headers(1).Range.StoryType * I don't
know why, but this line is supposed to correct the issue of blank/skip
header and footers.
For Each pRange In ActiveDocument.StoryRanges
Do
For Each oFld In pRange.Fields
Select Case oFld.Type
Case wdFieldMergeField
MsgBox "Field found"
'Do your deed here.
Case Else
'Do nothing
End Select
Next
Set pRange = pRange.NextStoryRange
Loop Until pRange Is Nothing
Next
End Sub
 

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