Although I had the previous macro on file, this is probably the better
approach. The following will work - with most of the code ensuring that the
DocVariable field {DocVariable varFname} alone should update when the macro
is run.
Sub InsertFileNameSegment()
Dim vPath As Variant
Dim sName As String
Dim oVars As Variables
Dim oSection As Section
Dim oHeader As HeaderFooter
Dim oFooter As HeaderFooter
Dim oField As Field
Set oVars = ActiveDocument.Variables
With ActiveDocument
If Len(.Path) = 0 Then .Save
vPath = Split(.FullName, "\")
sName = vPath(UBound(vPath) - 1)
sName = sName & "\" & Left(.name, InStrRev(.name, ".") - 1)
oVars("varFname").Value = sName
For Each oField In .Fields
If oField.Type = wdFieldDocVariable Then
oField.Update
End If
Next oField
For Each oSection In ActiveDocument.Sections
For Each oHeader In oSection.Headers
If oHeader.Exists Then
For Each oField In oHeader.Range.Fields
If oField.Type = wdFieldDocVariable Then
oField.Update
End If
Next oField
End If
Next oHeader
For Each oFooter In oSection.Footers
If oFooter.Exists Then
For Each oField In oFooter.Range.Fields
If oField.Type = wdFieldDocVariable Then
oField.Update
End If
Next oField
End If
Next oFooter
Next oSection
End With
End Sub
--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP
My web site
www.gmayor.com
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>