how to write path in vba?

5444
4
01-04-2011 05:43 PM
yosiwidiyanto
New Contributor
Dear All vba programmer
I have data (.shp, .txt) dan mxd file that store in directory D, I want my application can be run in every directory without changing data address in code. Below is my code without using path

Private Sub UserForm_Initialize()
Dim strFile1 As String
    strFile1 = "D:\APLIKASI SIL\Data Teks\LandUnit.txt"
Dim strLandUnit As String 
Open strFile1 For Input As #1
Do Until EOF(1)
    Input #1, strLandUnit
    cboSatuanLahan.AddItem strLandUnit
Loop
Close #1

Dim strFile2 As String
    strFile2 = "D:\APLIKASI SIL\Data Teks\Kabupaten.txt"
Dim strKabupaten As String
Open strFile2 For Input As #2
Do Until EOF(2)
    Input #2, strKabupaten
    cboKabupaten.AddItem strKabupaten
Loop
Close #2

End Sub

I have tried this way to change:

strFile1 = "D:\APLIKASI SIL\Data Teks\LandUnit.txt"
to
strFile1 = App.Path & "\APLIKASI SIL\Data Teks\LandUnit.txt"

and

strFile2 = "D:\APLIKASI SIL\Data Teks\Kabupaten.txt"
to
strFile2 = App.Path & "\APLIKASI SIL\Data Teks\Kabupaten.txt"

But .Path is unknown in vba

I doo need someone help to solve this problem.
Thanks a lot

Yosie
0 Kudos
4 Replies
HarishDave
New Contributor II
VBA code given below provides AppPath.

Dim AppPath As String
AppPath = Left(Application.Templates.Item(Application.Templa tes.Count - 1), Len(Application.Templates.Item(Application.Templat es.Count - 1)) - Len(Application.Document.Title))

MsgBox AppPath
0 Kudos
yosiwidiyanto
New Contributor
i have paste the code in userform initialize, but the code is error. one of them Len is unknown. what should i do?
thanks
0 Kudos
HarishDave
New Contributor II
Just delete the space in the word "Templa tes". Or copy the code given below.

Dim AppPath As String
AppPath = Left(Application.Templates.Item(Application.Templates.Count - 1), Len(Application.Templates.Item(Application.Templates.Count - 1)) - Len(Application.Document.Title))

MsgBox AppPath
0 Kudos
yosiwidiyanto
New Contributor
No more error, but when I delete
1. strFile1 = "D:\APLIKASI SIL\Data Teks\LandUnit.txt"
2. strFile2 = "D:\APLIKASI SIL\Data Teks\Kabupaten.txt"
3. Dim pFeatWorkspace As IFeatureWorkspace
    Set pFeatWorkspace = pWorkspaceFact.OpenFromFile("D:\APLIKASI SIL\Shp", 0)
    Dim pFeatClass As IFeatureClass
    Set pFeatClass = pFeatWorkspace.OpenFeatureClass("Batas-Kab-Bna-ABesar")
error comes (Path/file access error)
What I want is after use your given code, the pathnames above doesn't need anymore.
How to do that?
Thanks
0 Kudos