我正在运行以下代码,它基本上将excel工作表的最后一行保存为PDF文件,它将PDF文件保存到excel工作表和word模板所在的文件夹中(它们位于同一个文件夹中)。
如何将不同的位置设置为保存点?
我想将用户限制在一个特定的位置,而不是excel工作表和word模板所在的文件夹。
例如::我希望在这里保存文件:“C:\Users\User\\文件夹”
此外,请指导我如何实现它到我的代码,有点新的这一点。
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发布于 2020-03-01 21:03:34
我想这就是你想要的:
ActiveWorkbook.ExportAsFixedFormat Type:=xlTypePDF FileName:="sales.pdf" Quality:=xlQualityStandard OpenAfterPublish:=True https://learn.microsoft.com/en-us/office/vba/api/excel.workbook.exportasfixedformat
发布于 2020-03-01 21:24:56
您的代码在这里使用变量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")返回当前登录用户的名称
发布于 2020-03-02 01:49:02
因此,改变:
StrMMPath = ThisWorkbook.Path & "\"指向要保存文件的位置。例如:
StrMMPath = C:\Users\" & Environ("Username") & "\Desktop\Folder\"很高兴看到你真的为此付出了一些努力。你从哪里得到代码的网站甚至告诉你如何做这样的改变!到目前为止,您在多个线程中所做的一切似乎都被要求向用户提供解决方案和/或对您从另一个站点复制的代码的修改。
PS:投票给帮助你的答案也是很礼貌的。
https://stackoverflow.com/questions/60479707
复制相似问题