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

有没有办法强制excel总是打印PDF格式的文件? 出于某种原因,我发现的标准代码(在本网站和其他网站上)似乎不起作用。

以下是我正在使用的代码:

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

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

很久以前,我使用了与Excel结合的开源PDFPrinter。 这里是我写的一些代码,似乎是做你想做的。 也许你可以使用这个作为你自己的解决scheme的开始?

'将保存的文件作为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 

'设置variables值

 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创build者完成后再释放对象

 Do Until pdfjob.cCountOfPrintjobs = 0 DoEvents Loop pdfjob.cClose Set pdfjob = Nothing 

'将打印机重置为默认值

 Application.ActivePrinter = KTPCurrentPrinter 

结束小组

问候,

罗伯特·伊尔布林克

你是否得到这样的错误或运行代码?

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

如果是的话,看看下面的链接

http://support.microsoft.com/kb/813120

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

  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