jxlsp 发表于 2008-10-12 10:14:00

[求助]VBA怎么获取自己的路径?

VB可用App.Path可以获取自己的路径,请问VBA的代码是什么呢?

王咣生 发表于 2008-10-12 19:17:00

转贴:
'取得当前应用程序路径
Private Function GetDvbPath() As String
    On Error Resume Next
    Err.Clear
   
    Dim strFilePath As String
    Dim strFileName As String
   
    strFileName = VBE.ActiveVBProject.FileName
    If Err Then
      strFilePath = ""
    Else
      Dim pos As Integer
      pos = VBA.InStrRev(strFileName, "\\", -1, vbTextCompare)
      strFilePath = VBA.Left(strFileName, pos - 1)
    End If
    GetDvbPath = strFilePath
   
End Function

jxlsp 发表于 2008-10-13 08:54:00

非常感谢版主

robbin840311 发表于 2008-10-17 09:05:00

学习了。

wuyunpeng888 发表于 2008-10-26 14:35:00

我试了怎么不好用

lioney 发表于 2009-4-29 08:49:00

Private Function GetDvbPath() As String
    On Error Resume Next
    Err.Clear
   
    Dim strFilePath As String
    Dim strFileName As String
   
    strFileName = ThisDrawing.VBE.ActiveVBProject.FileName
    If Err Then
      strFilePath = ""
    Else
      Dim pos As Integer
      pos = VBA.InStrRev(strFileName, "\", -1, vbTextCompare)
      strFilePath = VBA.Left(strFileName, pos - 1)
    End If
    GetDvbPath = strFilePath
   
End Function
我作了点改动,在VBA里通过了测试。返回的路径类似:C:\aaa\bbb

lioney 发表于 2009-4-29 14:03:00

少写了一点,是ThisDrawing.Application.VBE.ActiveVBProject.FileName
页: [1]
查看完整版本: [求助]VBA怎么获取自己的路径?