注意:我對查找工作表的路徑並不感興趣,我打算將該工作表的路徑寫入位於與.OTM文件相同的文件夾中的文本文件中。 我需要將此代碼從硬編碼路徑轉換爲從位於與宏相同文件夾中的文本文件中讀取的路徑。如何獲得使用宏(vba代碼)本身的宏的路徑?
如何獲得使用宏(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文件的屬性。首先谷歌響應:https://stackoverflow.com/questions/2813925/how-to-get-the-path-of-current-worksheet-in-vba 2.更改'strPath'指的是,而不是 –
可能的重複[如何獲取VBA中當前工作表的路徑?](https://stackoverflow.com/questions/2813925/how-to-get-the-path-of-current-worksheet-in-vba) – FatTony
我是對找到工作簿的路徑不感興趣,我很想找到宏的路徑,所以我知道從哪裏讀取我的文本文件。 – NAlexP