TheSwamp

Code Red => VB(A) => Topic started by: Vince on August 01, 2012, 10:45:53 AM

Title: Help With Setting Path
Post by: Vince on August 01, 2012, 10:45:53 AM
Hi Swamp Members,

I am trying to update a path in my VBA code. The current path is set as       Private Const fileX = "c:\Temp\MyFile.txt"     and I would like to change this to be the users Windows System Variable  %tmp%  .

Can anyone help me with this change....??  Your assistance would be appreciated.



Regards,
Vince





Title: Re: Help With Setting Path
Post by: BlackBox on August 01, 2012, 06:04:39 PM
Consider the Environ function; pseudo code (untested):

Code - vb.net: [Select]
  1. Private Const fileX = Environ("TMP") & "\" & "MyFile.txt"
  2.  
Title: Re: Help With Setting Path
Post by: 57gmc on August 01, 2012, 06:25:21 PM
You can just set your const to that string, but not all folder/path operations will evaluate that string to a valid path. So it depends on what you want to do with that const.
Title: Re: Help With Setting Path
Post by: BlackBox on August 01, 2012, 06:51:42 PM
Considering that this is presumably an internal application, where IT has deployed user profiles, it's not far fetched to use the TMP variable path.

If this is a concern, then use a simple FSO test for a valid path during initialization.
Title: Re: Help With Setting Path
Post by: Vince on August 02, 2012, 07:45:01 AM
Basically I would like to save the  MyFile.txt  to the user who is logged on  temp  folder   "C:\Users\Vincent Ferrara\AppData\Local\Temp".
Title: Re: Help With Setting Path
Post by: BlackBox on August 02, 2012, 09:49:16 AM
Basically I would like to save the  MyFile.txt  to the user who is logged on  temp  folder   "C:\Users\Vincent Ferrara\AppData\Local\Temp".

Was this post (http://www.theswamp.org/index.php?topic=42388.msg475629#msg475629) of any help?
Title: Re: Help With Setting Path
Post by: Vince on August 02, 2012, 12:32:17 PM
Hi RenderMan,

I get the following error when I tried your Suggestion:

Code: [Select]
Compile Error:

Method or Data Member Not Found


Regards,
Vince
Title: Re: Help With Setting Path
Post by: Keith™ on August 02, 2012, 12:48:20 PM
Well, it depends on how you put the information into your app If you copied the information above, you will probably get an error because VBA is trying to interpret MyFile as an object and .txt as a method or property. The filename must be in quotes as well.

Try this ...
Code: [Select]
Private Const fileX = Environ("TMP") & "\MyFile.txt"
Title: Re: Help With Setting Path
Post by: Cathy on August 02, 2012, 04:23:51 PM
I didn't think you could use a function like Environ() in a constant.  Wouldn't you need to dim fileX as a string instead? 
Title: Re: Help With Setting Path
Post by: Matt__W on August 02, 2012, 04:31:33 PM
This may be of some help.

http://en.kioskea.net/faq/951-vba-vb6-my-documents-environment-variables
Title: Re: Help With Setting Path
Post by: BlackBox on August 02, 2012, 04:33:09 PM
Not a VBA guy myself, I found Environ() in a Google search actually, so you may be correct.

Perhaps instead of declaring 'fileX' as a constant, it may be prudent to have a GetFilePath(FileName As String) Function, that is supplied the "MyFile.txt" file name, and Returns the "TMP" path & FileName string.

Just a thought.
Title: Re: Help With Setting Path
Post by: Keith™ on August 02, 2012, 07:04:05 PM
Ah, I didn't see it before .. the problem may be multiple things happening.

1) You cannot have a Private Const inside a declared function
2) You can't assign a value to a Private Const that uses the result of a function
3) You can't declare a variable and also assign a value to it in the same line in VBA like you can in VB6 and VB.Net

So ..
This works when used inside a function ...

Code: [Select]
    Dim FileX As String
    FileX = Environ("tmp") & "\MyFile.txt"