Log in

View Full Version : [SOLVED:] How to reach the desktop while avoiding "USERNAME"



RandomGerman
06-01-2017, 01:18 AM
Hi all,

when I had some trouble with a few macros, copying picutres from a hidden presentation, I learnt that


Environ$("USERNAME") & "\AppData\Roaming\Microsoft\AddIns\"

does not work on every machine, as the USERNAME seems to be not defined correctly anywhere. Or whatever the reason is.



Environ$("appdata") & "\Microsoft\AddIns\"

worked more successful on these machines.


But is there a way to reach the desktop, too?

I have one macro, creating a folder on the desktop and storing some things there - and it should be on the desktop, as the user needs to find it later. Until now, I used


Environ("USERPROFILE") & "\Desktop\Name_of_the_new_folder\"


Is there a way to avoid using "USERPROFILE" here, too?



Environ("desktop") & "\Name_of_the_new_folder\"

is not working. But maybe there is something like this? Does anyone know?


Thank you!
RG

Paul_Hossler
06-01-2017, 07:38 AM
See if this is more reliable for you



Option Explicit

Sub WheresMyDesktop()

Dim sDesktopPath As String

sDesktopPath = CreateObject("Shell.Application").Namespace(CVar(0)).Self.Path

MsgBox sDesktopPath

End Sub

John Wilson
06-01-2017, 09:22 AM
As well as Paul's method and USERPROFILE you can also say:


Sub findDT()
Dim oWSHShell As Object
Dim sDesktopPath As String
Set oWSHShell = CreateObject("WScript.Shell")
sDesktopPath = oWSHShell.SpecialFolders("Desktop")
MsgBox sDesktopPath
Set oWSHShell = Nothing
End Sub

RandomGerman
06-01-2017, 10:56 AM
Thank you, Paul and John,

in deed, both codes work great. Are there any arguments for prefering one of them? Maybe not compatible to PPT2007 or whatever ...

RandomGerman
06-05-2017, 04:12 AM
Obviously there are not. :-)

Once again, thank you!