problem automating catalog merge using vb.net


C

Chris Wold

I am trying to automate a catalog merge from vb.net using the following code,
which I will explain below:

Public Sub PublisherExecuteMerge(ByVal fPath As String)
'Execute catalog merge in publisher
Dim fInfo As System.IO.FileInfo
Dim pubApp As Publisher.Application = Nothing
Dim pubDoc As Publisher.Document = Nothing
Dim mergeDoc As Publisher.MailMerge = Nothing
Try
pubApp = New Publisher.Application
pubApp.Open(Filename:=fPath, _
ReadOnly:=False, AddToRecentFiles:=False, _
SaveChanges:=Publisher.PbSaveOptions.pbDoNotSaveChanges)
pubApp.ActiveWindow.Visible = True
'Add a new docuemnt
pubDoc = pubApp.Documents.Add
mergeDoc = pubDoc.MailMerge
mergeDoc.Execute( _
True, _
Publisher.PbMailMergeDestination.pbMergeToNewPublication, _
"C:\temp\foo.pub")
Catch ex As Exception
MsgBox(ex.Message)
Finally
pubDoc = Nothing
mergeDoc = Nothing
pubApp = Nothing
GC.Collect()
End Try
End Sub

The full path to the template doc is given in "fPath". The template already
contains a catalog merge area, merge fields, and is linked to an ASCII
(tab-delimited text file) datasource. If I use publisher, I can open the
template and using the wizard, get the correct result and save it to a new
document. However, I need to do the same thing in vb.net. So what happens
when I run this sub is that the template is opened in Publisher and a new doc
is created. However the second document is empty and I get this error:
"System.ArgumentException: Value does not fall within the expected range"
which occurs when the sub attempts to execute the mail merge.

Can anyone help me with this?

Thank you,
Chris
 
Ad

Advertisements

C

Chris Wold

Never mind, I figured it out (using VBA in Publisher) and will share if
anyone is interested...
 
C

che

Hi Chris,

I'm desperate for a similar solution. Did you manage to find one? I'm having
trouble opening the newly merged document within the word application control
instance hosted in my application. I can't seem to get this line to work:

mergeDoc = pubDoc.MailMerge

Any help would be greatly appreciated!

Thanks.
 
Ad

Advertisements

C

Chris Wold

Hi Che,

This following code works for Publisher. Are you trying to merge using a
Word template? I need to do that as well in the next week or so. I would
appreciate it if you let me know what works for you in Word.

Thanks,
Chris

Public Sub PublisherExecuteMerge(ByVal fPath As String)
'Execute catalog merge in publisher
Dim fInfo As System.IO.FileInfo
Dim pubApp As Publisher.Application = Nothing
Dim destDoc As Publisher.Document = Nothing
Dim mergeDoc As Publisher.MailMerge = Nothing
Try
pubApp = New Publisher.Application
pubApp.Open( _
Filename:=fPath, _
ReadOnly:=True, _
AddToRecentFiles:=False, _
SaveChanges:=Publisher.PbSaveOptions.pbDoNotSaveChanges)
'Don't show the input file (template)
pubApp.ActiveWindow.Visible = False
' "mergeDoc" is the catalog merge that is defined in the input file
mergeDoc = pubApp.ActiveDocument.MailMerge
' "destDoc" contains the result of the merge and will be displayed in
' a new window.
destDoc = mergeDoc.Execute( _
False, _
Publisher.PbMailMergeDestination.pbMergeToNewPublication)
'Show the result of the mail merge
destDoc.Application.ActiveWindow.Visible = True
Catch ex As Exception
Throw New Exception(ex.Message)
Finally
destDoc = Nothing
mergeDoc = Nothing
pubApp = Nothing
GC.Collect()
End Try
 

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