To insert the file path and name or filename without extension in a Word document using VBA code, follow these steps:
- Open your Word document.
- Press
Alt + F11
to open the Visual Basic for Applications (VBA) editor. - Click on
Insert
in the menu and then chooseModule
to add a new module. - Copy and paste the following VBA code into the module:
Sub InsertFileNameAndPathWithoutExtension()
Dim xPathName As String
Dim xDotPos As IntegerWith Application.ActiveDocument
If Len(.Path) = 0 Then .Save
xDotPos = InStrRev(.FullName, “.”)
xPathName = Left(.FullName, xDotPos – 1)
End WithSelection.TypeText xPathName
End Sub - This code inserts the file path and name without extension at the cursor position.
- Close the VBA editor.
- To run the code, press
Alt + F8
, selectInsertFileNameAndPathWithoutExtension
, and clickRun
.
The file path and name without extension will be inserted at the current cursor position in your Word document.