将Excel文件保存为PDF以指定path

我想将一个Excel文件保存为.pdf文件到特定的位置,然后通过邮件发送文件。

我正在使用Office 2000:|

这是我的代码到目前为止:

Application.ActivePrinter = "PDF995 on Ne00:" ActiveSheet.PageSetup.PrintArea = Range("A68").Value ActiveWindow.SelectedSheets.PrintOut Copies:=1, ActivePrinter:= _ "PDF995 on Ne00:", Collate:=True Set WB = ActiveWorkbook Set oApp = CreateObject("Outlook.Application") Set omail = oApp.Createitem(0) With omail .To = Range("B61").Value .Subject = "Approved" .Body .Display Rows("81:134").Select Selection.EntireRow.Hidden = True End With 

我可以轻松地保存文件并将其发送,但是我无法将其保存到特定的位置。

我需要能够指定一个像“C:\ path \ file.pdf”这样的path。

如果将文件保存到固定位置,但无法select位置,作为最后的手段,您可以始终使用fso的MoveFile将其移动到指定的位置

例如。 如果该文件始终保存为“C:\ temp \ file1.pdf”,并且您希望在桌面上

 'Initialise first' set fso = CreateObject("Scripting.FileSystemObject") ... 'After procedure' desired_destination = "c:\windows\desktop\" target_file = "C:\temp\file1.pdf" fso.MoveFile target_file, desired_destination 

如果你想检查一个已经存在的文件冲突(我相信fso的Move不允许覆盖),用CopyFile打开over-write,如果需要的话删除源文件

如果你想使用文件对话框来select目的地,可以使用UserAccounts.CommonDialog对象(尽pipe这不适用于Vista)或SAFRCFileDlg.FileOpen(几乎只适用于XP),或借用IE提示框。 (不幸的是,据我所知,VBS的select并不是那么好)

检查他们在这里: http : //www.robvanderwoude.com/vbstech_ui_fileopen.php

这有点复杂,因为您必须设置registry项。 假设您拥有安装了初始registry项的Adobe Acrobat完整版本:

首先,你有registry访问function,你把它放在一个非表单模块:

 Private Const HKEY_CURRENT_USER As Long = &H80000001 Private Const HKCU = HKEY_CURRENT_USER Private Const KEY_SET_VALUE = &H2& Private Const REG_SZ = 1 Private Declare Function RegOpenKeyEx Lib "advapi32" _ Alias "RegOpenKeyExA" ( _ ByVal hKey As Long, _ ByVal lpSubKey As String, _ ByVal ulOptions As Long, _ ByVal samDesired As Long, _ phkResult As Long) As Long Private Declare Function RegSetValueExA Lib "ADVAPI32.DLL" _ (ByVal hKey As Long, _ ByVal sValueName As String, _ ByVal dwReserved As Long, _ ByVal dwType As Long, _ ByVal sValue As String, _ ByVal dwSize As Long) As Long Private Declare Function RegCloseKey Lib "ADVAPI32.DLL" ( _ ByVal hKey As Long) As Long 

然后,您使用下面的代码来设置通知Adobe在哪里保存该文件的registry项。 请注意,每次打印时都必须设置。

 Dim RegResult As Long, Result As Long RegResult = RegOpenKeyEx(HKCU, "Software\Adobe\Acrobat Distiller\PrinterJobControl", _ 0&, KEY_SET_VALUE, Result) RegResult = RegSetValueExA(Result, "C:\Windows\splwow64.exe", 0&, REG_SZ, _ FileName, Len(FileName)) RegResult = RegCloseKey(Result) 

另外请注意,“C:\ Windows \ splwow64.exe”是我需要的Excel 2010 32位,它可能会有所不同。 要确定它(不会更改)首先手动打印到PDF,然后转到registry项,看看在HKCU \ Software \ Adob​​e \ Acrobat Distiller \ PrinterJobControl LastPDFPortFolder项中使用了什么应用程序。 然后使用该可执行文件的完整应用程序path的名称。

尝试这个:

 sName = "C:\path\file.pdf" ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:=sName