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:
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!
Dive into the highlighted features below or click here to explore the full power of Kutools for Word.
๐ Document Mastery: Split Pages / Merge Documents / Export Selection in Various Formats (PDF/TXT/DOC/XLSX) / 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 our 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 our Insert Group
๐ Precision Selections: Pinpoint specific pages / tables / shapes / heading paragraphs / Navigate with ease using our Select Group
โญ Star Enhancements: Navigate swiftly to any location / auto-insert repetitive text / seamlessly toggle between document windows / 11 Conversion Tools ...
Transform your Word tasks with Kutools. ๐ Download with 30-day trial Now ๐.
