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

To insert the file path and name or filename without extension in a Word document using VBA code, follow these steps:

  1. Open your Word document.
  2. Press Alt + F11 to open the Visual Basic for Applications (VBA) editor.
  3. Click on Insert in the menu and then choose Module to add a new module.
  4. Copy and paste the following VBA code into the module:
    Sub InsertFileNameAndPathWithoutExtension()
    Dim xPathName As String
    Dim xDotPos As Integer

    With Application.ActiveDocument
    If Len(.Path) = 0 Then .Save
    xDotPos = InStrRev(.FullName, “.”)
    xPathName = Left(.FullName, xDotPos – 1)
    End With

    Selection.TypeText xPathName
    End Sub

  5. This code inserts the file path and name without extension at the cursor position.
  6. Close the VBA editor.
  7. To run the code, press Alt + F8, select InsertFileNameAndPathWithoutExtension, and click Run.
How to insert file path and filename without extension in Word document

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

The file path and name without extension will be inserted at the current cursor position in your Word document.

Gotkey.net
Logo
Compare items
  • Total (0)
Compare
0
Shopping cart