Tip: Other languages are Google-Translated. You can visit the English version of this link.
Log in
x
or
x
x
Register
x

or

 How to insert file path and filename without extension in Word document?

Normally, we can insert the filename or file path and name in the Word document by using the Filename field function, but, this inserted filename including the file extension docx or doc. How could you insert the file name or file path and name without extension?

Insert file path and name or filename without extension by using VBA code


Insert file path and name or filename without extension by using VBA code

To insert the filename without extension in Word document, please apply the following VBA code:

1. Put the cursor to a place where you want to insert the file path and name without extension.

2. Then, hold down the ALT + F11 keys to open the Microsoft Visual Basic for Applications window.

3. And then, click Insert > Module, copy and paste below code into the opened blank module.

VBA code: Insert file path and name without extension:

Sub InsertFileNameAndPath()
    Dim xPathName As String
    Dim xDotPos As Integer
    With Application.ActiveDocument
        If Len(.Path) = 0 Then .Save
        xDotPos = VBA.InStrRev(.FullName, ".")
        xPathName = VBA.Left(.FullName, xDotPos - 1)
    End With
    Application.Selection.TypeText xPathName
End Sub

4. Then press F5 key to run this code and the file path and name has been inserted into the document without extension, see screenshot:

doc insert filename without extension 1

Note: If you just want to insert the file name without extension into the document, you should apply this code:

VBA code: Insert file name only without extension:

Sub InsertFileNameOnly()
    Dim xPathName As String
    Dim xDotPos As Integer
    With Application.ActiveDocument
        If Len(.Path) = 0 Then .Save
        xDotPos = VBA.InStrRev(.Name, ".")
        xPathName = VBA.Left(.Name, xDotPos - 1)
    End With
    Application.Selection.TypeText xPathName
End Sub



Recommended Word Productivity Tools

 

Kutools For Word - More Than 100 Advanced Features For Word, Save Your 50% Time

  • Complicated and repeated operations can be done one-time processing in seconds.
  • Insert multiple images across folders into Word document at once.
  • Merge and combine multiple Word files across folders into one with your desired order.
  • Split the current document into separate documents according to heading, section break or other criteria.
  • Convert files between Doc and Docx, Docx and PDF, collection of tools for common conversions and selection, and so on...
Say something here...
symbols left.
You are guest ( Sign Up? )
or post as a guest, but your post won't be published automatically.
Loading comment... The comment will be refreshed after 00:00.

Be the first to comment.