如何保存到当前用户\桌面位置

所以我试图保存一个.PRN文件到当前用户桌面的Excel工作表。 我想也许有一个标准的“通配符”运算符,我可以使用,但我没有看到任何地方。 我已经通过堆栈溢出了,我发现可能是我正在寻找的信息,但由于我不是一个程序员,而且不擅长与Excel,我不知道是否是我想要的,如果是,在哪里把它放在我的代码中。

这是我目前正在运行,只能在我的机器上运行:

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 

虽然它做我想要的,当我上传到我们的份额,显然另一个用户将无法使用它。 我在一个问题上看到有人回答说要使用这个:

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

但是我不知道或者不明白如何将其纳入我的macros观上面。 任何帮助或build议将是伟大的。 我甚至在正确的轨道上?

虽然roryap的解决scheme是完美的,工作,我想添加另一个工作的解决scheme。

 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 

你可以像这样使用它:

 Dim desktopFolderPath As String desktopFolderPath = CreateObject("WScript.Shell").specialfolders("Desktop") fileName = desktopFolderPath & "\PRN Test files\" & Range("'Customer_Info'!R2").Text & ".prn" 

解决方法是将这些标准文件夹“标记化”为可在保存数据时replace标准文件夹path的符号,并在再次读取数据时将其扩展到下一台机器的本地定义的特殊文件夹。

我不知道任何普遍接受的令牌(标签,名称,缩写或粗俗的赞誉):如果有人能够启发我,我将不胜感激,因为这是一个非常普遍的任务,我讨厌重新发明轮子。

下面精美雕刻的方形轮子将为您的公寓提供一个有趣的对话框,并为您的问题提供一个笨拙的解决scheme:您只需通过下面的“replace”function保存path, 每次捕捉并保存一个文件夹数据存储的path

每次从数据存储中检索文件夹path并使用它打开文件夹时,使用 “扩展”function读取path

Cl,cl cl,,cl it it it it it it it it newly newly newly newly newly newly newly newly newly newly Wheel Wheel 我还在Excellerando Blogspot上为热核捕鼠器devise了一些非常有趣的devise:search“恐怖的黑客”这个词。

 公共函数SubstituteStandardFolders(ByVal strPath As String)As String
 '为标准文件夹插入缩写 
昏暗strUser作为string Dim strDesk As String Dim strAppD As String Dim strDocs As String Dim strTemp As String
strDesk = CreateObject(“WScript.Shell”)。SpecialFolders(“Desktop”) strDocs = CreateObject(“WScript.Shell”)。SpecialFolders(“MyDocuments”) strAppD = CreateObject(“WScript.Shell”)。SpecialFolders(“AppData”) strTemp = CreateObject(“Scripting.FileSystemObject”)。GetSpecialFolder(2) strUser = CreateObject(“WScript.Network”)。UserName
strPath = Replace(strPath,strDesk,“[Desktop]”,,,vbTextCompare) strPath = Replace(strPath,strDocs,“[My Documents]”,,,vbTextCompare) strPath = Replace(strPath,strAppD,“[Application Data]”,,,vbTextCompare) strPath = Replace(strPath,strTemp,“[Temp]”,,,vbTextCompare) strPath = Replace(strPath,strUser,“[User]”,,,vbTextCompare)
SubstituteStandardFolders = strPath
结束function

公共函数ExpandStandardFolders(ByVal作为string的strPath)作为string 用全名replace标准文件夹的缩写
Dim strUser As String Dim strDesk As String Dim strAppD As String Dim strDocs As String Dim strTemp As String
strDesk = CreateObject(“WScript.Shell”)。SpecialFolders(“Desktop”) strDocs = CreateObject(“WScript.Shell”)。SpecialFolders(“MyDocuments”) strAppD = CreateObject(“WScript.Shell”)。SpecialFolders(“AppData”) strTemp = CreateObject(“Scripting.FileSystemObject”)。GetSpecialFolder(2) strUser = CreateObject(“WScript.Network”)。UserName
strPath = Replace(strPath,“[Desktop]”,strDesk,,,vbTextCompare) strPath = Replace(strPath,“[My Documents]”,strDocs,,,vbTextCompare) strPath = Replace(strPath,“[Application Data]”,strAppD,,,vbTextCompare) strPath = Replace(strPath,“[Temp]”,strTemp,,,vbTextCompare) strPath = Replace(strPath,“[User]”,strUser,,,vbTextCompare)
ExpandStandardFolders = strPath
结束function

注意最后的'冰雹玛丽'条款:如果一切都失败了,换掉[用户]的用户名,并希望path在所有其他机器上以相同的方式构build。

这个代码有一些限制。 看看令牌列表:

[Desktop] [My Documents] [Application Data] [Temp] [User]

如果您枚举Shell SpecialFolders集合,您将看到不止于此:

 Public Sub EnumerateSpecialFolders() 
Dim objCollection As Object Dim varX As Variant
Set objCollection = CreateObject(“WScript.Shell”)。SpecialFolders
对于每个varX在objCollection中 Debug.Print varX 下一个varX
结束小组

但是,我不能从这个集合中获得密钥 – 只是值 – 而且序号会因系统而异。

如果你能得到钥匙,这将是一个合理的令牌,以供普遍使用。 当然,假设在您的公司的一半工作站上,“我的文档”不是由“MiaDocumetari”密钥或一串两字节和三字节的unicode检索的。

另外:欢迎来到Deep VBA。 这是一些需要使用方格轮换方法的简单任务,而且我有一个偷偷摸摸的事实,那就是在SDK开发人员库中,我们的同事使用更为广泛的语言来解决这个问题:这些解决方法源于糟糕的devise决策操作系统级别,没有人真的“解决”他们。