1

While I am opening word document and saving it on my machine its working fine, but when I am uploading it on server and opening it there, It is going in if (doc == null) block, it should not go.

Please update my question title if its not relevant or ask for any clarification.

Here is my class:

using System;
using System.Collections.Generic;
using System.Web;
using Microsoft.Office.Interop.Word;

/// <summary>
/// Summary description for ClsWordExManager
/// </summary>
public class ClsWordExManager
{
     public enum Extension
        {
            WebPage = 0
        }

        private static string HtmExtension
        {
            get
            {
                return ".htm";
            }
        }

        private static Application objWordApp = null;
        private static object objMissing = System.Reflection.Missing.Value;
        private static Document doc = null;
        static ClsWordExManager()
        {
            try
            {
                objWordApp = new Application();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public static void InitializeClass()
        {
            objWordApp.Visible = false;
        }

        private static string Open(object strFilePath)
        {
            string str = string.Empty;
            try
            {
                objWordApp.Visible = false;
                str += "<br /> word App visiblitly false";
            }
            catch (Exception ex)
            {
                objWordApp = new Application();
                str += ex.Message;
            }
            try
            {
                doc = objWordApp.Documents.Open(ref strFilePath, ref objMissing, ref objMissing, ref objMissing, ref objMissing, ref objMissing, ref objMissing, ref objMissing, ref objMissing, ref objMissing, ref objMissing, ref objMissing, ref objMissing, ref objMissing, ref objMissing, ref objMissing);
                str += "<br /> word document opened";
                if (doc == null)
                {
                    // It is null when I upload it on Windows Server 2008 with office 2007 installed.
                    str += "<br /> After openging its null";
                }
            }
            catch (Exception ex)
            {
                Close();
                objWordApp.Visible = false;
                str += "<br /> word document closed with : " + ex.Message;
            }
            return str;
        }

        private static void Close()
        {
            try
            {
                doc.Close(ref objMissing, ref objMissing, ref objMissing);
            }
            catch
            {
            }
        }


        private static string SaveAs(string FilePath, string strFileExtension, WdSaveFormat objSaveFormat)
        {
            try
            {
                if (ClsCommon.IsValidUser()) // impersonating User
                {
                    FilePath = System.IO.Path.ChangeExtension(FilePath, strFileExtension);
                    try
                    {
                        if (doc != null)
                        {
                            object objFilePath = FilePath;
                            object objFormat = objSaveFormat;
                            doc.SaveAs(ref objFilePath, ref objFormat, ref objMissing, ref objMissing, ref objMissing, ref objMissing, ref objMissing, ref objMissing, ref objMissing, ref objMissing, ref objMissing, ref objMissing, ref objMissing, ref objMissing, ref objMissing, ref objMissing);
                        }
                        else
                        {
                            FilePath += "document value is null";
                        }
                    }
                    catch
                    {
                        FilePath += "<br /> Saving document throwing expe";
                        return FilePath;
                    }

                }
                else
                {
                    FilePath += "<br /> Not valid for saving file ";
                }
            }
            catch (Exception ex)
            {
                FilePath += ex.Message;
            }
            finally
            {
                Close();
            }
            return FilePath;
        }

        public static string ReadWordFile(string strFilePath, Extension objExtension)
        {
            string strFileContent = "<br /> Reading Word File could not be completed";
            try
            {
                strFileContent += Open(strFilePath);
                if (objExtension == Extension.WebPage)
                {
                    try
                    {
                        string strNewFileName = SaveAs(strFilePath, HtmExtension, WdSaveFormat.wdFormatFilteredHTML);
                        if (strNewFileName != "")
                        {
                            strFileContent += strNewFileName + ClsCommon.ReadFile(strNewFileName, true); // ignore this line as it just read html file.
                        }
                        else
                        {
                            strFileContent += "file not saved";
                        }
                    }
                    catch (Exception ex)
                    {
                        strFileContent += ex.Message;
                    }
                }
                else
                {
                    Close();
                }
            }
            catch (Exception exx)
            {
                strFileContent += exx.Message;
            }
            return strFileContent;
        }

        public static void Quit()
        {
            try
            {
                objWordApp.Quit(ref objMissing, ref objMissing, ref objMissing);
            }
            catch
            {

            }
        }
}
vikas
  • 2,780
  • 4
  • 27
  • 37
  • 1
    Hints: don't ever do `try{...} catch (Exception ex) {throw ex;}`. You would do better to just leave out the try/catch block. Also, `ex.Message` might be good to show to a user (or not), but if you want to know what went wrong, you need `ex.ToString()`. Please stop using `try {...} catch{...}`. Something went wrong - don't ignore that. – John Saunders Feb 19 '13 at 05:42
  • Finally, what kind of application is this? Is it a console ? If it's a winhdows service or ASP.NET application, then you can't use Office Interop like that. – John Saunders Feb 19 '13 at 05:45
  • @JohnSaunders its a ASP.net App and issue is solved as I posted in answer, I did Impersonate User before calling the class, and its work charming – vikas Feb 19 '13 at 06:32
  • @JohnSaunders that are all good points, but try+catch+throw does not have to be bad, but just don't `thow ex;`, but just use `throw;`. So `try{...} catch (Exception ex) { throw /*no argument here*/; }` than you pass all info to the higher levels (or wrap it in a new exception if you wish to add info: `throw new SomeMeaningFullException("with some meaningfull message", ex);`) – SvenL Sep 10 '15 at 09:07

2 Answers2

1

Creating a desktop folder for your ASP.NET user might have solved one problem, but you'll run into many more. Word might pop up a dialog box, and you'll be stuck.

Office automation is explicitly unsupported on the server-side, and trust me - you'll have lots of problem:

Microsoft does not currently recommend, and does not support, Automation of Microsoft Office applications from any unattended, non-interactive client application or component (including ASP, ASP.NET, DCOM, and NT Services), because Office may exhibit unstable behavior and/or deadlock when Office is run in this environment.

I suggest you go over the article in the link above from where the quote came, and use the alternatives.

Avner Shahar-Kashtan
  • 14,492
  • 3
  • 37
  • 63
0

ok problem solved thanks for Sameer S post I just create Desktop folder on server 2008 machine @

C:\Windows\System32\config\systemprofile

And now Its working

And in addition of this as this is ASP.NET app, so I added impersonation check before calling class and its working fine.

Community
  • 1
  • 1
vikas
  • 2,780
  • 4
  • 27
  • 37