【问题标题】:Document.Save is Showing SaveAs DialogDocument.Save 显示另存为对话框
【发布时间】:2014-08-18 09:53:53
【问题描述】:

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

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

为什么我收到“编辑过多”错误,因此不需要进行如此多的保存(这无论如何都会减慢处理速度!)?

【问题讨论】:

  • 这是您项目中的所有代码/代码吗???它可以在我的电脑上运行。“没有另存为对话框/没有错误”
  • 没有。这实际上是一个微小的提取物。它有 100 行代码,分布在各个类中。在我评论说“执行了许多任务”的地方,可能会有 大量 的操作,包括添加文本、格式化、添加图表、向表格添加行等。我的直觉是真正的问题围绕“文档中的编辑过多”错误,这迫使我保存 - 也许保存的频率正在吹掉 Word...
  • 我会说问题出在“执行的许多任务”中
  • 为什么对这个问题投反对票???我可以知道为什么这样我可以在未来改进我的问题......

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


【解决方案1】:

我一直在搞乱,想出一个解决方法,而不是真正的解决方法。我怀疑真正的问题是too many edits 错误。所以我做了更多的拖网搜索并找到了this forum post - 特别是史蒂夫的第 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() 条目 - 不再显示另存为对话框。

【讨论】:

    猜你喜欢
    • 2013-11-12
    • 1970-01-01
    • 1970-01-01
    • 2011-08-03
    • 2015-03-28
    • 1970-01-01
    • 1970-01-01
    • 2021-10-25
    • 2021-08-20
    相关资源
    最近更新 更多