Excel and Office RAQ

Inserting a truncated filename at the end of a Word document (without full path)


Tactical solutions

Research projects

Microsoft Office

Multiple choice guessing


Knowledge measurement audit

Item bank services

Item design course








Technical articles

Excel and Office RAQ

Free Excel Add-in

Free PowerPoint Add-in


Home page

Q: Employees here are in the habit of inserting a { FILENAME \p } at the end of their Word docs, then removing most of the path and the file extension. They only want the bottom-line FOLDER NAME and the FILE NAME.

So, rather than having them insert this field, I want to put a button on their toolbar that will run a macro that will insert the FOLDER NAME\FILE NAME.

A: Here's one way to do it. This code makes a note of where they are in the document, goes to the end, puts in the truncated path and filename (surrounded by line feeds), then returns the user to where they were. I'm assuming you know how to assign this to a toolbar button.

  Sub TruncPath()

    Dim strFile As String
    Dim strPath As String
    Dim intLen As Integer
    Dim intNumSlash As Integer
    Dim strShortPath As String
    Dim strFinal As String
    Dim rngPlaceHolder As Range

    strFile = Left(ActiveDocument.Name, Len(ActiveDocument.Name) - 4)
    strPath = ActiveDocument.Path
    intLen = Len(strPath)
    intNumSlash = InStrRev(strPath, "\")
    strShortPath = Right(strPath, intLen - intNumSlash)
    strFinal = strShortPath & "\" & strFile

    Set rngPlaceHolder = Selection.Range
    Selection.EndKey Unit:=wdStory
    Selection.TypeText Chr(13) & strFinal & Chr(13)

  End Sub


Back to other RAQ topics