I have asp.net app in which I manipulate with word documents and than save them in a folder. The application is working as it should locally but when I am running the same app on the server, the word document doesn't get created, and I am getting file could not be found, when I am trying to access it. Any idea what can be the reason? Fyu, I have MS word installed on the server, if that helps. Thanks in advance, Laziale

UPDATE: Code Sample Added

Word._Application app = null;
Word.Documents docs = null;
Word.Document doc = null;
object path = HttpContext.Current.Server.MapPath(@"~\Docs\Report.doc");
Data dt = new Data();
double footprintTotalEmmissions = 0.0, totalPercentage = 0.0;

app = new Word.Application();
app.Visible = false;
app.DisplayAlerts = Word.WdAlertLevel.wdAlertsNone;
SqlConnection conn = new SqlConnection(connString);
SqlCommand commGetUserId = new SqlCommand("GetEmailData", conn);
commGetUserId.CommandType = CommandType.StoredProcedure;
commGetUserId.CommandTimeout = 0;
try
{

    commGetUserId.Parameters.AddWithValue("@UserID", username);
    SqlDataReader rdr = null;
    conn.Open();
    rdr = commGetUserId.ExecuteReader();
    if (rdr.HasRows)
    {
        while (rdr.Read())
        {
            companyName = rdr[1].ToString();
            address = rdr[2].ToString();
            footprintEl = Convert.ToDouble(rdr[3].ToString());
            electricityConsumed = Convert.ToSingle(rdr[7].ToString());
            dollarsEl = Convert.ToDecimal(rdr[6].ToString());
            naturalGasConsumed = Convert.ToSingle(rdr[9].ToString());
            dollarsNG = Convert.ToDecimal(rdr[8].ToString());
            footprintNG = Convert.ToDouble(rdr[10].ToString());
            footprintProp = Convert.ToDouble(rdr[11].ToString());
            dollarsProp = Convert.ToDecimal(rdr[12].ToString());
            footprintheatingOil = Convert.ToDouble(rdr[13].ToString());
            dollarsHo = Convert.ToDecimal(rdr[14].ToString());
            dollarsWaste = Convert.ToDecimal(rdr[15].ToString());
            dollarsWater = Convert.ToDecimal(rdr[16].ToString());
            waterTotal = Convert.ToInt32(rdr[17].ToString());
            footprintTr = Convert.ToDouble(rdr[18].ToString());
            footprintFlights = Convert.ToDouble(rdr[19].ToString());
            toMonthYear = Convert.ToDateTime(rdr[5].ToString());
            fromMonthYear = Convert.ToDateTime(rdr[4].ToString());
        }
    }
    rdr.Close();
    //        toMonthYear = Convert.ToDateTime(docCompanyMain.SelectSingleNode("Xml/Resources/Electricity/AnnualBillingPeriod").Attributes[1].InnerText);
    int month = toMonthYear.Month;
    int yearr = toMonthYear.Year;
    totalTo = Convert.ToString(month + "/" + yearr);
    month = fromMonthYear.Month;
    yearr = fromMonthYear.Year;
    totalFrom = Convert.ToString(month + "/" + yearr);
    footprintTotalEmmissions = footprintEl + footprintNG + footprintProp + footprintheatingOil + footprintTr + footprintFlights;
    totalPercentage = footprintTotalEmmissions * 0.05;
    totalAmount = dollarsEl + dollarsHo + dollarsNG + dollarsProp + dollarsWaste + dollarsWater;
    docs = app.Documents;
    doc = docs.Open(ref path, ref o, ref o, ref o, ref o, ref o, ref o, ref o, ref o, ref o, ref o, ref o, ref o, ref o, ref o, ref o);
    doc.Activate();

    foreach (Word.Range range in doc.StoryRanges)
    {
        if (range.Text.ToString().Contains("Today_Date"))
        {

            string test = "";
        }
        Word.Find find = range.Find;
        object findText = "[Company_Name]";
        object replacText = companyName;
        object findDate = "[Month_Day_Year]";
        object todaysDate = DateTime.Now.ToShortDateString();
        object findAddress = "[Company_address]";
        object total_Address = completeAddress;
        object findwaterTotal = "[Water Total]";
        object totalWater = waterTotal;
        object elecCO2Total = "[Elect_Total_Co2]";
        object co2Total = footprintEl;
        object ngCO2Total = "[NG_Total_Co2]";
        object ngTotal = footprintNG;
        object propaneTotal = "[Prop_Total_Co2]";
        object propTotal = footprintProp;
        object heatTotal = "[Heat_Oil_Total_Co2]";
        object heatingTotal = footprintheatingOil;
        object motorTotal = "[Motor_Trans_Total_Co2]";
        object motorc0total = footprintTr;
        object airMilesTotal = "[Air_Miles_Total_Co2]";
        object airCO2Total = footprintFlights;
        object totalEmmissions = "[Total_Emissions_Co2]";
        object emissionsTotal = footprintTotalEmmissions;
        object todayDate = "[Today_Date]";
        object dateToday = DateTime.Now.ToShortDateString();
        object year = "[Year]";
        object yearValue = DateTime.Now.Year;
        object compName = "[Company Name]";
        object totalCarbonFootprint = "[total_carbon_footprint]";
        object percentageSave = "[Fivepercent_total_carbon_footprint]";
        object percentageResult = totalPercentage;
        object dollarsSave = "[Dollars_saved]";
        object obDollarsSave = '$' + Convert.ToString(totalAmount);
        object fromMonthTo = "[From_Month_Year";
        object replaceFromMonthTO = totalFrom;
        object toMonthTo = "To_Month_ Year]";
        object replaceToMonthTo = totalTo;

        object replace = Word.WdReplace.wdReplaceAll;
        object findWrap = Word.WdFindWrap.wdFindContinue;

        find.Execute(ref fromMonthTo, ref o, ref o, ref o, ref oFalse, ref o,
            ref o, ref findWrap, ref o, ref replaceFromMonthTO,
            ref replace, ref o, ref o, ref o, ref o);
        find.Execute(ref toMonthTo, ref o, ref o, ref o, ref oFalse, ref o,
          ref o, ref findWrap, ref o, ref replaceToMonthTo,
          ref replace, ref o, ref o, ref o, ref o);
        find.Execute(ref findText, ref o, ref o, ref o, ref oFalse, ref o,
            ref o, ref findWrap, ref o, ref replacText,
            ref replace, ref o, ref o, ref o, ref o);
        find.Execute(ref findDate, ref o, ref o, ref o, ref oFalse, ref o,
            ref o, ref findWrap, ref o, ref todaysDate,
            ref replace, ref o, ref o, ref o, ref o);
        find.Execute(ref findAddress, ref o, ref o, ref o, ref oFalse, ref o,
            ref o, ref findWrap, ref o, ref total_Address,
            ref replace, ref o, ref o, ref o, ref o);
        find.Execute(ref findwaterTotal, ref o, ref o, ref o, ref oFalse, ref o,
            ref o, ref findWrap, ref o, ref totalWater,
            ref replace, ref o, ref o, ref o, ref o);
        find.Execute(ref elecCO2Total, ref o, ref o, ref o, ref oFalse, ref o,
            ref o, ref findWrap, ref o, ref co2Total,
            ref replace, ref o, ref o, ref o, ref o);
        find.Execute(ref ngCO2Total, ref o, ref o, ref o, ref oFalse, ref o,
            ref o, ref findWrap, ref o, ref ngTotal,
            ref replace, ref o, ref o, ref o, ref o);
        find.Execute(ref propaneTotal, ref o, ref o, ref o, ref oFalse, ref o,
            ref o, ref findWrap, ref o, ref propTotal,
            ref replace, ref o, ref o, ref o, ref o);
        find.Execute(ref heatTotal, ref o, ref o, ref o, ref oFalse, ref o,
           ref o, ref findWrap, ref o, ref heatingTotal,
           ref replace, ref o, ref o, ref o, ref o);
        find.Execute(ref motorTotal, ref o, ref o, ref o, ref oFalse, ref o,
           ref o, ref findWrap, ref o, ref motorc0total,
           ref replace, ref o, ref o, ref o, ref o);
        find.Execute(ref airMilesTotal, ref o, ref o, ref o, ref oFalse, ref o,
           ref o, ref findWrap, ref o, ref airCO2Total,
           ref replace, ref o, ref o, ref o, ref o);
        find.Execute(ref totalEmmissions, ref o, ref o, ref o, ref oFalse, ref o,
           ref o, ref findWrap, ref o, ref emissionsTotal,
           ref replace, ref o, ref o, ref o, ref o);
        find.Execute(ref year, ref o, ref o, ref o, ref oFalse, ref o,
       ref o, ref findWrap, ref o, ref yearValue,
       ref replace, ref o, ref o, ref o, ref o);
        find.Execute(ref todayDate, ref o, ref o, ref o, ref oFalse, ref o,
          ref o, ref findWrap, ref o, ref dateToday,
          ref replace, ref o, ref o, ref o, ref o);
        find.Execute(ref compName, ref o, ref o, ref o, ref oFalse, ref o,
         ref o, ref findWrap, ref o, ref replacText,
         ref replace, ref o, ref o, ref o, ref o);
        find.Execute(ref totalCarbonFootprint, ref o, ref o, ref o, ref oFalse, ref o,
       ref o, ref findWrap, ref o, ref emissionsTotal,
       ref replace, ref o, ref o, ref o, ref o);
        find.Execute(ref percentageSave, ref o, ref o, ref o, ref oFalse, ref o,
      ref o, ref findWrap, ref o, ref percentageResult,
      ref replace, ref o, ref o, ref o, ref o);
        find.Execute(ref dollarsSave, ref o, ref o, ref o, ref oFalse, ref o,
        ref o, ref findWrap, ref o, ref obDollarsSave,
        ref replace, ref o, ref o, ref o, ref o);

        Marshal.FinalReleaseComObject(find);
        Marshal.FinalReleaseComObject(range);
    }

    //  doc.Save();
    string name = "";               
    name = HttpContext.Current.Server.MapPath(@"~\Docs\Report.doc");
    object nameObject = name;
    doc.SaveAs(ref nameObject, ref o, ref o, ref o, ref o, ref o, ref o, ref o, ref o, ref o, ref o, ref o, ref o, ref o, ref o, ref o);
    ((Word._Document)doc).Close(ref o, ref o, ref o);
    app.Quit(ref o, ref o, ref o);
share|improve this question
Post your Word Document creation code. FYI automation of Office on the server is not supported by MSFT but I know sometimes its the only option – kd7 Dec 21 '11 at 17:57
look at C# and Microsoft.Interop in google as well there are TONS of examples out there.. this replaces the old OleAutomation / COM frm unmanaged code days.. – DJ KRAZE Dec 21 '11 at 17:58
@kd7 please find attached my code. at DJ KRAZE I am using Interop, and as I said it looks good locally, but for some reason when I am trying to run the page from the server, the document doesn't get created. Thanks both of you – Laziale Dec 21 '11 at 18:13
That's many ref os – Nico Dec 21 '11 at 18:13
1  
Could possibly be a permissions issue creating the file in the specified folder. As a test give NETWORK SERVICE account full access to the folder where you want the file created and see if the file gets created. – Kev Ritchie Dec 21 '11 at 18:55
show 4 more comments

Know someone who can answer? Share a link to this question via email, Google+, Twitter, or Facebook.

Your Answer

 
or
required, but never shown
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.