首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Excel到PDF -如何指定宏将保存PDF文件的位置

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

Stack Overflow用户
提问于 2020-03-01 20:57:37
回答 3查看 398关注 0票数 0

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

如何将不同的位置设置为保存点?

我想将用户限制在一个特定的位置,而不是excel工作表和word模板所在的文件夹。

例如::我希望在这里保存文件:“C:\Users\User\\文件夹”

此外,请指导我如何实现它到我的代码,有点新的这一点。

代码语言:javascript
复制
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
EN

回答 3

Stack Overflow用户

发布于 2020-03-01 21:03:34

我想这就是你想要的:

代码语言:javascript
复制
ActiveWorkbook.ExportAsFixedFormat Type:=xlTypePDF FileName:="sales.pdf" Quality:=xlQualityStandard OpenAfterPublish:=True 

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

票数 0
EN

Stack Overflow用户

发布于 2020-03-01 21:24:56

您的代码在这里使用变量strMMPath作为路径。

代码语言:javascript
复制
.SaveAs Filename:=StrMMPath & StrName & ".pdf", FileFormat:=wdFormatPDF, AddToRecentFiles:=False

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

代码语言:javascript
复制
Dim SavePath as String

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

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

票数 0
EN

Stack Overflow用户

发布于 2020-03-02 01:49:02

因此,改变:

代码语言:javascript
复制
StrMMPath = ThisWorkbook.Path & "\"

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

代码语言:javascript
复制
StrMMPath = C:\Users\" & Environ("Username") & "\Desktop\Folder\"

很高兴看到你真的为此付出了一些努力。你从哪里得到代码的网站甚至告诉你如何做这样的改变!到目前为止,您在多个线程中所做的一切似乎都被要求向用户提供解决方案和/或对您从另一个站点复制的代码的修改。

PS:投票给帮助你的答案也是很礼貌的。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60479707

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档