如何使用宏获取没有扩展名或完整路径的Word文档的当前文件名? [英] How do I get the current filename of a Word document, without the extension or full path, using a macro?

查看:351
本文介绍了如何使用宏获取没有扩展名或完整路径的Word文档的当前文件名?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些提取文件完整路径(减去扩展名)的代码,我试图将其修改为仅存储文件名,而没有扩展名.

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

这将显示C:\Users\test,文档的名称为test.docm.如何修改它以仅显示文件名?我是否需要沿\拆分字符串并提取最后一部分?

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?

推荐答案

FSO为此类型的事物提供了一组方法,其中之一是"getBaseName"

FSO has a set of methods for this type of thing, one of which is "getBaseName"

Msgbox CreateObject("scripting.filesystemobject").getbasename(o.Name)

http://msdn.microsoft.com /en-us/library/xhxzwwe1(v=vs.84).aspx

这篇关于如何使用宏获取没有扩展名或完整路径的Word文档的当前文件名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