Document.Save显示SaveAs对话框

时间:2014-08-18 09:53:54

标签: vb.net ms-word word-vba ole-automation

我有一个Visual Studio 2010 VB.NET 4.0 Windows应用程序项目。该代码填充了Word 2010文档。该区域有30到60个表格,任何地方都有30到50个嵌入式图表(全部定义为内联形状(InlineShape))。

我不得不开始进行常规Document.Save()调用,因为我收到以下错误:There are too many edits in the document. This operation will be incomplete. Save your work.。有足够的磁盘空间和内存。

在大多数情况下,.Save()有效,但在调用.Save()时会随机显示另存为对话框。作为旁注,如果我单击取消,则会引发以下错误:Command failed at Microsoft.Office.Interop.Word.DocumentClass.Save()

以下是代码的摘录,可以让您了解正在发生的事情:

Imports _word = Microsoft.Office.Interop.Word
...
...
Dim wrd As _word.Application = CreateObject("Word.Application")
wrd.Visible = True
wrd.ScreenUpdating = True

Dim doc As _word.Document = wrd.Documents.Open("C:\my-file-template.docx")

doc.Application.DisplayAlerts = _word.WdAlertLevel.wdAlertsNone
doc.Range.NoProofing = True

Dim reportFilePathName As String = "C:\my-file.docx"
If File.Exists(reportFilePathName) Then
    File.Delete(Me.reportFilePathName)
End If
doc.SaveAs2(reportFilePathName)
...
'Numerous tasks carried out
...
doc.Save() 'This may or may not cause the save as dialog to show
...

有人知道保存为对话框的原因吗?我可以阻止吗?

我有没有理由得到“编辑太多”错误,因此不需要进行如此多的保存(这无论如何都会减慢进程!)

1 个答案:

答案 0 :(得分:0)

我一直在搞乱并提出一个解决方案,而不是一个真正的解决方案。我怀疑真正的问题是too many edits错误。所以我做了一些拖网工作并找到了this forum post - 特别是史蒂夫的第四篇文章。

这就是诀窍,我把它剥了一下:

Imports _word = Microsoft.Office.Interop.Word
...
Public Shared Sub GarbageCollect(ByRef doc As _word.Document)
    doc.Application.Options.Pagination = False
    doc.UndoClear()
    doc.Repaginate()
    doc.Application.ScreenUpdating = True
    doc.Application.ScreenRefresh()
End Sub

史蒂夫建议临时堆空间已用完。

这消除了too many edits in document错误,随后我能够删除所有doc.Save()个条目 - 不再显示为对话框显示。