Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

VB.NET program unable to open source file

Status
Not open for further replies.

FiMacAU

Programmer
Feb 25, 2009
2
0
0
US
Hi all,
I've got a simple mailmerge program written in VB.NET that is used to read data from a plain text input file and create one of five formatted mailing label types. I recently had to update the references to the Office Interop assemblies to allow the program to be run on a new server running Windows Server 2008 and Office 2007. (Previously the program was running on Windows Server 2003 and Offiec 2003).
The program runs successfully from a command line, however, we need to schedule its execution for daily runs via our APPWORX scheduling tool.
When run as a job from appworx, as a privileged domain user I get the following error:

Unhandled Exception: System.Runtime.InteropServices.COMException (0x800A1722): Word was unable to open the data source.
at Microsoft.Office.Interop.Word.MailMerge.OpenDataSource(String Name, Object& Format, Object& ConfirmConversions, Object& ReadOnly, Object& LinkToSource, Object& AddToRecentFiles, Object& PasswordDocument, Object& PasswordTemplate, Object& Revert, Object& WritePasswordDocument, Object& WritePasswordTemplate, Object& Connection, Object& SQLStatement, Object& SQLStatement1, Object& OpenExclusive, Object& SubType)

I know this is potentially a "proprietary" issue, but any help would be greatly appreciated.
My program code is shown below.
Thanks in advance for any help.

Imports Microsoft.Office.Core
Imports Word = Microsoft.Office.Interop.Word

Module Module1

Sub Main()

Dim args() As String
Dim source As String
Dim dest As String
Dim mtype As String
Dim strState As String
Dim strNatn As String
Dim oApp As Word.ApplicationClass
Dim oDoc As Word.DocumentClass

'Parse the command parameters
args = Split(Command(), " ")
'Check to see if there are enough parameters coming on
If args.Length < 3 Then
Console.WriteLine("Not enough parameters passed")
Console.WriteLine("Usage: MailMerge.exe path_to_datasource path_to_sourcefile MergeFormat")
Exit Sub
End If
'Set the parameter variables
source = args(0)
dest = args(1)
mtype = args(2)

'Start a new document in Word
oApp = CreateObject("Word.Application")
oDoc = oApp.Documents.Add
With oDoc.MailMerge
'Insert the mail merge fields temporarily so that
'you can use the range containing the merge fields as a layout
'for your labels -- to use this as a layout, you can add it
'as an AutoText entry.
If mtype = "1" Then
With .Fields
.Add(oApp.Selection.Range, "ZZ_FIRST_MI")
oApp.Selection.TypeText(" ")
.Add(oApp.Selection.Range, "ZZ_LAST_SUFFIX")
oApp.Selection.TypeParagraph()
.Add(oApp.Selection.Range, "STR1")
oApp.Selection.TypeParagraph()
.Add(oApp.Selection.Range, "STR2")
oApp.Selection.TypeParagraph()
.Add(oApp.Selection.Range, "STR3")
oApp.Selection.TypeParagraph()
.Add(oApp.Selection.Range, "CITY")
oApp.Selection.TypeText(", ")
.Add(oApp.Selection.Range, "STATE")
oApp.Selection.TypeText(" ")
.Add(oApp.Selection.Range, "ZIPC")
oApp.Selection.TypeParagraph()
.Add(oApp.Selection.Range, "NATN")
End With
End If
If mtype = "2" Then
With .Fields
.Add(oApp.Selection.Range, "HSNAME")
oApp.Selection.TypeParagraph()
.Add(oApp.Selection.Range, "HS_STR1")
oApp.Selection.TypeParagraph()
.Add(oApp.Selection.Range, "HS_STR2")
oApp.Selection.TypeParagraph()
.Add(oApp.Selection.Range, "HS_STR3")
oApp.Selection.TypeParagraph()
.Add(oApp.Selection.Range, "HS_CITYSTATEZIP")
End With
End If

If mtype = "3" Then
With .Fields
.Add(oApp.Selection.Range, "ZZ_LAST_SUFFIX")
oApp.Selection.TypeText(", ")
.Add(oApp.Selection.Range, "FNAME")
oApp.Selection.TypeText(" ")
.Add(oApp.Selection.Range, "MNAME")
oApp.Selection.TypeParagraph()
.Add(oApp.Selection.Range, "ADMTYP")
oApp.Selection.TypeText(" ")
.Add(oApp.Selection.Range, "APPLTERM")
oApp.Selection.TypeParagraph()
.Add(oApp.Selection.Range, "MAJORCODE")
oApp.Selection.TypeText(" ")
.Add(oApp.Selection.Range, "COUNSELOR_NAME")
End With
End If

