我只是想问一下如何使用VBA获取实际打开的工作簿的名称和路径?
我在下面的代码片段中尝试了它,但不断收到编译错误。
我的文档类型为.docm
。
我做错了什么?
我的代码段:
Sub TestFileOpened()
Dim strPath As String, strPathAndName As String
strPath = Application.ThisWorkbook.Path
strPathAndName = strPath & Application.ThisWorkbook.Name
MsgBox strPathAndName
' Test to see if the file is open.
If IsFileOpen(strPathAndName) Then ....
答案 0 :(得分:2)
显然,您正在尝试使用Word中的一些Excel代码,因此首先需要保留Excel App:
Dim oExcel as Excel.Application
Dim wB as Excel.WorkBook
Dim strPath As String
Dim strPathAndName As String
On Error Resume Next
Set oExcel = GetObject(, "Excel.Application")
If Err.Number <> 0 Then
Set oExcel = CreateObject("Excel.Application")
End If
On Error GoTo 0
Set wB = oExcel.Workbooks(1)
strPath = wB.Path
strPathAndName = strPath & "\" & wB.Name
MsgBox strPathAndName
' Test to see if the file is open.
If IsFileOpen(strPathAndName) Then ....
答案 1 :(得分:1)
假设您想要打开单词文档的位置,请尝试以下操作:
Sub TestFileOpened()
Dim strPath As String, strPathAndName As String
strPath = ThisDocument.Path
strPathAndName = strPath & "\" & ThisDocument.Name
MsgBox strPathAndName
' Test to see if the file is open.
If IsFileOpen(strPathAndName) Then ....
(注意不要忘记在文件路径和名称之间添加反斜杠)