Solved

relative path to file

Posted on 2009-05-20
3
491 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
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…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses
Course of the Month5 days, 19 hours left to enroll

626 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