If mtype = "4" Then
With .Fields
oApp.Selection.TypeText(" ")
oApp.Selection.TypeText(" ")
.Add(oApp.Selection.Range, "COUNSELORCODE")
oApp.Selection.TypeParagraph()
oApp.Selection.TypeText(" ")
.Add(oApp.Selection.Range, "ZZ_LAST_SUFFIX")
oApp.Selection.TypeText(", ")
.Add(oApp.Selection.Range, "FNAME")
oApp.Selection.TypeText(" ")
.Add(oApp.Selection.Range, "MNAME")
oApp.Selection.TypeParagraph()
oApp.Selection.TypeText(" ")
.Add(oApp.Selection.Range, "ID")
oApp.Selection.TypeText(" ")
.Add(oApp.Selection.Range, "APPLTERM")
oApp.Selection.TypeParagraph()
oApp.Selection.TypeText(" ")
End With
End If

If mtype = "5" Then
With .Fields
.Add(oApp.Selection.Range, "ZZ_LAST_SUFFIX")
oApp.Selection.TypeText(", ")
.Add(oApp.Selection.Range, "FNAME")
oApp.Selection.TypeText(" ")
.Add(oApp.Selection.Range, "MNAME")
oApp.Selection.TypeParagraph()
.Add(oApp.Selection.Range, "ID")
oApp.Selection.TypeText(" ")
.Add(oApp.Selection.Range, "ADMTYP")
oApp.Selection.TypeParagraph()
.Add(oApp.Selection.Range, "MAJORCODE")
oApp.Selection.TypeText(" ")
.Add(oApp.Selection.Range, "APPLTERM")
oApp.Selection.TypeParagraph()
oApp.Selection.TypeParagraph()
End With
End If

Dim oAutoText As Word.AutoTextEntry
oAutoText = oApp.NormalTemplate.AutoTextEntries.Add("MyLabelLayout", oDoc.Content)
oDoc.Content.Delete() 'Merge fields in document no longer needed now
'that the AutoText entry for the label layout
'has been added so delete it.

'Set up the mail merge type as mailing labels and use
'a tab-delimited text file as the data source.
.MainDocumentType = Word.WdMailMergeMainDocType.wdMailingLabels
.OpenDataSource(Name:=source) 'Specify your data source here

'Create the new document for the labels using the AutoText entry
'you added -- 5160 is the label number to use for this sample.
'You can specify the label number you want to use for the output
'in the Name argument.

oApp.MailingLabel.CreateNewDocumentByID(LabelID:="1359804671", _
Address:="", AutoText:="MyLabelLayout", LaserTray:= _
Word.WdPaperTray.wdPrinterManualFeed, ExtractAddress:=False, PrintEPostageLabel:=True, _
Vertical:=False)

'Execute the mail merge to generate the labels.
.Destination = Word.WdMailMergeDestination.wdSendToNewDocument
.Execute()

'Delete the AutoText entry you added
oAutoText.Delete()

End With

'Close the original document and make Word visible so that
oDoc.Close(False)
'the mail merge results are displayed
oApp.Visible = False

'Prevent save to Normal template when user exits Word
oApp.NormalTemplate.Saved = True
oApp.Documents.Item(1).SaveAs(dest)

'Find and replace , after city when foreign addr
oApp.Documents.Open(FileName:=dest, _
ConfirmConversions:=False, _
ReadOnly:=True, AddToRecentFiles:=False, _
PasswordDocument:="", _
PasswordTemplate:="", Revert:=False, _
WritePasswordDocument:="") ', _

' Search for the target string.
With oApp.ActiveWindow.Selection.Find
.ClearFormatting()
.Text = ", "
.Replacement.Text = ""
.Forward = True
.Wrap = Word.WdFindWrap.wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = True
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
.Execute()
End With

oApp.Documents.Save()
'oApp.ActiveDocument.Close()
oApp.Quit()

Console.WriteLine("Word doc created and stored at " & dest)

End Sub

End Module
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top