Click here to Skip to main content
15,881,588 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
hi - I have a helpdesk system and using a .doc file as a template I am allowing ussers to gernerate a create a new instance of this file with specific details related to the call in question. After saving this file I'd like to display it in a new window to the user but I'm not sure how to do this. Can anyone help me please?
C#
protected void btnGenerateCallSheet_Click(object sender, EventArgs e)
        {
            lblGenerateDocError.Visible = false;

            Word.Application wrdApp = new Word.Application();
            Word._Document wrdDoc;
            Object oMissing = System.Reflection.Missing.Value;
            string strTemplateLocation = "";

            try
            {
                Object oFalse = false;
                Object templatePath;
                Object oTrue = true;
                Object oBookMark;

                strTemplateLocation = @ConfigurationManager.AppSettings["Word_Template_Location"] + "CallSheetNew.doc";

                wrdApp.Visible = true;
                templatePath = (string)strTemplateLocation;
                wrdDoc = wrdApp.Documents.Add(ref templatePath, ref oMissing, ref oMissing, ref oMissing);

                oBookMark = "Callers_Tel";
                wrdDoc.Bookmarks.Item(ref oBookMark).Range.Text = txtCallerTel.Text;

                oBookMark = "Call_Date";
                wrdDoc.Bookmarks.Item(ref oBookMark).Range.Text = Convert.ToDateTime(lblCallDateValue.Text).ToString("d");

                oBookMark = "Call_Operator";
                wrdDoc.Bookmarks.Item(ref oBookMark).Range.Text = lblCallTakenByValue.Text;

                //Inserts the Authorisation Number with the appended Invoice number
                oBookMark = "Call_AuthNum";
                //wrdDoc.Bookmarks.Item(ref oBookMark).Range.Text = cRec.AuthNumber;
                wrdDoc.Bookmarks.Item(ref oBookMark).Range.Text = lblAuthNumberValue.Text;

                oBookMark = "ContactBeforeArriving";
                wrdDoc.Bookmarks.Item(ref oBookMark).Range.Text = ddlCallCustomer.SelectedItem.Text;

                oBookMark = "PreferredDay";
                //wrdDoc.Bookmarks.Item(ref oBookMark).Range.Text = ddlPreferredDay.SelectedItem.Text;
                StringBuilder strpreferredday = new StringBuilder();
                foreach (ListItem cBox in cblPreferredDay.Items)
                {
                    if (cBox.Selected)
                    {
                        strpreferredday.AppendLine(cBox.Text);
                    }
                }

                wrdDoc.Bookmarks.Item(ref oBookMark).Range.Text = strpreferredday.ToString().Replace("<br>", " ");

                oBookMark = "PreferredTime";
                wrdDoc.Bookmarks.Item(ref oBookMark).Range.Text = ddlPreferredTime.SelectedItem.Text;

                string strAddress = String.Empty;

                if (txtCustomerStreet.Text.Length > 0) strAddress = txtCustomerStreet.Text + "\n";
                if (txtCustomerPostcode.Text.Length > 0) strAddress += txtCustomerPostcode.Text + " ";
                if (txtCustomerTown.Text.Length > 0)
                    strAddress += txtCustomerTown.Text + "\n";
                else
                    strAddress += "\n";
                if (txtCustomerCountry.Text.Length > 0) strAddress += txtCustomerCountry.Text;

                oBookMark = "Cust_Address";
                wrdDoc.Bookmarks.Item(ref oBookMark).Range.Text = strAddress;

                oBookMark = "Cust_TelHome";
                wrdDoc.Bookmarks.Item(ref oBookMark).Range.Text = "(H):" + txtCustomerTelHome.Text;

                oBookMark = "Cust_TelMobile";
                wrdDoc.Bookmarks.Item(ref oBookMark).Range.Text = "(M):" + txtCustomerTelMobile.Text;

                oBookMark = "Cust_TelWork";
                wrdDoc.Bookmarks.Item(ref oBookMark).Range.Text = "(W):" + txtCustomerTelWork.Text;

                oBookMark = "Cust_Notes";
                wrdDoc.Bookmarks.Item(ref oBookMark).Range.Text = txtCustomerNotes.Text;

                string strVisitingAddress = String.Empty;
                if (txtVisitingStreet.Text.Length > 0)
                {
                    if (txtVisitingStreet.Text.Length > 0) strVisitingAddress = txtVisitingStreet.Text + "\n";
                    if (txtVisitingPostCode.Text.Length > 0) strVisitingAddress += txtVisitingPostCode.Text + " ";
                    if (txtVisitingTown.Text.Length > 0)
                        strVisitingAddress += txtVisitingTown.Text + "\n";
                    else
                        strVisitingAddress += "\n";
                    if (txtVisitingCountry.Text.Length > 0) strVisitingAddress += txtVisitingCountry.Text;
                }
                else
                {
                    strVisitingAddress = "See Customer's Home Address.";
                }

                oBookMark = "Cust_VisitingAddress";
                wrdDoc.Bookmarks.Item(ref oBookMark).Range.Text = strVisitingAddress;

                oBookMark = "Engineer";
                wrdDoc.Bookmarks.Item(ref oBookMark).Range.Text = ddlEngineers.SelectedItem.Text;

                oBookMark = "Model_Number";
                wrdDoc.Bookmarks.Item(ref oBookMark).Range.Text = txtModelName.Text;

                oBookMark = "Model_Quantity";
                wrdDoc.Bookmarks.Item(ref oBookMark).Range.Text = txtQuantity.Text;

                oBookMark = "Model_Serial";
                wrdDoc.Bookmarks.Item(ref oBookMark).Range.Text = txtSerialNumber.Text;

                oBookMark = "Model_PurchasedFrom";
                wrdDoc.Bookmarks.Item(ref oBookMark).Range.Text = txtPurchasedFrom.Text;

                if (txtPurchaseDate.Text.Length > 0)
                {
                    if (!Convert.ToDateTime(txtPurchaseDate.Text).ToString("d").Equals("01/01/0001"))
                    {
                        oBookMark = "Model_DateOfPurchase";
                        wrdDoc.Bookmarks.Item(ref oBookMark).Range.Text = Convert.ToDateTime(txtPurchaseDate.Text).ToString("d");
                    }
                }
                //Section Removed from CallSheetNew
                //oBookMark = "Model_AdditionalNotes";
                //wrdDoc.Bookmarks.Item(ref oBookMark).Range.Text = txtAdditionalNotesModel.Text;

                oBookMark = "Callers_Name";
                wrdDoc.Bookmarks.Item(ref oBookMark).Range.Text = txtCallerForename.Text + " " + txtCallerSurname.Text;

                oBookMark = "Cust_Name";
                wrdDoc.Bookmarks.Item(ref oBookMark).Range.Text = txtCustomerForename.Text + " " + txtCustomerSurname.Text;

                oBookMark = "Model_ReportedFault";
                wrdDoc.Bookmarks.Item(ref oBookMark).Range.Text = ddlReportedFaults.SelectedItem.Text;

                oBookMark = "Resolution";
                wrdDoc.Bookmarks.Item(ref oBookMark).Range.Text = ddlActions.SelectedItem.Text;

                oBookMark = "Who_Pays";
                wrdDoc.Bookmarks.Item(ref oBookMark).Range.Text = rblWhoPays.SelectedItem.Text;

                wrdDoc = wrdApp.ActiveDocument;

                string strFolderPath = @ConfigurationManager.AppSettings["Upload_File_Location"] + lblAuthNumberValue.Text;
                string strFileName = "CallSheet - " + lblAuthNumberValue.Text + ".doc";

                if (!Directory.Exists(strFolderPath))
                {
                    //Creates the new folder if it doesn't already exist
                    Directory.CreateDirectory(strFolderPath);
                }

                int file_append = 0;
                while (File.Exists(strFolderPath + @"\" + strFileName))
                {
                    //Numerically appends the file name to avoid overwriting an existing file
                    file_append++;
                    strFileName = "CallSheet - " + lblAuthNumberValue.Text + "_" + file_append.ToString() + ".doc";
                }
                Object filename;
                filename = (string)strFolderPath + @"\" + strFileName;
                wrdDoc.SaveAs(ref filename, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);               

                if (!ServiceDB.insCallRecordHistory(lblAuthNumberValue.Text, "Call Sheet Generated", Session["Username"].ToString()))
                    throw new Exception("Call Sheet Saved but cannot update call record history");
            }
            catch (Exception ex)
            {
                lblGenerateDocError.Visible = true;
                lblGenerateDocError.Text = "ERROR: Cannot generate call sheet - " + ex.Message + "---" + strTemplateLocation;
            }
            finally 
            {
                if (wrdApp != null)
                {
                    Object oSaveChanges;
                    oSaveChanges = false;
                    ((_Application)wrdApp).Quit(ref oSaveChanges, ref oMissing, ref oMissing);
                    wrdApp = null;
                }
            }
        }
Posted

1 solution

i've had to do a couple of things to fix this, the end of my code now looks like this:
C#
finally
            {
                if (wrdApp != null)
                {
                    Object oSaveChanges;
                    oSaveChanges = false;
                    ((_Application)wrdApp).Quit(ref oSaveChanges, ref oMissing, ref oMissing);
                    wrdApp = null;
                }

                OpenFileForUser(strFolderPath, strFileName);
            }
        }

        protected void OpenFileForUser(string strFolderPath, string strFileName)
        {
            Response.Clear();
            Response.ContentType = "application/ms-word";
            Response.AddHeader("Content-disposition", "inline; filename=" + strFileName);
            String path = strFolderPath + @"\" + strFileName; 
            Response.WriteFile(path);
            Response.End();
        }

the btnGenerateCallSheet was inside an UpdatePanel so I've added the following trigger to my front end page
ASP.NET
<triggers>
    <asp:postbacktrigger controlid="btnGenerateCallSheet" xmlns:asp="#unknown" />
</triggers>
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900