问题
I have a Visual Studio 2010 VB.NET 4.0 Windows Application project. The code is populating a Word 2010 document. There are anywhere in the region of 30 to 60 tables and anywhere in the region of 30 to 50 embedded charts (all defined as inline shapes (InlineShape
)).
I had to start putting in regular Document.Save()
calls as I was getting the following error: There are too many edits in the document. This operation will be incomplete. Save your work.
. There is plenty of disk space available and memory also.
In most cases, the .Save()
works, but randomly the save as dialog will be shown when the .Save()
is called. As a side note, if I click to cancel the following error is raised: Command failed at Microsoft.Office.Interop.Word.DocumentClass.Save()
.
Here is an extract of the code to give you an idea of what is going on:
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
...
Does anybody know why the save as dialog is showing? Can I stop it?
Is there a reason why I am getting the "too many edits" error and therefore don't need to have so many saves going on (which is slowing the process down anyway!)?
回答1:
I have been messing extensively and come up with a work around rather than a true fix. I suspected that the real issue is with the too many edits
error. So I've done some more trawling and found this forum post - specifically the 4th post by Steve.
This did the trick and I stripped it down a little to this:
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 suggests that the scratch heap space has run out.
This got rid of the too many edits in document
error and subsequently I was able to remove all the doc.Save()
entries - no more save as dialog showing.
来源:https://stackoverflow.com/questions/25360555/document-save-is-showing-saveas-dialog