Solved

relative path to file

Posted on 2009-05-20
3
484 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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
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…
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…

706 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

23 Experts available now in Live!

Get 1:1 Help Now