Legal Learning was telling us:
Legal Learning nous racontait que :
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
'_______________________________________
--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site:
http://www.word.mvps.org