Click here to Skip to main content
15,888,020 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I Publish(Deployed) My website on IIS

Everything is working Fine

But i am generating Word using Find And Replace (Mail Merge) Method,

From Visual Studio its Working

But When i open project from another computer using http://192.168.0.101/Electro/SalesInvoice.aspx

it's Not Generating Word Document

and if we check path its showing path like this ==>
D:ExportToWordExportInvoiceExportSalesInvoiceTamplate.docx


What I have tried:

protected void tpbtnExportWord_Click(object sender, EventArgs e)
  {
      //Finding the FooterTemplate and access its controls
      Control FooterTemplate = Tprptr.Controls[Tprptr.Controls.Count - 1].Controls[0];

      Label lblTotalAfterTax = FooterTemplate.FindControl("tplblTotalAfterTax") as Label;

      Label TplblTaxableValue = FooterTemplate.FindControl("tplblTaxableValue") as Label;
      Label TPlblsgst = FooterTemplate.FindControl("tplblsgst") as Label;
      Label Tplblcgst = FooterTemplate.FindControl("tplblcgst") as Label;
      Label TPlbligst = FooterTemplate.FindControl("tplbligst") as Label;
      Label TplblTotalValue = FooterTemplate.FindControl("tplblTotalValue") as Label;
      //Label TPlblTotalBeforeTax = FooterTemplate.FindControl("tplblTotalBeforeTax") as Label;
      Label TPlblTotalBeforeTaxValue = FooterTemplate.FindControl("tplblTotalBeforeTaxValue") as Label;
      Label TPlblTotalinWordValule = FooterTemplate.FindControl("tplblTotalinWordValule") as Label;
      Label TPtotalTaxValue = FooterTemplate.FindControl("totalTaxValue") as Label;
      Label TPGstOnReverceChargeValue = FooterTemplate.FindControl("tplblGstOnReverceChargeValue") as Label;

      objSalesInvoiceBO.TpTotalBeforeTax = Convert.ToDecimal(TplblTaxableValue.Text.ToString());
      objSalesInvoiceBO.TpTotalBeforeTax = Convert.ToDecimal(TPlblTotalBeforeTaxValue.Text.ToString());
      objSalesInvoiceBO.TpTotalAfterTax = Convert.ToDecimal(lblTotalAfterTax.Text.ToString());
      objSalesInvoiceBO.TpGstOnReverceCharge = TPGstOnReverceChargeValue.Text.ToString();
      objSalesInvoiceBO.TpSgsttotal = Convert.ToDecimal(TPlblsgst.Text.ToString());
      objSalesInvoiceBO.TpCgsttotal = Convert.ToDecimal(Tplblcgst.Text.ToString());
      objSalesInvoiceBO.TpIgsttotal = Convert.ToDecimal(TPlbligst.Text.ToString());
      objSalesInvoiceBO.TpTotalInWord = TPlblTotalinWordValule.Text.ToString();
      objSalesInvoiceBO.TpTotalAfterTax = Convert.ToDecimal(TplblTotalValue.Text.ToString());


      #region

      if (expddlInvoiceTypeCategory.SelectedValue == "1")
      {
          object strTemplate = "D:\\ExportToWord\\TAXInvoice\\TaxInvoiceBlank.docx";
          strReportPath = "D:\\ExportToWord\\TAXInvoice\\resultt.docx";

          try
          {
              object missing = System.Reflection.Missing.Value;
              object outputFileName = strReportPath;
              object fileFormat;

              if (File.Exists((string)strTemplate))
              {
                  DateTime today = DateTime.Now;

                  object readOnly = false;
                  object isVisible = false;
                  wordApp = new Word.Application();
                  aDoc = null;
                  wordApp.Visible = false;

                  aDoc = wordApp.Documents.Open(ref strTemplate, ref missing,
                  ref readOnly, ref missing, ref missing, ref missing,
                  ref missing, ref missing, ref missing, ref missing,
                  ref missing, ref isVisible, ref missing, ref missing,
                  ref missing, ref missing);

                  aDoc.Activate();
                  IList<int> lstTableCounts = new List<int>();
                  for (int i = 1; i <= aDoc.Tables.Count; i++)
                  {
                      Word.Cell cell = aDoc.Tables[i].Cell(1, 1);

                      if (cell.Range.Text.Contains("<lblMultiResults>"))
                      {
                          lstTableCounts.Add(i);
                      }
                  }
                  int[] inttablecounts = lstTableCounts.ToArray();

                  if (TPInvoiceId == 0)
                  {
                      DataTable dt = new DataTable();
                      dt = objSalesInvoiceBL.GetTpSalesInvoiceMaxId();
                      string Maxid = dt.Rows[0][0].ToString();
                      if (dt.Rows.Count > 0)
                      {
                          objSalesInvoiceBO.TPInvoiceId = Convert.ToInt32(Maxid);
                      }
                  }
                  else
                  {
                      objSalesInvoiceBO.TPInvoiceId = Convert.ToInt32(TPInvoiceId);
                  }
                  //int result = objPurchaseOrderBL.insert_PurchaseMaterialTotal(objPurchaseOrderBO);
                  DataTable dt2 = new DataTable();
                  DataTable dt4 = new DataTable();

                  dt2 = objSalesInvoiceBL.Select_TpSalesInvoiceMaterialdetailForPDF(objSalesInvoiceBO);
                  dt4 = objSalesInvoiceBL.Select_TpSalesInvoiceMaterialdetailForSupplierPoNo(objSalesInvoiceBO);
                  if (dt2 != null)
                  {
                      if (dt2.Rows.Count > 0)
                      {
                          #region Code for Multiple Results table
                          foreach (var d in inttablecounts)
                          {
                              string[] strcol;
                              strcol = new string[aDoc.Tables[d].Columns.Count];
                              for (int i = 1; i <= aDoc.Tables[d].Columns.Count; i++)
                              {
                                  Word.Cell cell = aDoc.Tables[d].Cell(3, i);
                                  strcol[i - 1] = cell.Range.Text.Replace("\r", "").Replace("\a", "");
                              }
                              for (int i = 1; i < dt2.Rows.Count; i++)
                              {
                                  aDoc.Tables[1].Rows.Add(aDoc.Tables[d].Rows[i + 2]);
                                  for (int j = 1; j <= aDoc.Tables[d].Columns.Count; j++)
                                  {
                                      Word.Cell cell = aDoc.Tables[d].Cell(i + 2, j);
                                      cell.Range.Text = strcol[j - 1].Substring(0, strcol[j - 1].Length - 1) + " " + i + ">";
                                  }
                              }
                              for (int i = 1; i <= aDoc.Tables[d].Columns.Count; i++)
                              {
                                  Word.Cell cell = aDoc.Tables[d].Cell(dt2.Rows.Count + 2, i);
                                  cell.Range.Text = strcol[i - 1].Substring(0, strcol[i - 1].Length - 1) + " " + dt2.Rows.Count + ">";
                              }
                              try
                              {
                                  for (int j = 1; j <= aDoc.Tables[d].Columns.Count; j++)
                                  {
                                      Word.Cell cell = aDoc.Tables[d].Cell(1, j);
                                      cell.Range.Borders[Word.WdBorderType.wdBorderLeft].LineStyle = Word.WdLineStyle.wdLineStyleNone;
                                      cell.Range.Borders[Word.WdBorderType.wdBorderRight].LineStyle = Word.WdLineStyle.wdLineStyleNone;
                                      cell.Range.Borders[Word.WdBorderType.wdBorderTop].LineStyle = Word.WdLineStyle.wdLineStyleNone;

                                      //wdLineStyleSingle
                                  }

                              }
                              catch (Exception ex)
                              {

                              }
                          }
                          FindAndReplace(wordApp, "<tinvoiceno>", //P Invoice No
                          dt4.Rows[0]["TpAutoNumber"].ToString());
                          FindAndReplace(wordApp, "<tinvoicedate>", //date
                          dt4.Rows[0]["TpInvoiceDate"].ToString());

                          FindAndReplace(wordApp, "<lblMultiResults>", "");
                          for (int i = 1; i <= dt2.Rows.Count; i++)
                          {
                              FindAndReplace(wordApp, "<mulSrNo " + i + ">", i.ToString());
                              FindAndReplace(wordApp, "<mulpdescription " + i + ">",
                              dt2.Rows[i - 1]["Product Description HSN/HAC Code"].ToString());
                              FindAndReplace(wordApp, "<mulpqty " + i + ">",
                              dt2.Rows[i - 1]["Qty(Unit)"].ToString());
                          }
                      }
                  }
                  if (dt2.Rows[0][0].ToString() == "No")
                  {
                      outputFileName = strTemplate.ToString().Replace(".docx", ".pdf");
                      fileFormat = Word.WdSaveFormat.wdFormatPDF;
                  }
                  else
                  {
                      fileFormat = Word.WdSaveFormat.wdFormatDocumentDefault;
                  }
              }
              else
              {
                  System.Windows.Forms.MessageBox.Show("File dose not exist.");
                  return;
              }
              aDoc.SaveAs(ref outputFileName, ref fileFormat, ref missing, ref missing,
  ref missing, ref missing, ref missing, ref missing,
  ref missing, ref missing, ref missing, ref missing,
  ref missing, ref missing, ref missing, ref missing);
              object saveChanges = Word.WdSaveOptions.wdDoNotSaveChanges;
              aDoc.Close(ref saveChanges, ref missing, ref missing);
              wordApp.Quit();
              System.Diagnostics.Process.Start(@outputFileName.ToString());
          }
          catch (Exception ex)
          {
          }
      }
  }
Posted
Updated 26-Dec-17 20:24pm

1 solution

First off, why are you hardcoding paths in any application, much less a web app?

What if the computer you try to load this on doesn't have a D drive, or it's not accessible to your application?
What if it doesn't have Word installed?

I suspect this "From Visual Studio its Working" because that's your development computer, and you have a D drive, have full access to it as your user, and have Office installed.
But production is generally different: your user will have access only to specific areas, normally just your site folder and it's subdirectories; the IIS server is unlikely to have Office installed, and if it has a D: drive it's not going to be read and write accessible to the IIS user that your app runs under...
 
Share this answer
 
Comments
Akshada Sane 27-Dec-17 3:19am    
From My Developement computer i run website using chrome

localhost/Electro/SalesInvoice.aspx

that time its also not working (in same computer MS Office 2007 installed)

its works fine when run from asp.net platform.
Dave Kreskowiak 29-Dec-17 1:31am    
You absolutely can NOT use Office Interop in an ASP.NET application. None of the Office application support being used in a web app. Yes, it will work on your DEV machine, but put it into production with multiple users and your app will crash.

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