I have written a C# application to produce tickler letters but can't close the documents without error

0

I have created a c# application to produce tickler letters. My intent is to have the program create and print the letters then close word without needing any user input. If I use letter1.Close(); an exception is thrown when the second letter is created. If I don't use the close statement, then the user has to respond to a save dialog for each letter. I don't want to save the letters and want to avoid this as the process can produce dozens of letters. Any suggestions? I am using visual studio 2015 and word 2010. The letter1.Close(); throws the exception: The object invoked has disconnected from its clients. (Exception from HRESULT: 0x80010108 (RPC_E_DISCONNECTED))

TIA - Linda

var application = new Microsoft.Office.Interop.Word.Application();
var template1 = new Microsoft.Office.Interop.Word.Document();
var letter1 = new Microsoft.Office.Interop.Word.Document();
application.Visible = true;

//CreateMailMergeDataFile();
string ConnectionName = "TRUEIBM";
iDB2Connection conn = new iDB2Connection(connectionString);
conn.Open();
iDB2Command cmd = conn.CreateCommand();
string sqlStmt = Scripts.sqlGetVendorsAbouttoExpire;
cmd.CommandText = sqlStmt;
DataSet ds = new DataSet();
iDB2DataAdapter adapter = new iDB2DataAdapter();
adapter.SelectCommand = new iDB2Command(sqlStmt, conn);
adapter.Fill(ds);
cmd.Dispose();
conn.Close();

string l_vendorName;
string l_space = new string(' ', 10);
string l_vendor_no_n6;
string l_date = DateTime.Now.ToString("MMMM dd, yyyy");

object doNotSaveChanges = WdSaveOptions.wdDoNotSaveChanges;
object oMissing = System.Reflection.Missing.Value; 

List<VendortoExpireModel> vendorstoExpireList =
    new List<VendortoExpireModel>();

foreach (DataRow dr in ds.Tables[0].Rows)
    {
        VendortoExpireModel VendorstoExpire = new VendortoExpireModel();
        VendorstoExpire.Vendor_No_N6 = (dr "Vendor_No_N6"]);
        l_vendor_no_n6 = Convert.ToString(dr["Vendor_No_n6"]);
        VendorstoExpire.Vendor_Name = dr["Vendor_Name"].ToString();
        l_vendorName = dr["Vendor_Name"].ToString().Trim();
        VendorstoExpire.Address1 = dr["Address1"].ToString().Trim();
        VendorstoExpire.Address2 = dr["Address2"].ToString().Trim();
        VendorstoExpire.City = dr["City"].ToString().Trim();
        VendorstoExpire.State_Abbr = dr["State_Abbr"].ToString().Trim();
        VendorstoExpire.Zip = dr["Zip"].ToString().Trim();
        vendorstoExpireList.Add(VendorstoExpire);

        // Perform mail merge.
        template1 = application.Documents.Add = blah         

        foreach (Microsoft.Office.Interop.Word.Field field in template1.Fields)
            {
                if(field.Code.Text.Contains("Vendor_Name"))
                {
                    field.Select();
                    application.Selection.TypeText(VendorstoExpire.Vendor_Name);
                }
                else if(field.Code.Text.Contains("Address1") && VendorstoExpire.Address1 != "")
                {
                    field.Select();
                    application.Selection.TypeText(VendorstoExpire.Address1);
                }        

        //send to printer
        letter1.PrintOut(); 
        letter1.Close();   
}
c#
exception
ms-word
asked on Stack Overflow Jun 6, 2018 by LindaT • edited Jun 7, 2018 by LindaT

1 Answer

0

Note: Although the question is old, writing this answer just to help if anyone come across the same issue.

In my case, this exception is being thrown in case of performing any operation (e.g. method invocation, property access) on the already closed Document instance. e.g.

        var application = new Microsoft.Office.Interop.Word.Application();
        application.Visible = false;
        application.DisplayAlerts = WdAlertLevel.wdAlertsNone;
        object oFalse = false;

        var document = application.Documents.Open(ref fileName);
        document.Close();

        var format = document.AutoFormatOverride; --> This line throws the exception as the document instance is being accessed after closing it (i.e. may be disconnected already from the MS Word client)
        document.PrintOut(); --> This line would throw the exception if we skip the above line.

In the OP, this could be caused by the below line for the second letter since the same Document instance is used within the loop which would have been already closed in the 1st iteration.

letter1.PrintOut();
answered on Stack Overflow Feb 12, 2020 by Sivaram K

User contributions licensed under CC BY-SA 3.0