将 Excel 文件另存为 PDF 到特定路径

发布于 2024-09-26 16:11:28 字数 757 浏览 2 评论 0原文

我想将 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”这样的路径。

I would like to save an Excel file as a .pdf file to a specific location and then send the file in a mail.

I'm using Office 2000 :|

This is my code so far:

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

I can easily save the file and mail it, but I can't save it to a specific location.

I need to be able to specificy a path like "C:\path\file.pdf".

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

九厘米的零° 2024-10-03 16:11:28

如果您将文件保存到固定位置但无法选择位置,作为最后的手段,您始终可以使用 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 与打开覆盖,然后根据需要删除源文件

如果您想使用文件对话框来选择目标,您可以使用 UserAccounts.CommonDialog 对象(尽管这不适用于 Vista)或 SAFRCFileDlg.FileOpen (几乎只能在 XP 上运行)或者借用 IE 提示框。 (不幸的是,据我所知,VBS 的选项并不是那么好)

在这里查看它们: http:// www.robvanderwoude.com/vbstech_ui_fileopen.php

If you have the file saved to fixed location but you're unable to choose where, as a last resort you could always use fso's MoveFile to move it to your specified location

eg. If the file is always saved as "C:\temp\file1.pdf", and you want it on desktop

'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

If you want to check for an existing file conflict (I believe fso's Move doesn't allow for overwrite), use CopyFile with over-write switched on then Delete the source file if necessary

If you'd like to use a file dialog to choose the destination, you can use the UserAccounts.CommonDialog object (although that doesn't work with Vista) or SAFRCFileDlg.FileOpen (pretty much only works on XP) or borrow an IE prompted box. (Unfortunately the options aren't all that great with VBS to my knowledge)

Check them out here: http://www.robvanderwoude.com/vbstech_ui_fileopen.php

回眸一遍 2024-10-03 16:11:28

这有点复杂,因为您必须设置注册表项。
假设您拥有已安装初始注册表项的完整版 Adob​​e Acrobat:

首先,您具有注册表访问功能,并将其放入非工作表模块中:

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

然后,您使用以下代码来设置注册表项告诉 Adob​​e 在哪里保存文件。请注意,每次打印时都必须进行设置。

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,然后转到注册表项并查看 HKCU\Software\Adobe\Acrobat Distiller\PrinterJobControl LastPDFPortFolder 项中使用的应用程序。然后使用该可执行文件的完整应用程序路径的名称。

It's a little complicated, as you have to set registry keys.
Assuming that you have a full version of Adobe Acrobat that has installed the initial registry keys:

First, you have the registry accessing functions, which you put in a non-sheet module:

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

Then, you use the following code to set the registry key that tells Adobe where to save the file. Note, it has to be set everytime you print.

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)

Also Note, the "C:\Windows\splwow64.exe" is what I needed for my Excel 2010 32-bit, and it may be different for you. To determine it (which won't change) first print manually to PDF, then go to the registry key and see what application is used in the HKCU\Software\Adobe\Acrobat Distiller\PrinterJobControl LastPDFPortFolder key. Then use the name of the full application path for that executable.

绝對不後悔。 2024-10-03 16:11:28

试试这个:

sName = "C:\path\file.pdf"

ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:=sName

Try this:

sName = "C:\path\file.pdf"

ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:=sName
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文