| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647 |
- package office
- import (
- "github.com/go-ole/go-ole"
- "github.com/go-ole/go-ole/oleutil"
- "log"
- )
- func WPSWordSaveTo(wordFilePath, targetFilePath string, WdSaveAsFormat int) error {
- defer func() {
- if err := recover(); err != nil {
- log.Printf("WPSWordSaveTo error: %v\n", err)
- }
- }()
- ole.CoInitialize(0)
- defer ole.CoUninitialize()
- iunk, err := oleutil.CreateObject("kwps.Application")
- if err != nil {
- log.Printf("creating Word object error: %s", err)
- }
- defer iunk.Release()
- word := iunk.MustQueryInterface(ole.IID_IDispatch)
- defer word.Release()
- oleutil.PutProperty(word, "DisplayAlerts", false)
- oleutil.PutProperty(word, "Visible", false)
- // opening then saving works due to the call to doc.Settings.SetUpdateFieldsOnOpen(true) above
- docs := oleutil.MustGetProperty(word, "Documents").ToIDispatch()
- defer docs.Release()
- doc := oleutil.MustCallMethod(docs, "Open", wordFilePath).ToIDispatch()
- defer doc.Release()
- /*
- WdExportFormat
- wdExportFormatPDF 17 将文档导出为 PDF 格式。
- wdExportFormatXPS 18 将文档导出为 XML Paper Specification (XPS) 格式。
- */
- oleutil.MustCallMethod(doc, "SaveAs", targetFilePath, WdSaveAsFormat)
- oleutil.PutProperty(doc, "Saved", true)
- oleutil.MustCallMethod(doc, "Close")
- oleutil.MustCallMethod(word, "Quit")
- return nil
- }
|