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?
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
Recommended Productivity Tools
- Complicated and repeated operations can be done a one-time processing in seconds.
- Insert multiple images across folders into Word document at once.
- Merge and combine multiple Word files across folders in to one with your desired order.
- Split the current document into separate documents according to heading 1, 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...