COM Scripts MergedMultipleFiles incorrect PDFCreatorQueue.Count


#1

Good day.

I need to merge about 60 doc files into 1 pdf document. For this, I used the example of Word - VBA MergedMultipleFiles. Why is it that I can merge 20 - 30 instead of 60. Each time the result is slightly different.
I think that the print queue is formed incorrectly, because the PDFCreatorQueue.Count variable is not equal to 60.

At first I read files from 3 folders and file names in 3 arrays Anlz(), Func(), Vnet().
Further I send documents for printing from 3 formed arrays.
Then merge all the documents in the print queue in 1.

The problem with the number of files was solved, now the PDF file is created, but the order of the files in it is broken.

Script:

Sub testPage2PDF()
Dim fullPath
Dim PDFCreatorQueue As Variant
Dim printJob As Variant
Dim Anlz() As String ' массив файлов в папке 1. Анализ системных сообщений Windows
Dim Func() As String ' массив файлов в папке 2. Проверка функционирования системного ПО
Dim Vnet() As String ' массив файлов в папке 3. Диагностика и анализ ошибок управляющей сети
' получаем количество файлов в папке
adoc = Dir(ActiveDocument.Path + "\1. Анализ системных сообщений Windows*.DOC")
n = 0
Do While adoc <> ""
n = n + 1
adoc = Dir()
Loop
' переобъявляем размеры массивов
ReDim Anlz(n)
ReDim Func(n)
ReDim Vnet(n)

Set PDFCreatorQueue = CreateObject("PDFCreator.JobQueue")
fullPath = ActiveDocument.Path & "\Отчет.pdf "

MsgBox "Initializing PDFCreator queue..."
PDFCreatorQueue.Initialize
On Error GoTo RelaseCom
If (1 < 0) Then
RelaseCom:
PDFCreatorQueue.ReleaseCom
MsgBox "gfgf"
End If
'Set printer if necessary
Application.ActivePrinter = "PDFCreator"

MsgBox "Printing the current active document..."
' получаем титульный лист
tit = Dir(ActiveDocument.Path + "*тит*.DOC")
Application.PrintOut Background:=False, FileName:=(ActiveDocument.Path + "" + tit)

' считываем имена файлов в папке 1. Анализ системных сообщений Windows
adoc = Dir(ActiveDocument.Path + "\1. Анализ системных сообщений Windows*.DOC")
i = 0
Do While adoc <> ""
Anlz(i) = adoc
i = i + 1
adoc = Dir()
Loop

' считываем имена файлов в папке 2. Проверка функционирования системного ПО
adoc = Dir(ActiveDocument.Path + "\2. Проверка функционирования системного ПО*.DOC")
i = 0
Do While adoc <> ""
Func(i) = adoc
i = i + 1
adoc = Dir()
Loop

' считываем имена файлов в папке 3. Диагностика и анализ ошибок управляющей сети
adoc = Dir(ActiveDocument.Path + "\3. Диагностика и анализ ошибок управляющей сети*.DOC")
i = 0
Do While adoc <> ""
Vnet(i) = adoc
i = i + 1
adoc = Dir()
Loop

' выводим на печать файлы
For i = 0 To n - 1
Application.PrintOut Background:=False, FileName:=(ActiveDocument.Path + "\1. Анализ системных сообщений Windows" + Anlz(i))
Application.PrintOut Background:=False, FileName:=(ActiveDocument.Path + "\2. Проверка функционирования системного ПО" + Func(i))
Application.PrintOut Background:=False, FileName:=(ActiveDocument.Path + "\3. Диагностика и анализ ошибок управляющей сети" + Vnet(i))
Next i

MsgBox "Waiting for the job to arrive at the queue..."
If Not PDFCreatorQueue.WaitForJobs((n * 3 + 1), 35) Then
MsgBox "The print job did not reach the queue within " & " 35 seconds"
Else
MsgBox "Currently there are " & PDFCreatorQueue.Count & " job(s) in the queue"
MsgBox "Getting job instance"

MsgBox "Merging all available jobs now"
PDFCreatorQueue.MergeAllJobs

Set printJob = PDFCreatorQueue.NextJob

printJob.SetProfileByGuid ("DefaultGuid")

MsgBox "Converting under ""DefaultGuid"" conversion profile"
printJob.ConvertTo (fullPath)

If (Not printJob.IsFinished Or Not printJob.IsSuccessful) Then
    MsgBox "Could not convert the file: " & fullPath
Else
    MsgBox "Job finished successfully"
End If

End If

MsgBox "Releasing the object"
PDFCreatorQueue.ReleaseCom
End Sub

Please, help.


#2

Please use https://gist.github.com or https://codepad.co/ to share your script. (Or copy&paste it in your post - you can use the hide details option for example)

For example
class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine("Hello World!");

    }
}