繁体   English   中英

Excel to PDF - 如何指定宏将保存 PDF 文件的位置

[英]Excel to PDF - how to specify where the macro will save the PDF file

我正在运行这段代码,它基本上将我的 excel 表的最后一行保存为 PDF 文件,它将 PDF 文件保存到 excel 表和我的 word 模板所在的文件夹中(它们在同一文件夹中)。

如何将不同的位置设置为保存点?
我想将用户限制在特定位置,该位置不是 Excel 表和 word 模板所在的文件夹。

例如:我希望文件保存在这里:“C:\\Users\\User\\Desktop\\Folder”
另外,请指导我如何将它实现到我的代码中,这有点新。

Sub RunMerge()
' Sourced from: https://www.msofficeforums.com/mail-merge/21803-mailmerge-tips-tricks.html
' Note: this code requires a reference to the Word object model to be set, via Tools|References in the VBE.
Application.ScreenUpdating = False
Dim StrMMSrc As String, StrMMDoc As String, StrMMPath As String, StrName As String
Dim i As Long, j As Long
Const StrNoChr As String = """*./\:?|"
Dim wdApp As New Word.Application, wdDoc As Word.Document
wdApp.Visible = False
wdApp.DisplayAlerts = wdAlertsNone
StrMMSrc = ThisWorkbook.FullName
StrMMPath = ThisWorkbook.Path & "\"
StrMMDoc = StrMMPath & "MailMergeDocument.doc"
Set wdDoc = wdApp.Documents.Open(Filename:=StrMMDoc, AddToRecentFiles:=False, ReadOnly:=True, Visible:=False)
With wdDoc
  With .MailMerge
    .MainDocumentType = wdFormLetters
    .OpenDataSource Name:=StrMMSrc, ReadOnly:=True, AddToRecentFiles:=False, _
      LinkToSource:=False, Connection:="Provider=Microsoft.ACE.OLEDB.12.0;User ID=Admin;" & _
      "Data Source=StrMMSrc;Mode=Read;Extended Properties=""HDR=YES;IMEX=1"";", _
      SQLStatement:="SELECT * FROM `Sheet1$`"
    i = .DataSource.RecordCount
      .Destination = wdSendToNewDocument
      .SuppressBlankLines = True
      With .DataSource
        .FirstRecord = i
        .LastRecord = i
        .ActiveRecord = i
        StrName = .DataFields("Name")
      End With
      .Execute Pause:=False
      For j = 1 To Len(StrNoChr)
        StrName = Replace(StrName, Mid(StrNoChr, j, 1), "_")
      Next
      StrName = Trim(StrName)
      With wdApp.ActiveDocument
        'Add the name to the footer
        '.Sections(1).Footers(wdHeaderFooterPrimary).Range.InsertBefore StrName
        '.SaveAs Filename:=StrMMPath & StrName & ".docx", FileFormat:=wdFormatXMLDocument, AddToRecentFiles:=False
        ' and/or:
        .SaveAs Filename:=StrMMPath & StrName & ".pdf", FileFormat:=wdFormatPDF, AddToRecentFiles:=False
        .Close SaveChanges:=False
      End With
    .MainDocumentType = wdNotAMergeDocument
  End With
  .Close SaveChanges:=False
End With
wdApp.DisplayAlerts = wdAlertsAll
wdApp.Quit
Set wdDoc = Nothing: Set wdApp = Nothing
Application.ScreenUpdating = False
End Sub

我认为这就是你正在寻找的:

ActiveWorkbook.ExportAsFixedFormat Type:=xlTypePDF FileName:="sales.pdf" Quality:=xlQualityStandard OpenAfterPublish:=True 

https://docs.microsoft.com/en-us/office/vba/api/excel.workbook.exportasfixedformat

您的代码使用变量strMMPath作为此处的路径。

.SaveAs Filename:=StrMMPath & StrName & ".pdf", FileFormat:=wdFormatPDF, AddToRecentFiles:=False

要将其更改为当前用户桌面上的文件夹,请使用此

Dim SavePath as String

SavePath = "C:\Users\" & Environ$("UserName") & "\Desktop\Folder\"
.SaveAs Filename:=SavePath & StrName & ".pdf", FileFormat:=wdFormatPDF, AddToRecentFiles:=False

注意: Environ$("UserName")返回当前登录的用户名

所以改变:

StrMMPath = ThisWorkbook.Path & "\"

指向要保存文件的位置。 例如:

StrMMPath = C:\Users\" & Environ("Username") & "\Desktop\Folder\"

很高兴看到你真的在这一切上投入了一些努力。 您获得代码的站点甚至会告诉您如何进行此类更改!!! 到目前为止,您在多个线程中所做的一切似乎都被要求提供解决方案和/或修改您从另一个站点复制的代码。

PS:投票给对你有帮助的答案也是一种普遍的礼貌。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM