Solved

relative path to file

Posted on 2009-05-20
3
487 Views
Last Modified: 2012-05-07
I'm trying to access a file, but can't do it with a relative path.
If I use an absolute path, e.g.
excelFileSource = "C:\\My Documents\\myDirectory\\myFile.xls"
then it works, but I need to do it with a relative path.
How can I accomplish this?

Dim excelFileSource As String

excelFileSource = "myDirectory\\myFile.xls"
 

Set excelApplication = GetObject(, "Excel.Application")

If Err = 429 Then

    Set excelApplication = CreateObject("Excel.Application")

End If
 

Set excelWorkbookSource = excelApplication.Workbooks.Open(excelFileSource)

Open in new window

0
Comment
Question by:allelopath
3 Comments
 
LVL 1

Accepted Solution

by:
mcfitz13 earned 250 total points
ID: 24435040
depends on what you are using the vb in but in access 2000 or greater  try using CurrentProject.FullName and that should give you the full path the file then you would just use that string to get the current path and then use it as a variable.

or the curdir works for the drive.

0
 
LVL 76

Assisted Solution

by:David Lee
David Lee earned 250 total points
ID: 24435181
Hi, allelopath.

A relative path is relative to your current location on disk.  Based on the code you posted it's impossible to tell what the current path is.  Unless you launched the VB app from "C:\My Documents", then you can't use a relative path like the one you gave.  If you did launch the VB app from My Documents, then something like this should work

excelFileSource = App.Path & "\myDirectory\myFile.xls"
0
 
LVL 1

Author Closing Comment

by:allelopath
ID: 31583596
I ended up using ThisWorkbook.path
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now