vba 如何使用宏获取 Word 文档的当前文件名,不带扩展名或完整路径?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11564857/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I get the current filename of a Word document, without the extension or full path, using a macro?
提问by Ricardo Altamirano
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?
这将显示C:\Users\test
,并且文档的名称是test.docm
。如何修改它以仅显示文件名?我是否需要拆分字符串\
并提取最后一部分?
采纳答案by Tim Williams
FSO has a set of methods for this type of thing, one of which is "getBaseName"
FSO 有一套针对这种类型的方法,其中一个是“getBaseName”
Msgbox CreateObject("scripting.filesystemobject").getbasename(o.Name)
http://msdn.microsoft.com/en-us/library/xhxzwwe1(v=vs.84).aspx
http://msdn.microsoft.com/en-us/library/xhxzwwe1(v=vs.84).aspx
回答by Daniel
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
I initially posted this without the if, which would error if the file had never been saved, or had no extension.
我最初发布时没有使用 if,如果文件从未保存过或没有扩展名,则会出错。
回答by Mad VBA Coder
As I was not able write code using FSO (isn't it only for VB, is it?), I wrote this one, quite self explanatory :)
由于我无法使用 FSO 编写代码(它不是仅适用于 VB,是吗?),我写了这个,非常不言自明:)
Dim oldFilename As String
oldFilename = ActiveDocument.Name
If Right(oldFilename, 5) = ".docx" Then
MsgBox ("subtract .docx")
oldFilename = Left(oldFilename, Len(oldFilename) - 5)
ElseIf Right(oldFilename, 4) = ".doc" Then
MsgBox ("subtract .doc")
oldFilename = Left(oldFilename, Len(oldFilename) - 4)
Else
MsgBox ("no extension yet")
End If
回答by Ben Segar
Yeish, I wouldn't do it like that!
是的,我不会那样做!
Hypothetically, you have a whole folders worth of word and you don't need the extensions, you just want the names. What you would do is go through the word docs and parse them through this function with the type of extension you want removed from the file name
假设,你有一个完整的文件夹,你不需要扩展名,你只需要名字。您要做的是浏览单词 docs 并通过此函数解析它们,并使用您要从文件名中删除的扩展名类型
Function removeExtension(myDoc as Document, extension as String)
Dim documentWithoutExtension as String
documentWithoutExtension = replace(myDoc.Name, extension, "")
removeExtension = documentWithoutExtension
End Function
回答by Shane Sibbett
This one works for me.
这个对我有用。
Sub ShowFilename()
MsgBox ActiveWindow.Parent
End Sub
回答by MWCA44
An easy way would be:
一个简单的方法是:
Sub Test1()
Dim DocName As Document
Set DocName = ActiveDocument
end sub