0

I have 41 large Word documents (Titles) that I am trying to create using InterOp.

There are about 700 chapters in total. Each of the 41 gets some of the chapters.

There are a total of about 31,000 paragraphs that are spread out among the chapters.

In Visual Basic .Net I:

  1. Create a title
  2. Create a chapter and add the appropriate paragraphs to it.
  3. Then I add the chapter to the title.
  4. If there are more chapters for the title GoTo 2
  5. Else Save the title
  6. If there are more titles GoTo 1

The application runs as expected, except... After about Title 21 the documents do not appear in the folder where I am saving them.

Today I watched this process run. When I noticed that I was on title 23 but the folder only had Title 21 I put a break point on the line that saved the title to check if the path or file name were corrupted. They weren't. I executed the line with the save and went to the folder and refreshed it. Not there.

Next run I started with the first title missing and once again it ran for a while with no problems, but then later did the same thing.

I have no clue of what to do next.

edit:

Cindy - Some things I can answer right now. VS2012, Office 2010. The target folder is on a network drive. The code is in a Try-Catch, but as I pointed out in the post the Save executes. I do suspect that it is some cleanup issue. I modified the code to quit word after each title to see if that helps. The big problem is that this is hours of processing before the failure occurs. I started the new version before I left work and won't know if the changes helped until Monday. If I am still having the issue on Monday I will post the bare bones code then.

edit: I am making progress, but still have errors sometimes. The code that is called over 30,000 times looks like this

Private Sub CreateWordDoc(fileToOpen As String, savePath As String)

    '  Exit Sub 'test ' 

    If MSWord Is Nothing Then
        MSWord = New Word.Application
        MSWord.Visible = False
    End If
    '
    Dim doc As Word.Document = MSWord.Documents.Open(FileName:=fileToOpen.ToString, Visible:=False, Format:=Word.WdOpenFormat.wdOpenFormatWebPages, AddToRecentFiles:=False)
    '
    Threading.Thread.Sleep(x80010001FixSleepVal)
    '
    'speed up word
    With doc.Application
        .Options.Pagination = False
        .ScreenUpdating = False
    End With

    'set up find
    Dim fnd As Word.Find = doc.Content.Find
    fnd.Format = True
    fnd.Forward = True
    fnd.Wrap = Word.WdFindWrap.wdFindContinue

    showStatus("Formatting tabs")
    fnd.Text = XStructFormatFor.tabCHinDoc
    fnd.Replacement.Text = ControlChars.Tab

    Dim found As Boolean = fnd.Execute(Replace:=Word.WdReplace.wdReplaceAll)
    '
    Threading.Thread.Sleep(x80010001FixSleepVal)
    '
    showStatus("Formatting spaces")
    fnd.Text = XStructFormatFor.wordNBSPch
    fnd.Replacement.Text = " "
    found = fnd.Execute(Replace:=Word.WdReplace.wdReplaceAll)
    '
    Threading.Thread.Sleep(x80010001FixSleepVal)
    '
    fnd = Nothing

    doc.SelectAllEditableRanges()
    doc.Range.Copy()
    '
    Threading.Thread.Sleep(x80010001FixSleepVal)
    '
    doc.Close(SaveChanges:=Word.WdSaveOptions.wdDoNotSaveChanges)
    '
    doc = Nothing

    'template with macros
    Dim ofile As String = IO.Path.Combine(IO.Path.GetDirectoryName(pthExec), "DocTempl.dotm")
    docTmpl = MSWord.Documents.Open(ofile.ToString, AddToRecentFiles:=False)
    '
    Threading.Thread.Sleep(x80010001FixSleepVal)
    '
    'speed up word
    With docTmpl.Application
        .Options.Pagination = False
        .ScreenUpdating = False
    End With

    docTmpl.SelectAllEditableRanges()
    showStatus("Macros")
    '
    Threading.Thread.Sleep(x80010001FixSleepVal)
    '
    docTmpl.Range.PasteAndFormat(Word.WdRecoveryType.wdFormatOriginalFormatting)
    MSWord.Visible = True
    'macros run when visible
    Try
        docTmpl.Windows(1).WindowState = Word.WdWindowState.wdWindowStateNormal
        'this code and the associaed macro are
        'to make lines in table be 1/4 pt.
        showStatus("Formatting Table - check Word status bar")
        docTmpl.Application.Run("TableLines")
        '
        Threading.Thread.Sleep(x80010001FixSleepVal)
        '
        showStatus("Formatting Paragraphs")
        docTmpl.Application.Run("SetParaNorm")
        '
        Threading.Thread.Sleep(x80010001FixSleepVal)
        '
        showStatus("Formatting Paragraphs Common")
        docTmpl.Application.Run("SetParaCommon")
        '
        Threading.Thread.Sleep(x80010001FixSleepVal)
        '
        '
        MSWord.Visible = False
        docTmpl.SaveAs2(FileName:=savePath.ToString, FileFormat:=Word.WdSaveFormat.wdFormatDocumentDefault, AddToRecentFiles:=False)
        docTmpl.Close(Word.WdSaveOptions.wdDoNotSaveChanges)
        docTmpl = Nothing
        If tempFilesL.Count > 500 Then
            CloseMSWord(True)
            TempFileLimit()
        End If
        showStatus("")
    Catch ex As Exception
        AddActivity("ERROR --->  " & ex.Message)
        thisApp.GALog.WriteException(ex, TraceEventType.Error, "Macro error")
    End Try
