Access fields contained in textbox within Headers/Footers?

G

gattsi

Hi,

I've seen a few posts out there that attempt to deal with accessing
fields within headers/footers but I've not been able to implement any
of the ideas succesfully using Word 2003 (SP2).

I need to access each field (contained in a textbox) within a
header/footer and use its code property (fld.code). Has anyone been
able to do this?

Thanks


For Each rngStory In ActiveDocument.StoryRanges
Do
If rngStory.ShapeRange.Count > 0 Then
For Each oShp In rngStory.ShapeRange

oShp.Select 'This throws a "member cannot be accessed
in this view" error!!
For Each fld In Selection
MsgBox fld.Code
Next

Next
End If
Set rngStory = rngStory.NextStoryRange
Loop Until rngStory Is Nothing
Next
 
J

Jezebel

A field in a textbox in a header is accessible using code like this --

activedocument.StoryRanges(wdPrimaryHeaderStory).ShapeRange(1).TextFrame.TextRange.Fields(1)
....
 
G

gattsi

Thanks for the reply.

I have a single textbox in a header that contains 2 fields ( {ref
test1} and {ref test2} ) and some plain text.

If I run the following code I get the error "The object does not
support attached text":

MsgBox
ActiveDocument.StoryRanges(wdPrimaryHeaderStory).ShapeRange(1).TextFrame.TextRange.Fields.Count

I can avoid this error by checking .HasText first, but this returns
false despite there being plain text and 2 fields in the textbox.

Any ideas?
 
J

Jezebel

The code works for me. Do you have different first page/left/right headers?
Do you have any other shapes in your header?
 
G

gattsi

I managed to get it going (sort of).

It seems that when the textbox is contained within a drawing canvas,
running the code you supplied generates the "The object does not
support attached text" error. If you disable the drawing canvas
(Tools->Options->General....) and insert a text box the code works
fine.

Unfortunately I cannot control whether the drawing canvas is used in a
user's template/document and still require a means of getting to these
fields in all scenarios.
 
G

gattsi

sorted it!

If rngStory.ShapeRange.Count > 0 Then
For Each oShp In rngStory.ShapeRange

'Go through drawing canvas' object
and process for each shape on the canvas
If oShp.Type = 20 Then
For Each oShpOnCanvas In
oShp.CanvasItems
If
oShpOnCanvas.TextFrame.TextRange.Fields.Count > 0 Then
For Each fld In
oShpOnCanvas.TextFrame.TextRange.Fields
strFieldsList =
GetFieldListDetail(fld.Code, strFieldsList)
Next
End If
Next
Else
'Shapes not on drawing canvas.
Inserted straight into header/footer.
If oShp.TextFrame.HasText Then
For Each fld In
oShp.TextFrame.TextRange.Fields
strFieldsList =
GetFieldListDetail(fld.Code, strFieldsList)
Next
End If
End If
Next
End If
 

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