4

In MS Word you can insert a field code to show the document file name with the option to include or not the full path.

What I want is to only insert the folder path excluding the file name.

Macros are not an option.

Can this be done?

Is there an alternate field code?

Is there some magical undocumented switch on the FILENAME fieldcode? (Microsoft have been known to do this before!)

Judith
  • 673
  • 5
  • 18
Shevek
  • 16,738

2 Answers2

3

Unfortunately, there is no way to do this with the FILENAME field without a macro, the FILENAME field doesn't offer an option to omit the name of the file.

Anyway, here is a macro to insert the path of the file, without the file name, for those who might need it.

Sub InsertPath()
  Dim sPath As String

  sPath = ActiveDocument.Path
  If sPath = "" Then
    MsgBox "You need to save the document before running this macro.", _
           vbOKOnly, "Document not saved"
  Else
    sPath = sPath & Application.PathSeparator
    Selection.TypeText(sPath)
  EndIf
EndSub
Jawa
  • 3,679
3

I'm still playing around with this as it seems like a reasonable enough thing to expect to be able to do... but so far the only way I've found is by the rather nasty cheat of insering a filename field with path and then applying a white font colour to the text (i.e. the filename itself) and so you end up with just the path and a block of invisible text after it.

Problem is that this could mess with formatting and is really badly kludgy.

-=EDIT=-

Slightly less kludgy solution...

If you don't mind having a macro in your normal.dotm and having a hidden variable in your document then you may be able to use this macro and set a button on your quick access to run it... I've tried it and it works on a macro free document so the document itself does not need macros to use this method, I'd put this in my normal.dotm template...

Sub updatePath()
'
' updatePath Macro
'
'
Dim myPath As String
myPath = ActiveDocument.Path
If myPath = "" Then
    'do nothing as the document has no path... needs to be saved first
Else
    If ActiveDocument.Variables.Count = 0 Then
        ActiveDocument.Variables.Add Name:="myPath", Value:=myPath
    Else
        i = 1
        Do While i < (ActiveDocument.Variables.Count + 1)
            If ActiveDocument.Variables.Item(i).Name = "myPath" Then
                ActiveDocument.Variables.Item(i).Value = myPath
            End If
            i = i + 1
        Loop
    End If
End If

End Sub

And then just add a field code

DOCVARIABLE myPath

which when updated after running the macro above would do exactly what you wanted. Granted it is two or three clicks rather than just a simple update, but it does mean that your exported document gets the proper field type and only people with this macro get to say where the document should be stored :)

Mokubai
  • 95,412