Skip to main content

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

Author: Xiaoyang Last Modified: 2018-08-31

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

Best Office Productivity Tools

Kutools for Word - Elevate Your Word Experience with Over 100 Remarkable Features!

🤖 Kutools AI Assistant: Transform your writing with AI - Generate Content  /  Rewrite Text  /  Summarize Documents  /  Get Quick Answers, all within Word

📘 Document Mastery: Split Pages  /  Merge Documents  /  Export Selection in Various Formats (PDF/TXT/DOC/HTML...)  /  Batch Convert to PDF  /  Export Pages as Images  /  Print Multiple Files at once...

Contents Editing: Batch Find and Replace across Multiple Files  /  Resize All Pictures  /  Transpose Table Rows and Columns  /  Convert Table to Text...

🧹 Effortless Clean: Sweap away Extra Spaces  /  Section Breaks  /  All Headers  /  Text Boxes  /  Hyperlinks  /  For more removing tools, head to the Remove group...

Creative Inserts: Insert Thousand Separators  /  Check Boxes  /  Radio Buttons  /  QR Code  /  Barcode  /  Diagonal Line Table  /  Equation Caption  /  Image Caption  /  Table Caption  /  Multiple Pictures  /  Discover more in the Insert group...

🔍 Precision Selections: Pinpoint Specific Pages  /  Tables  /  Shapes  /  Heading Paragraphs  /  Enhance navigation with more Select features...

Star Enhancements: Navigate to Any Location  /  Auto-Insert Repetitive Text  /  Toggle Between Document Windows  /  11 Conversion Tools...

👉 Want to try these features? Download Kutools for Word today and experience the difference! 🚀
Free Download     Read More     Buy Now
 
Comments (0)
No ratings yet. Be the first to rate!
There are no comments posted here yet
Leave your comments
Posting as Guest
×
Rate this post:
0   Characters
Suggested Locations