PDA

View Full Version : Calling a File in VBA




polevault139
Oct 28, 2007, 01:24 PM
For a class I have to write a program in VBA which involves calling a file. I know the syntax to call the file but I am not sure of the directory. So here is what I have.

Open ("I don't know the directory") For Input As 1

This is probably a really easy question and I am just being oblivious to the answer. So can you please help me with this question.



polevault139
Oct 30, 2007, 01:29 PM
Well after searching the internet again nothing has come up so could you guys please help or at least say that you don't have any idea. I have Leopard so I don't know if that will make a difference but I don't think so

Anyways here is an updated file path I have tried with the same result

Open ("Macintosh HD/Users/kevinsevers/Documents/Engineering 160/Programming/LeastSqrs.dat") For Input As 1

When I follow my path it goes right to the file so is there just some syntax I am missing?

I get the error Runtime Error : 53 File not Found

vanmacguy
Oct 30, 2007, 01:32 PM
I don't really have the answer apart from reminding you that OSX is case sensitive?

Sorry, that's the best I can come up with.

Cheers.

polevault139
Oct 30, 2007, 01:36 PM
Thanks but I double checked all of the grammar and spelling and it all looks good. I've tried with or without spaces, with colons instead of forward slashes, with back slashes. Im just running out of ideas.

therevolution
Oct 30, 2007, 01:37 PM
Try to keep in mind: you're asking for Windows programming help on a Mac forum. ;)

I don't know VB, but I will take a stab. Try putting a slash before "Macintosh HD" like so:

Open("/Macintosh HD/<etc>")

By the way, how are you running this on Leopard? Mono?

polevault139
Oct 30, 2007, 01:43 PM
Thanks for your help and I know I was kind of taking a shot in the dark asking for help with VBA on a Mac Forum but thats what we have to learn I want to learn Java but don't know where to start.

I just figured out my problem, I was reading an article about calling a file with OS X and they said the thing about using colon's so I tried it one more time at it finally worked, I think. I haven't actually finished writing the program but at least it didn't give me the file not found error so I think that solved it. Here is what I have now.

Open ("Macintosh HD:Users:myname: Documents:Engineering160:Programming:LeastSqrs.dat") for Input as 1

So that seems to work. Thanks for all of the suggestions though, it got me thinking of different ways to try it.

Note: There isnt a space after the colon before Documents but if I didnt do that the simely face would have been there

vanmacguy
Oct 30, 2007, 02:21 PM
Good job on figuring it out.

It's always helpful to bounce stuff off others when you're stuck.

Cheers.

Elkef
Jul 1, 2011, 08:48 AM
I have another little problem...

Macintosh HD:Users:elkef:Documents:GG_Test1.docx

This works perfectly... But if I change my harddisk's name, it no longer works...
Anyone knows of a / or ~ alternative?

Thom_Edwards
Jul 1, 2011, 09:16 AM
But if I change my harddisk's name, it no longer works
It's been a very long time since I've messed with this kind of thing so I may be completely mistaken, but I seem to remember "startup disk" (or something like that) being used to represent your primary hard drive. Worth a shot, maybe.

EDIT: After some curious googling, I found a page that mentions what I was thinking of. http://www.realgeek.com/forums/vba-file-path-hitch-447214.html. The post is from 2006, so it may be outdated, but it's worth a try I would think.

Elkef
Jul 1, 2011, 10:29 AM
Thanks!

This still works!
Const sAScript As String = "Tell application ""Finder""" & _
vbNewLine & "return name of startup disk" & vbNewLine & _
"end tell"
Const sPath As String = ":System Folder:Preferences:"
Dim sVolumeName As String
sVolumeName = MacScript(sAScript) & sPath

This is how I get the users shortname and homedir location

ze_path = ze_path + MacScript("(do shell script ""cd ~; pwd | tr '/' ':' "")") + "Documents:"


where tr changes / into :

Together with your hint it works now...

chown33
Jul 1, 2011, 12:01 PM
The AppleScript to get the current user's home folder is this:
get path to home folder

If you copy and paste that into AppleScript Editor and run it, you'll see the current complete path beginning with the startup disk.

The path of the startup disk is this:
get path to startup disk


If you want a Posix path for any of those:
get Posix path of (path to home folder)
get Posix path of (path to startup disk)

Elkef
Jul 2, 2011, 01:49 PM
The AppleScript to get the current user's home folder is this:
get path to home folder

If you copy and paste that into AppleScript Editor and run it, you'll see the current complete path beginning with the startup disk.

The path of the startup disk is this:
get path to startup disk


If you want a Posix path for any of those:
get Posix path of (path to home folder)
get Posix path of (path to startup disk)



Thank you very much, that works... Only now I have an ALIAS in front of the path... Is there a way to eradicate that in Applescript or do I need to do that with a little VBA substring?

chown33
Jul 2, 2011, 02:28 PM
Thank you very much, that works... Only now I have an ALIAS in front of the path... Is there a way to eradicate that in Applescript or do I need to do that with a little VBA substring?

get path to startup disk as string

get path to home folder as string

get path to documents folder as string

More "path to xyzzy" folder identifiers:
http://face.centosprime.com/macosxw/applescript-path-to/

Elkef
Jul 2, 2011, 04:56 PM
Thank y'all!