Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

relative path to file

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

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

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…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
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…
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

705 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