End Sub

The thread sleeps were from an article I found, and they do appear to help. Here are some refs.

'Call was rejected by callee. (Exception from HRESULT: 0x80010001 (RPC_E_CALL_REJECTED)). 
'happens occasionally
'see - http://stackoverflow.com/questions/20638978/call-was-rejected-by-callee-exception-from-hresult-0x80010001-rpc-e-call-rej
'and
'http://blogs.artinsoft.net/Mrojas/archive/2012/09/28/Office-Interop-and-Call-was-rejected-by-callee.aspx

Today I made it through about 10,000 sections before I had any error.

If you follow all the links, for all the titles and chapters here, Revised Statutes you will get an idea of what I am doing. FWIW - two weeks ago I could barely spell Interop, and now I am trying to produce documents that will literally fill 20 Stephen King size novels.

There is a loop on the outside that feeds the code you see. It takes care of releasing the Word app. Two weeks ago the first issue I uncovered and fixed was having multiple instances of the Word app. Today there is only one instance of Word running at anytime.

If you notice there is a commented Exit Sub at the beginning. This morning I ran it with the Exit Sub un-commented for several hours to make certain that the code I posted caused the problem. It ran through the list several times without a problem. It was when I commented it out, that it threw an exception after about 10,000 sections. The section processed later without issue.

I have ran this locally and using a network drive, same problem.

Deduplicator
  • 44,692
  • 7
  • 66
  • 118
dbasnett
  • 11,334
  • 2
  • 25
  • 33
  • It's impossible to trouble-shoot this without knowing a lot more about what you're doing... For starters: I'd like to see the "bare bones" code: instatiating the objects, saving, cleaning up the objects. Where's the target folder located (network, harddrive, removable media...)? Which version of Word? Is your code in a try...catch block? – Cindy Meister Mar 04 '16 at 16:22
  • @CindyMeister - see edits. – dbasnett Mar 04 '16 at 23:59
  • Since network is involved you also have to take into account that the network connection may be "hiccuping". Quitting Word is not usually a good idea. Actually, it might be a good idea for you to look into generating these documents directly as OOXML files, rather than using the Word interop. That file format was designed for this kind of thing; automating Word was *not*. – Cindy Meister Mar 07 '16 at 17:43
  • @CindyMeister - It dawned on me that I can provide an example and help debug this darned thing. I'll post later if you are still interested. – dbasnett Mar 08 '16 at 12:23
  • Sure. Although I can't repro your network environment. – Cindy Meister Mar 08 '16 at 18:08
  • @CindyMeister - see edits above. – dbasnett Mar 09 '16 at 11:21
  • Sorry, lost track of this for some reason... It's possible that you're running into Word not being able to continue to handle all the editing changes, although that usually manifests itself in a message to the user. Word. I see you have a "clean up" for temp files, but no idea what is actually going on here? Do you at some point empty the Undo list? – Cindy Meister May 15 '16 at 18:56

0 Answers0