You are viewing limited content. For full access, please sign in.

Question

Question

Object reference not set to an instance of an object

asked on November 24, 2015

Good day everyone! 

 

Honestly I'm going crazy with this warning, I'm using workflow to create a copy of a document, replace a couple of specific fields and save the new document. On my computer everything works, but once I use the same c# code on the server it shows the warning message "Object reference not set to an instance of an object " 

I've even made a brand new workflow, where I typed all the code manually, but even then it showed the same warning. 

Now, there are two scenarios:

1- I run the code directly from the Script Editor and it works out correctly, the new document is created replacing the information I wanted. 

2- I run the whole workflow, it doesn't fail (it only shows the warning). The new document is created (even with the name I set) but not even one of the fields is replaced. 

 

Is there something I'm missing? I've been looking for this warning in answers and haven't found anything that could help me. 

 

Any comment please let me know, thanks in advance. 

0 0

Replies

replied on November 24, 2015

In the advanced properties for the script activity (under the gear icon in the top right corner of the properties pane), set it to throws errors instead of warnings. Then republish the workflow and run it.

Once you get the error, look in the Workflow Admin Console, under Monitoring\Error Logs\Activity Errors. That should give us the whole stack trace so we can see what exactly fails.

That said, i really wouldn't recommend running this script on the server since it's actually launching documents and could leave instances of Word running.

Take a look at the new Update Word Document activity in Workflow 10. It's using Word mail merge functionality to do something very similar with what your script is trying to accomplish.

2 0
replied on November 25, 2015

Miruna, sorry, for some reason I didn't notice your comment. 

 

Regarding Laserfiche 10, we saw the webinar a couple of weeks before so we know that the new version can do it (it was kind of funny and angry that we spent several hours making this code works the first time and now it can be done easily), the problem is that Laserfiche 10 can't be installed soon on production.

 

It's almost the end of my shift, tomorrow I will follow yours and Chris recommendations.

 

Thanks.  

0 0
replied on November 24, 2015 Show version history

Hi Ricardo,

 

You're going to want to also make sure the proper settings are enabled in the workflow administration console. Review the settings in Security -> File Browser Options and Security -> Scripting.

 

 

This is what determines which scripts, files, or file browsing options are accessible to executed workflows.

 

It looks like you may be referencing a word document and copying it from the C: drive. Can you confirm these three files are in the same location on the server as they are on your machine?

 

string docPath = @"C:\Firma Digital (no borrar)\Documentos\Machote\Machote prueba.docx";

string ruta = @"C:\Firma Digital (no borrar)\Documentos\En Proceso\"+nombre+".docx";

string ruta2 = @"C:\Firma Digital (no borrar)\Documentos\Terminada\"+nombre+".docx";

File.Copy(docPath, Path.Combine(Application.StartupPath, ruta),true);

 

1 0
replied on November 25, 2015 Show version history

I noticed that in the Workflow Administration Console under Security>Scripting, you only have LFSO Interops and .Net Framework. If you double click on that line and select the option of "All Assemblies", then click on "Add..." button, you should be able to find your assembly and add it here. Once you do this, try and run your Workflow again. Note: I do agree with Miruna on this, any file system access using Workflow could have adverse affects and cause Workflow to hang.

1 0
replied on November 24, 2015

Could you post your code for the "Crea Documento" script?

0 0
replied on November 24, 2015 Show version history

Take a look at any references to files or assemblies.

 

When you run the code from the script editor it will use all paths from your local machine.

 

When it runs as part of a workflow all paths (and permissions) will be executed from the workflow server and the service account it's running as.

 

So if there's a reference in your script to c:\picture.png

 

It will look at your local machine for c:\picture when running the workflow from script editor and the server's c: when ran as a workflow.

 

I've ran into this a few times, especially when moving a workflow from one workflow server to another, have to make sure any referenced objects, files, or assemblies can still be accessed with the path.

 

Cheers!

Carl

0 0
replied on November 24, 2015

Thanks for your responses.

Here is the code I'm using 

