2017-01-17 27 views
1

我只想问我如何获得名称路径的实际打开Woorkbook与VBA?获取使用VBA打开的Woorkbook的名称和路径

我想它在下面的代码段拍摄的,但总是收到编译错误。

我的文档类型为.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 .... 
+1

'ThisWorkBook'是Excel对象!你在Word中工作吗? – user3598756

+0

谢谢你,那是我的错! –

回答

1

假设你想要打开word文档的位置,请尝试以下操作:

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 .... 

(注意不要忘记添加后台文件路径和名称)h之间

+0

谢谢你! –

+0

我有另外一个问题,也许你能帮助我.... 我想使用此代码与Mac,但如果我只能更换“\”和“:”有一个错误。 我在做什么错? –

+0

@ T.Els我不上的MAC经常VBA程序,但你可以在这里看到:http://stackoverflow.com/questions/10045474/dir-function-not-working-in-mac-excel-2011-vba – User632716

2

显然,你试图使用从Word一些Excel中的代码,所以首先你需要获得Excel的应用程序保持:

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 .... 
相关问题