从excel vb脚本制作pdf文件 [英] Make pdf file from excel vb script

查看:126
本文介绍了从excel vb脚本制作pdf文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,

你能帮助我吗?我有一个可以从excel创建pdf文件的vbscript:

Can you help me? I have a vbscript which can creat pdf file from excel:

Sub PDFActiveSheet()

Dim ws As Worksheet

Dim strPath As String

Dim myFile As Variant

Dim strFile As String

On Error GoTo errHandler



设置ws = ActiveSheet



'输入名称并选择文件夹

'开始在当前工作簿文件夹中

strFile =替换(替换(ws.Name,"",""),"。"," _" )_

            &安培; " _" _

            &安培;格式(Now()," yyyymmdd \_hhmm")_

             &安培; " .pdf"

strFile = ThisWorkbook.Path& " \" &安培; strFile



myFile = Application.GetSaveAsFilename _

     ; (InitialFileName:= strFile,_

        FileFilter:=" PDF Files(* .pdf),* .pdf",_

        Title:=" Select Folder and FileName to save"")



如果myFile<> "假QUOT;然后

    ws.ExportAsFixedFormat _

       输入:= xlTypePDF,_

       文件名:= myFile,_

       质量:= xlQualityStandard,_

        IncludeDocProperties:= True,_

        IgnorePrintAreas:= False,_

        OpenAfterPublish:= False



    MsgBox"PDF文件已创建。"

结束如果



exitHandler:

   退出子

errHandler:

    MsgBox"无法创建PDF文件"

   恢复exitHandler

结束子

Sub PDFActiveSheet()
Dim ws As Worksheet
Dim strPath As String
Dim myFile As Variant
Dim strFile As String
On Error GoTo errHandler

Set ws = ActiveSheet

'enter name and select folder for file
' start in current workbook folder
strFile = Replace(Replace(ws.Name, " ", ""), ".", "_") _
            & "_" _
            & Format(Now(), "yyyymmdd\_hhmm") _
            & ".pdf"
strFile = ThisWorkbook.Path & "\" & strFile

myFile = Application.GetSaveAsFilename _
    (InitialFileName:=strFile, _
        FileFilter:="PDF Files (*.pdf), *.pdf", _
        Title:="Select Folder and FileName to save")

If myFile <> "False" Then
    ws.ExportAsFixedFormat _
        Type:=xlTypePDF, _
        Filename:=myFile, _
        Quality:=xlQualityStandard, _
        IncludeDocProperties:=True, _
        IgnorePrintAreas:=False, _
        OpenAfterPublish:=False

    MsgBox "PDF file has been created."
End If

exitHandler:
    Exit Sub
errHandler:
    MsgBox "Could not create PDF file"
    Resume exitHandler
End Sub

我的问题是:如何修改如果我想要更改此pdf文件的文件名,如果我需要来自单元格的文件名?

My question is: how to modify If i want to change the filename of this pdf file, If I need a filename from a cell?

谢谢你的支持!

Viiboo

推荐答案

取决于您想要的文件夹,以及您是否在文件名中包含扩展名,但要使用单元格的值,请更改

Depends on what folder you want, too, and whether you are including the extension in the file name, but to use the value of a cell, change

myFile = Application.GetSaveAsFilename
_


   (InitialFileName:= strFile,
_


       
FileFilter:=" PDF Files(* .pdf),* .pdf",_


 &nbs磷;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;
标题:="选择文件夹和文件名以保存")

to

myFile =" C:\Foldername \"
&范围("A2")。值和& " .pdf"

其中A2具有所需的
文件名,但不包括扩展名。



这篇关于从excel vb脚本制作pdf文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