Mailmerge error 'Command Failed' in call to OpenDataSource

B

BillComer

Hi all,

I am trying to develop a mailmerge application that needs to perform
typically 10,000 merges. Trouble is I want the outputed merge to go to
a separate file for each merge. When I run the following program I get
various failures, typically Command Failed' Code: 800A10066

Any help gratefully rxd.

This occurs after a random number of merges, say after 800 to 3500.
The CSV file I am using might look like:
reference, title, firstname
01234556677, Andys long title, Andy's Fax
01204556677, A long title, file 1
01612234455, A very long title, file 2
etc....

The VB script program I call as follows:
C:\tmp>wscript multiMergeFromOneCsvFile.vbs tmp bert

And for total info overload the actual Script is:
' Test program for ListDir function.
' Lists file names using wildcards.
' Author: Christian d'Heureuse (www.source-code.biz)

Option Explicit

'Set argArray = WScript.Arguments

Main

Sub Main
Dim Path
Dim dirName
Dim jobName ' c:/<dir>/job
Dim fileList ' list of CSV files that match this job

Dim objWord 'word application object

'create and intialize the word object
Set objWord = CreateObject("Word.Application")
With objWord
.Visible = False
.ActivePrinter = "PostScriptToFile"
End With


' Select Case WScript.Arguments.Count
' Case 0: Path = "*.*" ' list current directory
' Case 1: Path = WScript.Arguments(0)
' Case Else: WScript.Echo "Invalid number of arguments.": Exit
Sub
' End Select
dirName= "c:\" + WScript.Arguments(0)
fileList= WScript.Arguments(1)
jobName = dirName + "\" + fileList
Path = jobName + "*.csv"

' WScript.Echo Path

Dim fs: Set fs = CreateObject("Scripting.FileSystemObject")
Dim csvList ' handle to CSV file
Dim tempCsvFile ' handle to temp CSV file
Dim ts ' Stream ptr to file
Dim psTextLine ' a line from the CSV file
Dim psHeader ' the forst line of the csvFile
Dim lineArray ' the line as an array
Dim faxNum ' faxNumber - the first parameter from the array
Dim tempName ' name of temp CSV file


Set csvList = fs.GetFile(jobName + ".csv")
Set ts = csvList.OpenAsTextStream(1)
psHeader = ts.ReadLine

Do While ts.AtEndOfStream <> True
Dim fsTemp: Set fsTemp =
CreateObject("Scripting.FileSystemObject")
Dim tsTemp ' Stream ptr to temp CSV file

psTextLine = ts.ReadLine
lineArray = Split( psTextLine, ",")
faxNum=lineArray(0)

' get name of Temp file
tempName = jobName + "." + faxNum
' Open for writing
Set tsTemp = fsTemp.CreateTextFile( tempName + ".csv" )
' Set tsTemp = tempCsvFile.OpenAsTextStream(2)
tsTemp.WriteLine( psHeader )
tsTemp.WriteLine( psTextLine )
tsTemp.Close
' MsgBox ( jobName + "," + tempName + ".csv" )
Call mergeTheFile ( objWord, jobName, tempName )
fsTemp.DeleteFile ( tempName + ".csv" )
Set fsTemp = Nothing
Set tsTemp = Nothing
Loop

ts.close

'Release references
Set fs = Nothing
Set csvList = Nothing
Set tempCsvFile = Nothing
Set ts = Nothing
Set psTextLine = Nothing
Set psHeader = Nothing
Set lineArray = Nothing
Set faxNum = Nothing
Set tempName = Nothing

End Sub

Sub mergeTheFile (ByVal objWord, ByVal jobName, ByVal csvFile )
Dim objTemplateDocument 'word document object
Dim objMergeDocument 'word merged document
Dim outputFile

' create and initialize a new document
' MsgBox "fileToOpen..." + theFile + ".doc"
Set objTemplateDocument = objWord.Documents.Open(jobName + ".doc")
With objTemplateDocument
.MailMerge.OpenDataSource csvFile + ".csv" 'open the data
source
.MailMerge.Execute 'perform the mail merge
.Close
End With
Set objTemplateDocument = nothing

'as we started a new instance of word, and have closed
'the template document, we can safely assume that the only
'document we have left open is the merged document.
'obtain a reference to it, save its output and close
Set objMergeDocument = objWord.Documents(1)
With objMergeDocument
outputFile = csvFile + ".ps.tmp"
' MsgBox outputFile
.PrintOut False,,,outputFile ,,,,,,,True
' .SaveAs outputFile + ".doc"
' .PrintOut False
' .Close SaveChanges:=DoNotSaveChanges
.Close (0)
End With
Set objMergeDocument = Nothing

'Release references
Set objTemplateDocument = Nothing
Set objMergeDocument = Nothing
Set outputFile = Nothing

End Sub
 
B

Bill

supplementary log...

it repeatedly falls over after 4094 merges.
This is remarkably close to 4096 to be a coincidence &
allowing for atleast one file handle for the word doc it
self it looks like I am hitting a build in limit.

Any ideas on if this is resolvable or am I going to have to
split my merges into < 4094
 
P

Peter Jamieson

Any ideas on if this is resolvable or am I going to have to
split my merges into < 4094

I can't see anything wrong with your code, but quitting Word and creating a
new Word object is certainly what I would try as a workaround. Trouble is
that if there's a memory leak or some handles left lying around that may not
solve the problem either.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top