简体   繁体   English

Word 互操作 SaveAs2 为 PDF

[英]Word Interop SaveAs2 as PDF

I'm trying to save files as a PDF.我正在尝试将文件另存为 PDF。 My original code works to save as a word document..我的原始代码可以保存为 Word 文档。

Imports Word = Microsoft.Office.Interop.Word
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Dim objDoc As Word.Document = objWordApp.Documents.Open(appPath & "\PackListTemplate.dotm", [ReadOnly]:=True)
        objDoc = objWordApp.ActiveDocument
        With objDoc

... ...

    .SaveAs2(FileName:=savepath & soNumber & "_" & localDateTimeFileName & ".doc", AddToRecentFiles:=True, ReadOnlyRecommended:=True)

This is what I am trying to change my code to...这就是我试图将代码更改为...

.SaveAs2(savepath & "Packing Lists - " & soNumber & ".pdf", Word.WdSaveFormat.wdFormatPDF, AddToRecentFiles:=True, ReadOnlyRecommended:=True)

The problem is that the word application save as dialog box pops up.问题是弹出应用程序另存为对话框。 That's not ideal as this is supposed to be automated.这并不理想,因为这应该是自动化的。 When I use FileName:= everything works as I expect.当我使用FileName:=一切都按我的预期工作。 But when I use that bit in the PDF save, for some reason it doesn't like my Word.WdSaveFormat.wdFormatPDF .但是当我在 PDF 保存中使用那个位时,由于某种原因它不喜欢我的Word.WdSaveFormat.wdFormatPDF It underlines the W in Word .它强调WordW

What am I missing here?Any help is appreciated!我在这里错过了什么?感谢任何帮助!

Interop uses COM libraries registered at the time of installation of MS Office. Interop 使用在安装 MS Office 时注册的 COM 库。 This works as long as the machine, where you are trying to manipulate documents programmatically has MS Office installed.只要您尝试以编程方式操作文档的机器安装了 MS Office,这就会起作用。 A better approach is to use a toolkit which can merge template document and data even without MS Office installed (eg on a server).更好的方法是使用一个工具包,即使没有安装 MS Office(例如在服务器上),它也可以合并模板文档和数据。 You need to prepare a template Word document, format it and place placeholders in it where you want the data to appear in the final document.您需要准备一个模板 Word 文档,对其进行格式化并将占位符放置在其中您希望数据出现在最终文档中的位置。 You then prepare the data in .NET application and call document generation, based on a template document and your data.然后,您在 .NET 应用程序中准备数据并根据模板文档和您的数据调用文档生成。 Resulting document can be saved as docx, pdf or xps file or streamed in any of those formats to the client.生成的文档可以保存为 docx、pdf 或 xps 文件,或以任何这些格式流式传输到客户端。 You can see some examples here if you want to learn more.如果您想了解更多信息,可以在此处查看一些示例。 Eventually it takes only two lines of code to get the final document:最终只需要两行代码就可以得到最终的文档:

// Instancing report engine, by assigning the data source
DocumentGenerator dg = new DocumentGenerator(DataAccess.GetOrderById(7)); 
// Generating report by specifying the report template and the resulting report (as file paths) 
dg.GenerateDocument("example.docx", "example_output.docx");

When closing the document use:关闭文档时使用:

.Close(Word.WdSaveOptions.wdDoNotSaveChanges);

It worked form me.它对我有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM