【发布时间】:2015-11-01 09:58:25
【问题描述】:
上下文:
Vb.net 程序
Visual Studio 2010 终极版
MS Word 2010 自动化
Microsoft.Office.Interop.Word 库
我正在使用 saveAs2 方法来保存我正在创建的新文档,但是当我调用该方法时应用程序仍在提示我。为什么?
应用程序不可见。 application.displayAlerts 为假
各位有什么想法吗?
另外,当我手动完成 SaveUI 提示时,saveAs2 方法会引发异常。
这是我为要求它的人提供的代码:
Public Sub generateModel() Implements ModelGenerator.generateModel
wordApp.Visible = True
wordApp.DisplayAlerts = Word.WdAlertLevel.wdAlertsNone
wordDoc = wordApp.Documents.Add
wordDoc.PageSetup.TopMargin = wordApp.InchesToPoints(0.25)
wordDoc.PageSetup.BottomMargin = wordApp.InchesToPoints(0.25)
wordDoc.PageSetup.LeftMargin = wordApp.InchesToPoints(0.25)
wordDoc.PageSetup.RightMargin = wordApp.InchesToPoints(0.25)
With wordDoc.Content.Paragraphs.Add(wordDoc.Bookmarks.Item("\endofdoc").Range)
.Range.Text = _text
.Format.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter
.Format.LineUnitBefore = 1
.Range.Font.SmallCaps = True
.Range.Font.Size = 12
End With
Dim logo = wordDoc.Shapes.AddPicture(logoLoc)
logo.Height = wordApp.InchesToPoints(0.5)
logo.Width = wordApp.InchesToPoints(1.18)
Me.mainTable = wordDoc.Tables.Add(wordDoc.Bookmarks.Item("\endofdoc").Range, 3, 2)
mainTable.Rows.HeightRule = Word.WdRowHeightRule.wdRowHeightExactly
mainTable.Columns.Width = wordApp.InchesToPoints(4)
mainTable.Rows.Height = wordApp.InchesToPoints(3.25)
mainTable.Select()
wordApp.Selection.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphLeft
With wordDoc.Content.Paragraphs.Add(wordDoc.Bookmarks.Item("\endofdoc").Range)
.Range.Text = "Rapport journalier de production - page 2"
.Range.Font.Size = 10
.Format.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter
.Format.LineUnitBefore = 0
.Format.SpaceBeforeAuto = False
.Format.SpaceBefore = 0
End With
wordDoc.SaveAs2("C:\Doc1.docx")
wordDoc.Close(False)
wordApp.Application.Quit()
End Sub
**更新:
我在另一台机器上测试了代码,它可以工作。所以我在我的身上尝试了这段代码:
Dim app As New Microsoft.Office.Interop.Word.Application
Dim doc = app.Documents.Add
doc.SaveAs2("C:\Users\simon\Documents\Doc3.docx")
它仍然会弹出 saveUI。我很困惑...
【问题讨论】:
-
为什么不在 SaveAs2 调用中显示您的代码?
-
我刚刚添加了代码。
-
抛出哪种类型的异常?你也可以发一下吗?此外,您说:“应用程序不可见”->但是,您有
wordApp.Visible = True,表明它应该是可见的? -
抛出的异常是一个普通的
System.Runtime.InteropServices.COMException,上面写着“命令失败”。是的,在我给你的代码 sn-p 中,应用程序是可见的,但即使它不可见,我也尝试过。另外,应用程序是否可见并不重要。
标签: vb.net ms-word office-interop