namespace WorkflowActivity.Scripting.CreaDocumento
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Data.SqlClient;
    using System.Linq;
    using System.Text;
    using System.IO;
    using System.Reflection;
    using Microsoft.Office.Core;
    using System.Windows.Forms;
    using Word = Microsoft.Office.Interop.Word;
    /// <summary>
    /// Provides one or more methods that can be run when the workflow scripting activity is performed.
    /// </summary>
    public class Script1 : ScriptClass90
    {
        /// <summary>
        /// This method is run when the activity is performed.
        /// </summary>
        protected override void Execute()
        {
            // Write your code here. The BoundEntryInfo property will access the entry, RASession will get the Repository Access session
            Word.Application wordApp;
            Word.Document aDoc;
            object missing = Missing.Value;
            object filename;

            string nombre = "Documento de Prueba";

            string docPath = @"C:\Firma Digital (no borrar)\Documentos\Machote\Machote prueba.docx";
            string ruta = @"C:\Firma Digital (no borrar)\Documentos\En Proceso\"+nombre+".docx";
            string ruta2 = @"C:\Firma Digital (no borrar)\Documentos\Terminada\"+nombre+".docx";

            File.Copy(docPath, Path.Combine(Application.StartupPath, ruta),true);
            wordApp = new Word.Application();
            filename = Path.Combine(Application.StartupPath,ruta);

            if(File.Exists((string)filename)){
                object readOnly = false;
                object isVisible = false;
                wordApp.Visible = false;
                aDoc = wordApp.Documents.Open(ref filename, 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();

                string f = "";
                string r = "";

                for(int i = 1; i <= 2; i++){
                    f = "";
                    r = "";

                    if(i == 1){
                        f = "<nombre>";
                        r = "Ricardo";
                    }
                    if(i == 2){
                        f = "<cedula>";
                        r = "207140375";
                    }

                    object matchCase = true;
                    object matchWholeWord = true;
                    object matchWildCards = false;
                    object matchSoundsLike = false;
                    object matchAllWordForms = false;
                    object forward = true;
                    object format = false;
                    object matchKashida = false;
                    object matchDiacritics = false;
                    object matchAlefHamza = false;
                    object matchControl = false;
                    object read_only = false;
                    object visible = true;
                    object replace = 2;
                    object wrap = 1;
                    object findText = f;  //<------------Variable f
                    object replaceText = r; //<------------Variable r
                    wordApp.Selection.Find.Execute(ref findText, ref matchCase, ref matchWholeWord,
                    ref matchWildCards, ref matchSoundsLike, ref matchAllWordForms,
                    ref forward, ref wrap, ref format, ref replaceText, ref replace,
                    ref matchKashida, ref matchDiacritics, ref matchAlefHamza, ref matchControl);
                }

                aDoc.Save();

                object saveChanges = Word.WdSaveOptions.wdSaveChanges;
                wordApp.Quit(ref saveChanges, ref missing, ref missing);
            }
        }
    }
}

And here are the references

The code is not really well made, but I feel Script Editor isn't as flexible as other IDEs.  

Carl, the dll files are located in a "C" path, so when I added the references I pick them from there, how can I know if WF can access to that path? By IIS? 

 

Regards! 

 

 

0 0
replied on November 24, 2015 Show version history

Looks like you are referencing Office interop libraries. Most production servers do not have Office installed. That would be the first thing I would check. If Office is installed, as for my understanding, the dlls' must be copied to the Workflow folder in Program files and registered there. Check this post as it outlines it pretty well.

0 0
replied on November 24, 2015 Show version history

Chris, Office is already installed. About the dlls, I had them in a C path, but after reading Carl's comment I move them to the WF folder (also checked the post you mentioned and it seems they are in the right place now),:

Now, the only Dlls I actually need are Interop.Word, Access.Dao and office.core, as you can see in the image bellow they are now in the WF folder.

 

 

 

-----------

Edit:

Carl, not sure if this is what you were talking about, but I checked the Worfkflow services and they use LocalSystem account

0 0
replied on November 24, 2015

Additionally, I made another copy of DLLs in the 32-bits folder just in case and changed the references, but the problem persist..

 

Sorry for using two accounts, David is my coworker and his account was already logged.  

0 0
replied on November 24, 2015

Carl,

It seems that everything is configured correctly, please take a look at the screenshot, maybe I'm missing something:

 

Regarding the word document, there is only one document, in its content there are a couple of words between "<>", the first step is to copy that document and paste it in the "En Proceso" folder, once this copy is ready those words are replaced (it should use tokens but since its just a test I'm using the same strings). Finally, once the words between "<>" are replaced, I  make a copy of the modified doc and paste it in the "Terminada" folder, however since the code isn't working on the development server I'm not doing the last step. 

 

 

0 0
replied on November 26, 2015 Show version history

I followed Chris advice and added the DLL's using the Administration Console. 

 

The warning messages changed, it says:

 

I investigated this problem and it may be caused if I'm using 64-bits dlls in a 32-bits system and vice versa, but if I run my script in the Script Editor there is no problem. So I guess the warning changed but the problem might be the same.

 

Miruna, I also did what you suggested, there is only one error message which is different:

 

Please let me know your comments, I need to have this done pretty soon and I have no idea how to fix it.

 

 

 

0 0
replied on November 30, 2015

The stack trace indicates a security issue in the file path.

The other error is most likely related. You cannot mix and match libraries with different CPU architectures. If you're running Workflow as x64, then the Office libraries need to be x64 as well. Additionally, you're showing that the Workflow Service is running as Local System. So Word would be launched under that user when the script runs. It's very likely that Word is stalling trying to show the message about picking the username and initials that it usually shows when first launched.

These are just a few of the reasons why Microsoft does not support server-side automation of Office components.

 

1 0
replied on November 30, 2015 Show version history

Miruna,

 

I placed the DLLS in the workflow installation path, as suggested above. How can I fix the security issue?

 

I understand your point regarding the CPU architectures, but I'm confused, if this is the issue, then why does the code work correctly  when I run the script in the Script Editor?  

 

---------Edit---------

Also, my computer and WF are 64-bits, which is the same as the development server (where I'm facing this problem), and everything works fine on my computer.

 

Today I tried to run the WF again and the error message changed

0 0
replied on November 30, 2015

Per development the process in question is not supported by Microsoft in this link "Server Side Automation Process" Development redirect to Microsoft kb article, at the bottom of the same kb article, Microsoft list few alternative procedures to try on , the developer suggest to try one by one.  In addition here is the link for more alternative sites that can be review

0 0
You are not allowed to follow up in this post.

Sign in to reply to this post.