0

我有一个 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
...

有人知道为什么会显示另存为对话框吗?我能阻止它吗?

是否有原因导致我收到“编辑过多”错误,因此不需要进行如此多的保存(这无论如何都会减慢进程!)?

4

1 回答 1

0

我一直在搞乱,并想出了一个解决方法,而不是真正的解决方法。我怀疑真正的问题在于too many edits错误。所以我做了更多的拖网搜索并找到了这个论坛帖子- 特别是史蒂夫的第 4 篇帖子。

这成功了,我把它简化了一点:

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

Steve 建议暂存堆空间已用完。

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

于 2014-08-19T08:51:19.090 回答