【发布时间】:2020-06-23 15:34:55
【问题描述】:
注意:我对查找工作表的路径不感兴趣,我打算将工作表的路径写入与 .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
【问题讨论】:
-
1.找到告诉您是 excel 文件的属性。第一个谷歌回复:stackoverflow.com/questions/2813925/… 2. 更改
strPath以引用它 -
我对查找工作簿的路径不感兴趣,我对查找宏的路径感兴趣,所以我知道从哪里读取我的文本文件。