Как узнать имя книги Excel и адрес расположения её с помощью VBA?
Формулировка задачи:
У меня открыта книга Excel, хочу запустить макрос, который вывел название Файла и адрес расположения. Подскажите как это сделать?
Решение задачи: «Как узнать имя книги Excel и адрес расположения её с помощью VBA?»
textual
Листинг программы
Dim Path As String, Imea As String
Call FileNameTest(ThisWorkbook.FullName, Path, Imea)
MsgBox Path
MsgBox Imea
End Sub
Sub FileNameTest(PathFile$, Path$, File$)
'
' Âõîä: PathFile$ - ïîëíîå èìÿ ôàéëà
' Âûõîä: Path$ - èìÿ êàòàëîãà
' File$ - èìÿ ôàéëà
' Íàïðèìåð:
' Âõîä: PathFile$ = "d:\Test1\Test2\File.txt"
' Âûõîä: Path$ = "d:\Test1\Test2\"
' File$ = "File.txt"
''''''''''''''''''''''''''''''''''''''''''''''
Dim n As Long
n = InStrRev(PathFile$, "\")
If n <= 0 Then
Path$ = "": File$ = PathFile$
Else
Path$ = Left$(PathFile$, n): File$ = Mid$(PathFile$, n + 1)
End If
End Sub
Function InStrRev(ByVal pStr As String, pItem As String) As Integer
'*******************************************
'Purpose: Return location of last instance of a character or phrase.
'Inputs: ? InStrRev("the quick brown fox jumped the lazy dog", "the")
'Output: 28 - Location of last occurence of "the"
'*******************************************
Dim i As Integer, n As Integer, tLen As Integer
n = 0
tLen = Len(pItem)
For i = Len(RTrim(pStr)) To 1 Step -1
If Mid(pStr, i, tLen) = pItem Then
n = i
Exit For
End If
Next i
InStrRev = n
End Function