Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

relative path to file

Posted on 2009-05-20
3
Medium Priority
?
494 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 1000 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 1000 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
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…
Suggested Courses

916 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