【问题标题】:VBA - Saving to any users desktopVBA - 保存到任何用户桌面
【发布时间】:2016-09-27 23:05:50
【问题描述】:

我已经搜索了答案并阅读了许多回复,但我仍然迷路了。我有一个Excel 文档,我已经设置了一个 VBA CommandButton,它将工作表导出到 PDF 文档并保存到我的桌面,但是由于我需要多个用户来访问和保存文档,所以我假设它需要找到用户桌面并保存在那里,这对于不同的用户会有所不同。

我正在使用

Private Sub CommandButton1_Click() 
Sub SavePDF()     
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, _                   
          Filename:="C:\Integration\Export.pdf", _        
          OpenAfterPublish:=True 
End Sub  

任何帮助将不胜感激

【问题讨论】:

标签: excel vba


【解决方案1】:

我使用以下内容保存到用户桌面。我通常这样做是为了编写临时文本文件,然后将其删除,但它也可以用于此目的:

Private Sub CommandButton1_Click() 
Sub SavePDF()     
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, _                   
          Filename:= Environ("USERPROFILE") & "\Desktop\Export.pdf", _        
          OpenAfterPublish:=True 
End Sub  

【讨论】:

    【解决方案2】:

    如果它始终是您可以使用的桌面

    Private Sub CommandButton1_Click()
        ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, _
              Filename:=CreateObject("WScript.Shell").SpecialFolders("Desktop") & "\Export.pdf", _
              OpenAfterPublish:=True
    End Sub
    

    我会将 CommandButton1 重命名为其他名称 - 可能是 cmdExportPDF,并且正如 jsheeran 建议的那样 - 删除 Sub SavePDF() 行。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-06-18
      • 1970-01-01
      • 2013-05-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-07-07
      • 2019-04-30
      相关资源
      最近更新 更多