Click here to Skip to main content
15,891,951 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
iam use this code for wire from asp.net in word but run from iis locallhost Observation this error

Object reference not set to an instance of an object.
HTML
Line 114:
Line 115:            //System.Reflection.Missing.Value is used here for telling that method to use default parameter values when method execution
Line 116:            oDoc = oWordApplication.Documents.Open(fileName, newTemplate, docType, isVisible, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing,
Line 117:                ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);

when run or excute from vs2012 dont error and right was set-up code with out error
.
im dont whats problem.
C#
var realpath="~/template";
            var filePath = Path.Combine(HttpContext.Current.Server.MapPath("~/template"), Lcourseid.Text + ".doc");
            var oWordApplication = new Application();
            DirectoryInfo dir = new DirectoryInfo(Server.MapPath(realpath));
            foreach (FileInfo files in dir.GetFiles())
            {
                files.Delete();
            }
            // To invoke MyMethod with the default argument value, pass  
            // Missing.Value for the optional parameter.
            object missing = System.Reflection.Missing.Value;

            //object fileName = ConfigurationManager.AppSettings["DocxPath"];@"C:\DocXExample.docx";
            string fileName = @"D:\template1.dot";
            //string fileName1 = @"D:\sss.doc";
            object newTemplate = false;
            object docType = 0;
            object isVisible = true;

            //System.Reflection.Missing.Value is used here for telling that method to use default parameter values when method execution
            oDoc = oWordApplication.Documents.Open(fileName, newTemplate, docType, isVisible, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing,
                ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);
           // usable in earlier versions of Microsoft Word v2003  v11
            if(Convert.ToInt16(oWordApplication.Version) >=11)
            {
             //Sets or returns a Boolean that represents whether a document is being viewed in reading layout view.
              oDoc.ActiveWindow.View.ReadingLayout = false;
           }

            //The active window is the window that currently has focus.If there are no windows open, an exception is thrown.
            //microsoft.office.tools.word.
            oDoc.Activate();

            if (oDoc.Bookmarks.Exists("Title"))
            {
                oDoc.Bookmarks["Title"].Range.Text = "Test Field Entry from webform";
                oDoc.Bookmarks["Address"].Range.Text = "Address Field Entry from webform";
            }

            oDoc.SaveAs(filePath, ref missing);
            oWordApplication.Documents.Close(ref missing, ref missing, ref missing);
            //oWordApplication.Quit(ref SaveChanges, ref missing, ref missing, ref missing);
            ProcessRequest(filePath, Lcourseid.Text);
Posted
Comments
DamithSL 25-Dec-14 11:27am    
which line you get this error? have you debug and check which object you get null at runtime?

1 solution

C#
I worked out how to do it:

*********** 1. Find out your website is running under which identity ************
1. Open IIS and click your website;

2. Click "Advanced Settings..." on the right pane. The "Application Pool" dialog
will show up. Find out the "Application Pool", then close this dialog.

3. Click "Application Pools" under IIS. Click the application pool you just found
out, then click "Advanced Settings" on the right pane. The "Application Pool"
dialog will show up. Find out the "Identity". This could be something like
"NetworkService". This is the identity which runs your website. We need to
allow it to access the Word DCOM component.

4. Again click your website and double-click the "Authentication" icon in the middle
pane.

5. Right-click "Anynymouse Authentication" and select "Edit". Make sure the
"Application pool identity" radio button is selected.

*************** 2. Configure the Word DCOM ***************

1. Windows "Start | Run | dcomcnfg" to start DCOM configuration;

2. Go to "Component Services | Computers | My Computer | DCOM Config",
right-click "Microsoft Word 97 - 2003 Document" and select "Properties";

3. Go to "Identity" tab page, among the three radio buttons "The interactive user",
"The launching user" and "This user", select the last one, and put in the user
who installed Office onto this server/PC. This is one of the tricks otherwise
you will get errors once the backend code (such as ASP.NET) tries to invoke
the Word DCOM component while this user is not logged in. When you test on
your development PC using Visual Studio this error will never happen because
you installed Office and you are always logged in when you test.

4. Go to the "Secutiry" Tab, on the "Lanuch and Acctivation Permissions" group box,
Add the identity which runs your website - the one you found out earlier - something
like "NETWORK SERVCIE", and allow local launch and activation. Some link also
mentioned adding "IUSR_<machinename>".</machinename>
 
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