vbams-wordruntime-errorpdf-generationword-2007

Runtime error -2147467259 during PDF generation


I'm a newbie in using VBscript and I don't understand why I get always the errorcode -2147467259 ("-2147467259: This file is in use by another application or another user")(This is my own translation because the error message is in German). I am trying to generate PDF files from a serial letter in MS Word 2007 via VBscript. There are over 28.000 PDF files to generate from the .docx-file. Maybe it could be that this great number could be the reason that the script fails but I don't hope so ;).

Here is the VB code I am using:

Sub PDFErstellung()
    Dim sBrief As String
    Dim sVertreterVerzeichnis As String
    Dim sAusgabepfad As String
    Dim iAnzahl As Integer
    Dim sKundenName As String
    Dim sError As String
    Dim iModulo As Integer

    Application.Visible = False
    sAusgabepfad = "C:\temp\"

    On Error GoTo Fehler

    With ActiveDocument.MailMerge
        .DataSource.ActiveRecord = wdLastRecord
        iAnzahl = .DataSource.ActiveRecord
        .DataSource.ActiveRecord = wdFirstRecord
        Do
            .Destination = wdSendToNewDocument
            .SuppressBlankLines = True
            With .DataSource
                .FirstRecord = .ActiveRecord
                .LastRecord = .ActiveRecord
                sVertreterVerzeichnis = sAusgabepfad & .DataFields("VertreterNr").Value
                If Not IsDiskFolder(sVertreterVerzeichnis) Then
                    MkDir sVertreterVerzeichnis
                End If
                sKundenName = CleanFilename(Left(.DataFields("KundenName").Value, 20), "")
                sBrief = sVertreterVerzeichnis & "\" & .DataFields("VertreterNr").Value & "_" & _
                            .DataFields("KundenNr").Value & "_" & sKundenName & ".pdf"
            End With
            .Execute Pause:=False
            ActiveDocument.ExportAsFixedFormat OutputFileName:=sBrief _
                , ExportFormat:=wdExportFormatPDF, OpenAfterExport:=False, OptimizeFor:= _
                wdExportOptimizeForPrint, Range:=wdExportAllDocument, From:=1, To:=1, _
                Item:=wdExportDocumentContent, IncludeDocProps:=True, KeepIRM:=True, _
                CreateBookmarks:=wdExportCreateNoBookmarks, DocStructureTags:=True, _
                BitmapMissingFonts:=True, UseISO19005_1:=False            

            If InStr(1, ActiveWindow.Caption, "Serien") = 1 Then
                ActiveWindow.Close False
            End If

            If .DataSource.ActiveRecord < iAnzahl Then
                .DataSource.ActiveRecord = wdNextRecord
            Else
                Exit Do
            End If
        Loop
    End With
    Application.Quit savechanges:=wdDoNotSaveChanges     
End Sub

The thing I can' understand is that the script aborts at different times with different numbers of created PDF files. Sometimes it stopps after ~800 files, sometimes after ~7.000 and so on. It is not felt that it had worked once! Mind, I am trying to generate 28.000 files.

Additionally I have to say that Word is using a .csv-file (7 MB) exported from a database.

Is there something I can do?

I have noticed that if I close the 'ActiveWindow' I'm able to create far more PDF file than without closing this windows. Without closing, there is a limited number of generated PDF files (I have to test it again because I can't remember this number - something about 2.000?!).

At the last attempt I implemented a sleep method which I executed after 'ActiveWindow.Close False'. At the first time it seemed to help but after three times there is only a little improvement to determine...

Have someone an idea?

(I hope that my English was not too bad as that one can not understand it...)


Solution

  • I found the reason why the script had these problems: It was the anti-virus scanner which prevented the closing of the 'ActiveWindows'. That makes sense since the error occurred accordingly irregular. Now the script can handle all 28.000 letters!

    That's it!