connecting a datasource for word merging

From: John Davy (nspam_jdavy_at_adam.com.au)
Date: 11/26/04

  • Next message: Oleg Starodumov: "Re: General dr.watson"
    Date: Fri, 26 Nov 2004 12:59:51 +1030
    
    

    I am trying to set up a mail merge by automation using a text file as the
    data source but I keep getting the message

    "Requested object is not available" and the debugger stops on the
    opendatasource line (code below)

    (I am VERY new to c# so this is a bit difficult for me at the moment)

    I am using win xp and office xp 2003 (word 11)
    I have installed the PIA for office as per Microsoft KB

    Any help would be appreciated

    Cheers
    John

    -----------------------------------------------------------------------------------------------
    private void button1_Click(object sender, System.EventArgs e)
    {
    Word.Application wrdApp;
    Word._Document wrdDoc;
    Word.MailMerge wrdMailMerge;
    Object oMissing = System.Reflection.Missing.Value;
    Object oFalse = false;
    Object oTrue = true;
    Object oFormat = 0;
    Object oNul = null;
    String cTextFile = "d:\netmerge\test.txt";
    Object oDocName = "test.doc";

    // Create an instance of Word and make it visible.
    wrdApp = new Word.Application();
    wrdApp.Visible = true;

    // Add a new document.
    wrdDoc = wrdApp.Documents.Add(ref oMissing,ref oMissing,ref oMissing,ref
    oMissing);
    wrdDoc.SaveAs(ref oDocName,ref oMissing,ref oMissing,ref oMissing,ref
    oFalse,ref oMissing,ref oMissing,ref oMissing,ref oMissing,ref oMissing,ref
    oMissing,ref oMissing,ref oMissing,ref oMissing,ref oMissing,ref oMissing);
    wrdDoc.Select();

    // Set Up Mail Merge
    wrdMailMerge = wrdDoc.MailMerge;
    wrdMailMerge.Destination = Word.WdMailMergeDestination.wdSendToPrinter;

    //Connect to data
    /* OpenDataSource parameters
     TableName ,
     Format ,
     ConfirmConversions ,
     ReadOnly ,
     LinkToSource ,
     AddToRecentFiles ,
     PasswordDocument ,
     PasswordTemplate ,
     Revert ,
     WritePasswordDocument ,
     WritePasswordTemplate ,
     Connection ,
     SQLStatement ,
     SQLStatement1 ,
     OpenExclusive ,
     SubType
    */

    //problem ooccurs here
    wrdMailMerge.OpenDataSource(cTextFile,ref oFormat,ref oFalse,ref oFalse,ref
    oTrue,ref oFalse,ref oNul,ref oNul,
        ref oFalse,ref oNul,ref oNul,ref oNul,ref oNul,ref oNul,ref oFalse,ref
    oNul) ;

    //Perform mail merge.
    wrdMailMerge.Execute(ref oFalse);

    // Close the original form document.
    wrdDoc.Saved = true;
    wrdDoc.Close(ref oFalse,ref oMissing,ref oMissing);

    // Release References.
    wrdMailMerge = null;
    wrdDoc = null;
    wrdApp = null;
    }


  • Next message: Oleg Starodumov: "Re: General dr.watson"