Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/28.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
仅允许在Excel 2007/2010中保存为PDF_Excel_Pdf_Printing - Fatal编程技术网

仅允许在Excel 2007/2010中保存为PDF

仅允许在Excel 2007/2010中保存为PDF,excel,pdf,printing,Excel,Pdf,Printing,有没有办法强制excel始终以PDF格式打印文件?出于某种原因,我在这个网站和其他网站上找到的标准代码似乎不起作用 以下是我使用的代码: ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, FileName:= _ cFileName, Quality:=xlQualityStandard, IncludeDocProperties:=True, _ IgnorePrintAreas:=False, _ OpenAfterPublish:=False

有没有办法强制excel始终以PDF格式打印文件?出于某种原因,我在这个网站和其他网站上找到的标准代码似乎不起作用

以下是我使用的代码:

ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, FileName:= _
cFileName, Quality:=xlQualityStandard, IncludeDocProperties:=True, _
IgnorePrintAreas:=False, _
OpenAfterPublish:=False

我有一个简单的输入框来捕获文件名,我想阻止他们做任何其他事情。理想情况下,我希望将此代码放入BeforeSave事件和BeforePrint事件中,这样它们唯一能做的就是打印到PDF。这可能吗?

您是遇到了这样的错误还是正在运行代码

自动化错误:调用的对象已从Excel 2000中的客户端断开连接错误消息

如果是,请查看下面的链接

在工作表中使用下面的代码

    Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
    Macro1
    End Sub
在新模块中添加以下代码

    Sub Macro1()
    cfilename = "C:\Users\SONY\Desktop\Book1.pdf" 'you can use the input box method to get the desired file name and location
        ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
            cfilename, Quality:=xlQualityStandard, _
            IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:= _
            False
    End Sub

您是遇到这样的错误还是正在运行代码

自动化错误:调用的对象已从Excel 2000中的客户端断开连接错误消息

如果是,请查看下面的链接

在工作表中使用下面的代码

    Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
    Macro1
    End Sub
在新模块中添加以下代码

    Sub Macro1()
    cfilename = "C:\Users\SONY\Desktop\Book1.pdf" 'you can use the input box method to get the desired file name and location
        ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
            cfilename, Quality:=xlQualityStandard, _
            IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:= _
            False
    End Sub

很久以前,我将开源PDFPrinter与Excel结合使用。下面是我编写的一些代码,它们似乎可以满足您的需要。也许你可以用它作为你自己解决方案的开始

'在同一目录中以pdf格式打印保存的文件

KTCurrentFilePath = ActiveWorkbook.Path 'Store current FilePath

'Define Variables for PDF printjob

Dim pdfjob As Object

Dim KTPDFName As String

Dim KTPDFPath As String

Dim KTPCurrentPrinter As String
'设置变量值

KTPDFName = Range("MyPDFName").Value & ".pdf"

KTPDFPath = ActiveWorkbook.Path & Application.PathSeparator

KTPCurrentPrinter = Application.ActivePrinter
'检查工作表是否为空,如果为空则退出

If IsEmpty(ActiveSheet.UsedRange) Then Exit Sub
'启动PDF引擎

Set pdfjob = CreateObject("PDFCreator.clsPDFCreator")

On Error GoTo 0

With pdfjob

    If .cStart("/NoProcessingAtStartup") = False Then

        MsgBox "Can't initialize PDFCreator.", vbCritical + _

            vbOKOnly, "PrtPDFCreator"

            Application.ActivePrinter = KTPCurrentPrinter

        Exit Sub

    End If

    .cOption("UseAutosave") = 1

    .cOption("UseAutosaveDirectory") = 1

    .cOption("AutosaveDirectory") = KTPDFPath

    .cOption("AutosaveFilename") = KTPDFName

    .cOption("AutosaveFormat") = 0    ' 0 = PDF

    .cClearCache

End With
'将文档打印为PDF

ActiveSheet.PrintOut copies:=1, ActivePrinter:="PDFCreator"
'等待打印作业进入打印队列

Do Until pdfjob.cCountOfPrintjobs = 1

    DoEvents

Loop

pdfjob.cPrinterStop = False
'等待PDF创建者完成,然后释放对象

Do Until pdfjob.cCountOfPrintjobs = 0

    DoEvents

Loop

pdfjob.cClose

Set pdfjob = Nothing
'将打印机重置为默认值

