Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to save to current User\Desktop location

Tags:

excel

vba

So I am trying to save an excel worksheet as a .PRN file to the current users desktop. I thought maybe there was a standard "wildcard" operator that I could use, but I don't see that anywhere. I have looked through stack overflow and I found information that is probably what I am looking for, but due to me not being a programmer and not very good with excel, I don't know if it is what I want and if it is, where to put it in my code.

This is what I am currently running that only works on my machine:

Sub Save_PRN()
Dim fileName As String
fileName = "C:\Users\cameron\Desktop\PRN Test files\" & Range("'Customer_Info'!R2").Text & ".prn"
ActiveWorkbook.SaveAs fileName:=fileName, FileFormat:=xlTextPrinter, Password:="", WriteResPassword:="", ReadOnlyRecommended:=False, CreateBackup:=False
End Sub

While it does what I want, when I upload it to our share, clearly another user won't be able to use it. I saw on a question someone answered stating to use this:

MsgBox CreateObject("WScript.Shell").specialfolders("Desktop")

But I don't know or understand how to incorporate that into my macro above. Any help or advice would be great. Am I even on the right track?

like image 598
Cameron Avatar asked Jan 09 '23 03:01

Cameron


2 Answers

While roryap's solution is perfectly fine and working, I would like to add another working solution.

Dim strPath as String
strFileName = Environ("USERPROFILE") & "\Desktop\PRN Test files\" & Range("'Customer_Info'!R2").Text & ".prn"
ActiveWorkbook.SaveAs fileName:=strFileName, FileFormat:=xlTextPrinter, Password:="", WriteResPassword:="", ReadOnlyRecommended:=False, CreateBackup:=False
like image 77
Ralph Avatar answered Jan 10 '23 17:01

Ralph


You can use it like this:

Dim desktopFolderPath As String
desktopFolderPath = CreateObject("WScript.Shell").specialfolders("Desktop")
fileName = desktopFolderPath & "\PRN Test files\" & Range("'Customer_Info'!R2").Text & ".prn"
like image 35
rory.ap Avatar answered Jan 10 '23 18:01

rory.ap