我有代码提取文件的完整路径,减去扩展名,我试图修改它只存储文件的名称,再次没有扩展名.
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
Run Code Online (Sandbox Code Playgroud)
显示C:\Users\test,文档名称为test.docm.如何将其修改为仅显示文件名?我是否需要将字符串拆分\并提取最后一部分?
Sub ShowFilename()
Dim pathName As String
Dim o As Document
Set o = ActiveDocument
If InStrRev(o.Name, ".") <> 0 Then
MsgBox Left(o.Name, InStrRev(o.Name, ".") - 1)
Else
MsgBox o.Name
End If
End Sub
Run Code Online (Sandbox Code Playgroud)
我最初发布的没有if,如果文件从未保存过,或者没有扩展名,则会出错.
FSO为这类事物提供了一组方法,其中一个方法是"getBaseName"
Msgbox CreateObject("scripting.filesystemobject").getbasename(o.Name)
Run Code Online (Sandbox Code Playgroud)
http://msdn.microsoft.com/en-us/library/xhxzwwe1(v=vs.84).aspx