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

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

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

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

<p>转贴:</p><p><font face="新宋体">'取得当前应用程序路径<br/>Private Function GetDvbPath() As String<br/>&nbsp; &nbsp; On Error Resume Next<br/>&nbsp; &nbsp; Err.Clear<br/>&nbsp; &nbsp; <br/>&nbsp; &nbsp; Dim strFilePath As String<br/>&nbsp; &nbsp; Dim strFileName As String<br/>&nbsp; &nbsp; <br/>&nbsp; &nbsp; strFileName = VBE.ActiveVBProject.FileName<br/>&nbsp; &nbsp; If Err Then<br/>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;strFilePath = ""<br/>&nbsp; &nbsp; Else<br/>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;Dim pos As Integer<br/>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;pos = VBA.InStrRev(strFileName, "\\", -1, vbTextCompare)<br/>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;strFilePath = VBA.Left(strFileName, pos - 1)<br/>&nbsp; &nbsp; End If<br/>&nbsp; &nbsp; GetDvbPath = strFilePath<br/>&nbsp; &nbsp; <br/>End Function</font></p>

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

<p><font face="新宋体">Private Function GetDvbPath() As String<br/>&nbsp; &nbsp; On Error Resume Next<br/>&nbsp; &nbsp; Err.Clear<br/>&nbsp; &nbsp; <br/>&nbsp; &nbsp; Dim strFilePath As String<br/>&nbsp; &nbsp; Dim strFileName As String<br/>&nbsp; &nbsp; <br/>&nbsp; &nbsp; strFileName = <font color="#ff0000">ThisDrawing</font>.VBE.ActiveVBProject.FileName<br/>&nbsp; &nbsp; If Err Then<br/>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;strFilePath = ""<br/>&nbsp; &nbsp; Else<br/>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;Dim pos As Integer<br/>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;pos = VBA.InStrRev(strFileName, "<font color="#ff0000">\</font>", -1, vbTextCompare)<br/>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;strFilePath = VBA.Left(strFileName, pos - 1)<br/>&nbsp; &nbsp; End If<br/>&nbsp; &nbsp; GetDvbPath = strFilePath<br/>&nbsp; &nbsp; <br/>End Function</font></p><p><font face="新宋体">我作了点改动,在VBA里通过了测试。返回的路径类似:C:\aaa\bbb</font></p>

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

少写了一点,是<font face="新宋体"><font color="#ff0000">ThisDrawing</font>.Application.VBE.ActiveVBProject.FileName</font><br/>
页: [1]
查看完整版本: [求助]VBA怎么获取自己的路径?