Replacement for Environ$("Temp")

Oorang

Well-known Member
Joined
Mar 4, 2005
Messages
2,071
Since MS introduced sandboxmode I have been loathe to use the Environ method. However now I find myself needing to get the temp folder. I have considered searching for the most likely paths and then using the first one I find. But I was wondering if there is a way to get the temporay folder without having to go that route (solution needs to be useable in sandboxmode) Thoughts?
 

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
Uh nevermind... I found the solution like two seconds after I posted this:
Use the Microsoft Scripting Runtime
Code:
Dim fso As Scripting.FileSystemObject
Dim strPath as String
Set fso = CreateObject("Scripting.FileSystemObject")
strPath = fso.BuildPath(fso.GetSpecialFolder(TemporaryFolder), fso.GetTempName)
 
Upvote 0
Oorang,

Thanks for the question AND the answer! :biggrin:

I hate sandbox mode -- one of my favourites was Environ("username"), now I am having to find other ways of doing the same thing... fortunately they are out there, but usually involve a bit of hassle to track down :devilish:

Speaking of which, I wonder when MS will deny us access to the File Scripting Object and Shell commands?

Denis
 
Upvote 0
I didn't realize it but as I was reading more in-depth into the topic I could have just used this:
Code:
Function CEnviron(str as string) as string
CEnviron = Environ(str)
End Function
Apparently environ is only blocked in SQL and Control Default properties, not VBA.
 
Upvote 0
Environ$("username") still returns the name of the User. I use this as audit for many databases:

Private Sub Form_BeforeUpdate(Cancel As Integer)
'On the changing of The Description of the error refresh who has updated record and when
Forms![Frm_Input].[Last Changed] = Now
Forms![Frm_Input].[Last Changed by] = Environ$("username")
End Sub

Regards,

Alain
 
Upvote 0
Correct. You can still use it in VBA, just not as a control's default value or in an on-event property as a formula. Your code works because it calls a VBA routine. Had you tried to plug in =Environ$("Username") instead it would return "#Name?". (You could have done that in the past to spare yourself code). You also cannot access that funciton via SQL when sandboxmode is turned on.
As I understand it, the whole purpose of sandboxmode is not to protect from malicious vba, but to deter SQL injection attacks.
 
Upvote 0

Forum statistics

Threads
1,222,039
Messages
6,163,552
Members
451,843
Latest member
vitto

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top