I use the following vb.net function to purely save Word documents (no text input whatsoever, I'm only interested in mass creation of empty word documents at the moment):
Sub createDoc(ByVal cname As String, ByVal acctype As String)
counter += 1
wordDoc = wordApp.Documents.Add
wordDoc.SaveAs(OFDD.SelectedPath & "\" & cname & "_" & acctype & ".docx")
wordDoc.Close()
End Sub
The OFDD variable is the name of a folder browser vb component, and its SelectedPath property combined with the cname and acctype parameters provide me with the name of the Word document I want to create and save. Here are the declarations of the counter, wordDoc and wordApp variables:
Private Shared counter As Integer = 0
Private wordApp As New Word.Application
Private wordDoc As Word.Document
The wordDoc variable is assigned to a Document object by use of the second line of code in the subroutine createDoc. However, it appears that at the 83rd time that I'm trying to retrieve a document object and assign it to wordDoc I receive an exception stating that "Command failed" . I can tell that it's the 83rd time I enter the function because in my catchblock I print the value of counter in a message box, right after printing details about the received exception and right before I release my used resources and end the process.
Worried about whether my system has limitations related to MS Word automation, I created another Visual Studio project (a Console project, this time), referenced the Microsoft.Interop.Office.Word namespace and wrote the following simple module:
Imports Word = Microsoft.Office.Interop.Word
Module Module1
Sub Main()
Dim wordApp As New Word.Application
Try
For i As Integer = 0 To 150
Dim document As Word.Document = wordApp.Documents.Add()
document.SaveAs("C:\WordTester\" & i & ".docx")
document.Close()
Next
Catch
wordApp.Quit()
End Try
Console.WriteLine("Document objects left in memory: " & _
wordApp.Documents.Count) ' zero
Console.Read()
wordApp.Quit()
End Sub
End Module
Which works perfectly. Checking my filesystem, I see 150 word files created in "C:\WordTester". Given all these efforts of mine, I'm really baffled as to why the first code I've written gets stuck at the 83rd effort to create and save a document, and any help would be immensely appreciated.
Thank you for your time,
Jason
Edit: Here is the edited version of createDoc which I reference in a comment below:
Sub createDoc(ByVal cname As String, ByVal acctype As String)
counter += 1
wordApp = New Word.Application
wordDoc = New Word.Document
wordDoc = wordApp.Documents.Add
wordDoc.SaveAs(OFDD.SelectedPath & "\" & cname & "_" & acctype & ".docx")
wordDoc.Close()
wordApp.Quit()
End Sub