Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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!)

share|improve this question
    
I've edited my answer to add a possible solution... are macros not an option in the output file or at all? I think you have to use a macro somewhere but my solution would mean that the file that gets saved is macro free while the person creating the document has a macro, is that an acceptable solution? –  Mokubai Apr 20 '10 at 9:44
    
Macros are not allowed - corporate policy –  Shevek Apr 20 '10 at 12:00
    
I've spent a while looking into what can be done with the fieldcodes in Word and there is nothing like as much versatility to even think about doing it without using a macro or some other intelligent programming language. From what Googling I've done I can't find any switches or ways to do this that don't involve a macro. Given some time I could probably write a bit of Python code that traversed a directory full of files and put the correct path in the footer, but it would never be fully automated in Word. If someone finds the way I'd be curious... –  Mokubai Apr 20 '10 at 14:06
    
Corporate policy? That must really hinder productivity! –  marcusw Apr 22 '10 at 13:12

2 Answers 2

up vote 2 down vote accepted
+100

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 :)

share|improve this answer
    
I like the thinking, but agree it is kludgy! –  Shevek Apr 20 '10 at 9:31

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
share|improve this answer
    
I really can't believe there is no built in field code for this, FILEPATH for example or even a further switch on FILENAME. Bad on MS –  Shevek Apr 20 '10 at 9:32

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.