How do I get the current filename of a Word document, without the extension or full path, using a macro?

Viewed 54204

I have code that extracts the full path of a file, minus the extension, and I'm trying to modify it to only store the name of the file, once again without the extension.

Sub ShowFilename()

Dim pathName As String
With ActiveDocument
If Len(.Path) = 0 Then
.Save
End If
If Right(.Name, 1) = "x" Then
pathName = Left$(.FullName, (Len(.FullName) - 5))
Else
pathName = Left$(.FullName, (Len(.FullName) - 4))
End If
End With
MsgBox pathName

End Sub

This displays C:\Users\test, and the document's name is test.docm. How can I modify this to only display the filename? Do I need to split the string along \ and extract the last part?

6 Answers

This one works for me.

Sub ShowFilename()
MsgBox ActiveWindow.Parent
End Sub

An easy way would be:

Sub Test1()
Dim DocName As Document
Set DocName = ActiveDocument
end sub
Related