Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Win32:OLE and Word Mail Merge

by Gerard (Pilgrim)
on Oct 16, 2001 at 07:00 UTC ( [id://119048]=perlquestion: print w/replies, xml ) Need Help??

Gerard has asked for the wisdom of the Perl Monks concerning the following question:

Any advice / comments would be greatly appreciated... Background: I am writing a Win32::Gui application that stores contacts in flat text files and can then regroup certain ones to make a mail merge letter, amongst other things. I know that there are various other applications around to do a similar sort of thing, but Ichose to do this as there was a request for something similar at work and to take the opportunity to learn some perl. As is probably the case with most of you I find it much easier to learn when doing... Anyway, when coming to generate the Word mailmerge documnet - actually I am using an existing file and saving it with another name / location after changing the datasource. All of this works fine with the below code
my $datasource="somelocation\\somefile.cdm"; my $savelocation="somelocation\\somefile.doc"; use strict; use Win32::OLE ; use Win32::OLE::Const 'Microsoft Word'; my $Word= Win32::OLE->new('Word.Application', 'Quit'); $Word->Documents->Open($template); $Word->ActiveDocument->MailMerge->OpenDataSource($datasource,); $Word->ActiveDocument->SaveAs($savelocation);
Note: I used *.cdm as the extension as it was recomended to use something that word did not recognise so that it would not automatically assume the field and recorde delimiters. When I do this word brings up a dialog box asking me to select the delimters. "|" for field and "~" for record. Does anyone know how I can tell word what these are and thus avoid this intrusive dialog box? Look forward to hearring your wisdom. Cheers, Gerard.

Replies are listed 'Best First'.
Re: Win32:OLE and Word Mail Merge
by zakb (Pilgrim) on Oct 16, 2001 at 10:38 UTC

    Word is very picky about datasources for mail merges.

    I've done it before using CSV files by:

    • opening the CSV file using Documents.Open - Word recognises it's a CSV file; used delimiters "," and "<CR>" (no dialog, as these are standard)
    • saving the CSV as a Word document
    • using the Word document as the datasource, specifing it's a Word document in the call to MailMerge.OpenDataSource
    which actually works without any prompts. But, from your quoted delimiters I take it your not using a standard CSV file, so I doubt this would work. If you're opening a file Word does not recognise, I don't think you can get around the Word dialogs at all. Sorry ;).

      For anyone that is interested, here is my final conclusion... After much searching it seems that there is no way to dowhat I was trying. The only solution that I have come up with is to read the file, change the delimiters etc. and save it to another file. This works fine but is a little annoying as updates etc. have to be made in more than one place... But hey,... It works and I suppose that is all that really matters.
      Thanks for the information. Interesting, but doesn't seem to be avoidable as you say. Perhaps someone else will be able to shed some light. I am stumped!
Re: Win32:OLE and Word Mail Merge
by cacharbe (Curate) on Oct 16, 2001 at 16:32 UTC
    What delimiters are you using? I read the following information about the method:

    ---------------

    OpenDataSource Method
    Syntax:

    expression.OpenDataSource(Name, Format, ConfirmConversions, ReadOnly, LinkToSource, AddToRecentFiles, PasswordDocument, PasswordTemplate, Revert, WritePasswordDocument, WritePasswordTemplate, Connection, SQLStatement, SQLStatement1)

    expression Required. An expression that returns a MailMerge object.

    Name Required String. The data source file name. You can specify a Microsoft Query (.qry) file instead of specifying a data source, a connection string, and a query string.

    Format Optional Variant. The file converter used to open the document. Can be one of the following WdOpenFormat constants: wdOpenFormatAllWord, wdOpenFormatAuto, wdOpenFormatDocument, wdOpenFormatEncodedText, wdOpenFormatRTF, wdOpenFormatTemplate, wdOpenFormatText, wdOpenFormatUnicodeText, or wdOpenFormatWebPages. The default value is wdOpenFormatAuto.

    To specify an external file format, use the OpenFormat property with a FileConverter object to determine the value to use with this argument.

    ---------------

    Can you tell me the format of the file you are using (Your node mentions "|" and "~" but I wasn't sure that this was the case)? I'll then try to create a working example.

    C-.

Re: Win32:OLE and Word Mail Merge
by drinkd (Pilgrim) on Oct 16, 2001 at 16:54 UTC
    No way to do it? Sounds like a challenge :-). I can think of a couple of ways. I have had luck doing automated delimited parsing using the VBA macros in Word and Excel. Then you could automate the macro from Win32:OLE. Have you tried just sending the necessary keystrokes with Win32::CtrlGUI ? I have never tried this, and it seems kind of a kludge, but it might do the trick if there isn't other window activity going at the same time. drinkd
      In reply to the above two... I had thought of using Win32::CtrlGUI but haven't done so before. (Not that that is any reason not to) I just thought that it was quite likely to get rather ugly. Thanks for your information, I also found the same / similar information last night and this is actually what led me to my conclusion. Bearing in mind that I am new to all this... Anyway I can change the format to standard csv and this works fine... just not ideal for what I wanted. I would be interested in any solutions where I can use my existing delimiters | and ~ .

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://119048]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (4)
As of 2024-03-28 17:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found