Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Mailmerge and Fax / September 2003

Tip: Looking for answers? Try searching our database.

Mailmerge error 'Command Failed' in call to OpenDataSource

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
BillComer - 30 Sep 2003 13:55 GMT
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
Bill - 30 Sep 2003 16:58 GMT
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
Peter Jamieson - 01 Oct 2003 00:31 GMT
> 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.

--
Peter Jamieson
MS Word MVP

> supplementary log...
>
[quoted text clipped - 5 lines]
> Any ideas on if this is resolvable or am I going to have to
> split my merges into < 4094
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.