Is It Possible to Only Show Part of a Filename in a Doc?

M

mtbcpa

I would like to display the filename and path in a document, but only have it
show part of the information. Is there a way to mask or just display partial
information?

For instance, if the file is c:/documents/2009/document.doc I would like to
have it display only 2009/document.

Thanks!
 
D

Doug Robbins - Word MVP

It cannot be done by the use of Word Fields alone. It would however be
possible to create a macro that would create a string that consisted of the
final folder of the file path, together with the name of the file minus its
extension. Such as string could then be assigned to a document variable and
be displayed in the document by the use of a { DOCVARIABLE } field.

--
Hope this helps

Doug Robbins - Word MVP
Please reply only to the newsgroups unless you wish to avail yourself of my
services on a paid, professional basis.
 
G

Graham Mayor

You cannot insert part of a field, but you can insert part of the path with
a macro - and as filename fields do not automatically update, using a macro
is arguably the better way to insert the document name. The following will
insert the current folder name and the document filename without the
extension at the end of the document. The only proviso is that the last
paragraph of the document must not contain a backslash character "\" as that
is used to determine whether the filename has been written. If this is a
problem then it would need more code to write the filename segment to a
docvariable or to a bookmarked location.

Sub InsertFileNameAtEnd()
Dim vPath As Variant
Dim sName As String
With ActiveDocument
If Len(.Path) = 0 Then .Save
vPath = Split(.FullName, "\")
sName = vPath(UBound(vPath) - 1)
sName = sName & "\" & Left(.name, InStrRev(.name, ".") - 1)
If InStr(1, .Range.Paragraphs.Last.Range.Text, _
"\") = False Then
.Range.InsertAfter vbCr & sName
With .Range.Paragraphs.Last.Range
.Paragraphs.Alignment = _
wdAlignParagraphRight
.Font.name = "Arial"
.Font.Size = 8
End With
Else
.Range.Paragraphs.Last.Range.Text = sName
End If
End With
End Sub

http://www.gmayor.com/installing_macro.htm


--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP

My web site www.gmayor.com

<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
G

Graham Mayor

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

<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 

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