我終于遇到了一個問題,我還沒有在這里或網路上的其他任何地方找到答案:
我的程式從儀器中獲取一些測量值(我不能直接控制它,所以我必須等到用戶完成測量并決議報告),計算一些派生值并將這些值放回 pdf 報告中,由儀器控制軟體自動生成。
這一切都有效,直到我來到開始列印輸出的那一行。它總是打開文字列印對話框,而不是默默地覆寫我的檔案。在呼叫 PrintOut 時,我實際上不明白我做錯了什么。
這是示例代碼:
Imports Microsoft.Office.Interop
Module Example
Private Sub PrintReport()
Dim intAnswer As Integer
Dim strReportFileName As String = ""
Dim appWord As New Word.Application
Dim wdDoc As Word.Document
dim strPPF as string = "0.5" 'For testing, normally a parameter
dim strFolder as string = "C:\UVVis-Data" 'For testing, normally a parameter
'Find and open the PDF file of the report:
strReportFileName = (From fi As IO.FileInfo In (New IO.DirectoryInfo(strFolder.GetFiles("*.pdf")) Order By fi.LastWriteTime Descending Select fi)(0).FullName 'It will be always the newest file in that folder
appWord.Visible = False 'hide word from the user
wdDoc = appWord.Documents.Open(strReportFileName) 'open the PDF report
'Replace the placeholders which were defined in the report template earlier:
With appWord.Selection.Find
.Text = "#PPF#"
.Replacement.ClearFormatting()
.Replacement.Text = strPPF
.Execute(Replace:=Microsoft.Office.Interop.Word.WdReplace.wdReplaceAll)
End With
'Print out the modified report:
'wdDoc.PrintOut(False, False,, strReportFileName,,,,,,, True) 'this was my first approach
wdDoc.PrintOut(Background:=False, Append:=False, OutputFileName:=strReportFileName, PrintToFile:=True) 'this also doesn't work as intended
'Close the file and restore word to it's normal state:
wdDoc.Close(Microsoft.Office.Interop.Word.WdSaveOptions.wdDoNotSaveChanges)
appWord.Visible = True
appWord.Quit()
End Sub
end Module
uj5u.com熱心網友回復:
使用將檔案保存為 PDF 或 XPS 格式的Document.ExportAsFixedFormat方法。
Public Sub ExportAsFixedFormat_Example()
wdDoc.ExportAsFixedFormat pbFixedFormatTypePDF, "pathandfilename.pdf"
End Sub
uj5u.com熱心網友回復:
謝謝你的反饋。我發現了我的代碼的第二個問題:在 word 中打開原始檔案后,我無法覆寫它。
我解決了這個問題,首先將 pdf 移動到一個臨時檔案夾,在 word 中打開該臨時檔案并在 word 關閉后將其洗掉。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/448310.html
下一篇:VB.NET通過表單插入資料
