Here is the code. Thanks for your help!
Sub AddFileName()
Dim sSubject As String
Dim pRange As Word.Range
For Each pRange In ActiveDocument.StoryRanges
Do Until (pRange Is Nothing)
Select Case pRange.StoryType
Case wdFirstPageFooterStory, _
wdPrimaryFooterStory, wdEvenPagesFooterStory
On Error Resume Next
pRange.Tables(1).Cell(1, 1).Range.Fields.Add Range:=Selection.Range,
Type:=wdFieldSubject
On Error GoTo 0
Case Else
'Do nothing
End Select
Set pRange = pRange.NextStoryRange
Loop
Next
End Sub
Legal Learning was telling us:
Legal Learning nous racontait que :
> Here is the code. Thanks for your help!
>
[quoted text clipped - 35 lines]
>>
>> Post the relevant code.
You still did not mention the error you got... But from looking at your
code, I guess it has to do with the following line:
pRange.Tables(1).Cell(1, 1).Range.Fields.Add Range:=Selection.Range,
Type:=wdFieldSubject
The code will add the field at the range represented by the current
selection, not in the first cell of the table as desired.
See the following code for a correction.
'_______________________________________
Sub AddFileName()
Dim sSubject As String
Dim pRange As Word.Range
Dim rgeCell As Range
For Each pRange In ActiveDocument.StoryRanges
Do Until (pRange Is Nothing)
Select Case pRange.StoryType
Case wdFirstPageFooterStory, _
wdPrimaryFooterStory, wdEvenPagesFooterStory
On Error Resume Next
Set rgeCell = pRange.Tables(1).Cell(1, 1).Range
rgeCell.Collapse wdCollapseStart
rgeCell.Fields.Add Range:=rgeCell, Type:=wdFieldSubject
On Error GoTo 0
Case Else
'Do nothing
End Select
Set pRange = pRange.NextStoryRange
Loop
Next
End Sub
'_______________________________________
Why the
On Error Resume Next
line?
It is always better to code for predictable errors than to have a Band-Aid
that will let everything go.
I guess it has to do with the fact that some footers may not have a table.
If this is the case, try this instead:
'_______________________________________
Sub AddFileName()
Dim sSubject As String
Dim pRange As Word.Range
Dim rgeCell As Range
For Each pRange In ActiveDocument.StoryRanges
Do Until (pRange Is Nothing)
Select Case pRange.StoryType
Case wdFirstPageFooterStory, _
wdPrimaryFooterStory, wdEvenPagesFooterStory
If pRange.Tables.Count > 0 Then
Set rgeCell = pRange.Tables(1).Cell(1, 1).Range
rgeCell.Collapse wdCollapseStart
rgeCell.Fields.Add Range:=rgeCell, Type:=wdFieldSubject
End If
Case Else
'Do nothing
End Select
Set pRange = pRange.NextStoryRange
Loop
Next
End Sub
'_______________________________________

Signature
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
jmarcilREMOVE@CAPSsympatico.caTHISTOO
Word MVP site: http://www.word.mvps.org
Legal Learning - 19 Nov 2007 22:23 GMT
Perfect! You rock. Thanks so much.

Signature
CLG
> Legal Learning was telling us:
> Legal Learning nous racontait que :
[quoted text clipped - 111 lines]
> End Sub
> '_______________________________________