注意:我对找到工作表的路径不感兴趣,我打算在与.OTM文件位于同一文件夹的文本文件中写入工作表的路径。 我需要将此代码从硬编码路径转换为从与宏位于同一文件夹中的文本文件中读取的路径。
如何使用宏(vba代码)本身获取宏的路径?
Public xlApp As Object
Public xlWB As Object
Public xlSheet As Object
Sub OpenXl()
Dim enviro As String
Dim strPath As String
enviro = CStr(Environ("USERPROFILE"))
'the path of the workbook
strPath = enviro & "\Documents\test2.xlsx"
On Error Resume Next
Set xlApp = GetObject(, "Excel.Application")
If Err <> 0 Then
Application.StatusBar = "Please wait while Excel source is opened ... "
Set xlApp = CreateObject("Excel.Application")
bXStarted = True
End If
On Error GoTo 0
'Open the workbook to input the data
Set xlWB = xlApp.Workbooks.Open(strPath)
Set xlSheet = xlWB.Sheets("Sheet1")
' Process the message record
On Error Resume Next
End Sub
答案 0 :(得分:1)
OTM文件存储在我的电脑上(Windows 7 / Outlook 2010):
strPath = Environ("userprofile") & "\AppData\Roaming\Microsoft\Outlook\"
答案 1 :(得分:0)
只需使用:
ThisWorkbook.Path
这将返回包含代码的工作簿的路径。