Application.ActivePrinter = KTPCurrentPrinter
端接头

问候,


罗伯特·伊尔布林克(Robert Ilbrink)

很久以前,我将开源PDFPrinter与Excel结合使用。下面是我编写的一些代码,它们似乎可以满足您的需要。也许你可以用它作为你自己解决方案的开始

'在同一目录中以pdf格式打印保存的文件

KTCurrentFilePath = ActiveWorkbook.Path 'Store current FilePath

'Define Variables for PDF printjob

Dim pdfjob As Object

Dim KTPDFName As String

Dim KTPDFPath As String

Dim KTPCurrentPrinter As String
'设置变量值

KTPDFName = Range("MyPDFName").Value & ".pdf"

KTPDFPath = ActiveWorkbook.Path & Application.PathSeparator

KTPCurrentPrinter = Application.ActivePrinter
'检查工作表是否为空,如果为空则退出

If IsEmpty(ActiveSheet.UsedRange) Then Exit Sub
'启动PDF引擎

Set pdfjob = CreateObject("PDFCreator.clsPDFCreator")

On Error GoTo 0

With pdfjob

    If .cStart("/NoProcessingAtStartup") = False Then

        MsgBox "Can't initialize PDFCreator.", vbCritical + _

            vbOKOnly, "PrtPDFCreator"

            Application.ActivePrinter = KTPCurrentPrinter

        Exit Sub

    End If

    .cOption("UseAutosave") = 1

    .cOption("UseAutosaveDirectory") = 1

    .cOption("AutosaveDirectory") = KTPDFPath

    .cOption("AutosaveFilename") = KTPDFName

    .cOption("AutosaveFormat") = 0    ' 0 = PDF

    .cClearCache

End With
'将文档打印为PDF

ActiveSheet.PrintOut copies:=1, ActivePrinter:="PDFCreator"
'等待打印作业进入打印队列

Do Until pdfjob.cCountOfPrintjobs = 1

    DoEvents

Loop

pdfjob.cPrinterStop = False
'等待PDF创建者完成,然后释放对象

Do Until pdfjob.cCountOfPrintjobs = 0

    DoEvents

Loop

pdfjob.cClose

Set pdfjob = Nothing
'将打印机重置为默认值

Application.ActivePrinter = KTPCurrentPrinter
端接头

问候,



Robert Ilbrink

你的代码做什么而不是工作?确保你的输入框应该用文件名捕获文件的整个路径,例如C:\Users\SONY\Desktop\Book1.pdfif如果我没有输入完整路径,它会默认为当前路径吗?我计划一旦我让它工作,就覆盖默认路径,所以我只关心名称。你的代码做什么而不工作?确保你的inputbox用它的名称捕获文件的整个路径,例如C:\Users\SONY\Desktop\Book1.pdfif如果我没有输入完整的路径,它不是默认为当前路径吗?我计划在我让它工作后覆盖默认路径,所以我只关心名称。不,我没有收到任何错误-它只是不保存。没有错误,没有。没有错误,但是也没有生成PDF文件。它似乎只是在运行,但没有输出。嗨,杰伊,我使用了你给出的相同代码,它在保存工作表方面工作得很好。我在点击print.Kannan时出错-你是说它可以保存为PDF格式,但不会打印为PDF格式吗?好吧,我完全搞糊涂了。@jay您是否将KannanS的第一部分代码放入了“ThisWorkbook”代码窗格中?它必须放在那里才能捕获BeforeSave事件。打印事件重新路由将需要一个单独的代码块不,我没有得到任何错误-它只是不保存。没有错误,没有。没有错误,但是也没有生成PDF文件。它似乎只是在运行,但没有输出。嗨,杰伊,我使用了你给出的相同代码,它在保存工作表方面工作得很好。我在点击print.Kannan时出错-你是说它可以保存为PDF格式,但不会打印为PDF格式吗?好吧,我完全搞糊涂了。@jay您是否将KannanS的第一部分代码放入了“ThisWorkbook”代码窗格中?它必须放在那里才能捕获BeforeSave事件。打印事件重新路由将需要一段单独的代码Robert-是否需要我有一个PDF creator加载项才能使我的PDF例程正常工作?我以为Excel附带了一个内置的?Robert-是否需要我有一个PDF创建者外接程序才能让我的PDF例程正常工作?我以为Excel内置了一个?