MICROSOFT.OFFICE.INTEROP.WORD - Object Reference not set to an instance of an Object

Asked

Viewed 765 times

1

inserir a descrição da imagem aqui

I am in need of help with this problem using Microsoft.Office.Interop.Word. Detail: running the application in visual studio it works normally, without any error, however, if I play in IIS, it appears this error. Before him, I had a problem with COM permission, I changed the permissions and now this error appears. Look at the photos that show everything I’ve done so far. Here is some information on what has already been done.

1 - I added the permissions of IIS_IUSRS and NETWORK SERVICES in specific COM access permission (in case Microsoft Word document 97 - 2003);

2 - COM identity is defined as interactive user;

3 - There is the reference of Microsoft.Office.Interop.Word in the project;

4 - Microsoft.Office.Interop.Word exists on the computer, ie Office is installed (64bit version). The operating system is 64bit too.

I really need to solve this problem, because the project was developed - more than 3 months - on top of Microsoft.Office.Interop.Word, where I use the conversion to PDF and also, I use a method to modify the file before converting it to PDF.

CODE

Method p/ conversion to PDF

 public static void ConverterEmPdf(string entrada, string saida)
    {
        var missing = Type.Missing;
        var appWord = new Microsoft.Office.Interop.Word.Application();
        var wordDocument = appWord.Documents.Open(entrada);
        wordDocument.ExportAsFixedFormat(saida, Microsoft.Office.Interop.Word.WdExportFormat.wdExportFormatPDF);
        ((Microsoft.Office.Interop.Word._Document)wordDocument).Close(ref missing, ref missing, ref missing);
    }

Method for changing document

public static void InserirNumero(object fileName, object saveAs, int numero)
    {
        object missing = System.Reflection.Missing.Value;
        var wordApp = new Microsoft.Office.Interop.Word.Application();
        if (!File.Exists((string) fileName)) return;
        object readOnly = false;
        object isVisible = false;
        wordApp.Visible = false;
        var 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();
        LocalizarESubstituir(wordApp, "<numerodocumento>", numero);
        aDoc.SaveAs(ref saveAs, 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, ref missing);
        ((Microsoft.Office.Interop.Word._Document)aDoc).Close(ref missing, ref missing, ref missing);  
    }

Find and replace method used in previous method

private static void LocalizarESubstituir(Microsoft.Office.Interop.Word._Application wordApp, object findText, object replaceWithText)
    {
        var missing = Type.Missing;

        object matchCase = true;
        object matchWholeWord = true;
        object matchWildCards = false;
        object matchSoundLike = false;
        object nmatchAllWordForms = false;
        object forward = true;
        object format = false;
        object matchKashida = false;
        object matchDiacritics = false;
        object matchAlefHamza = false;
        object matchControl = false;
        object replace = 2;
        object wrap = 1;

        if (replaceWithText.ToString().Length < 250)
        {
            wordApp.Selection.Find.Execute(ref findText, ref matchCase,ref matchWholeWord, ref matchWildCards, ref matchSoundLike,
                                          ref nmatchAllWordForms,ref forward, ref wrap, ref format, ref replaceWithText, ref replace,  ref matchKashida, ref matchDiacritics, ref matchAlefHamza, ref matchControl);
        }
        else
        {
            wordApp.Application.Selection.Find.Execute(ref findText,
        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);
            ((Microsoft.Office.Interop.Word._Document)wordApp).Application.Selection.Text = (string)replaceWithText;
        }
    }

IMAGES OF EVERYTHING I HAVE EVER DONE TO TRY TO SOLVE THE PROBLEM

inserir a descrição da imagem aqui

inserir a descrição da imagem aqui

inserir a descrição da imagem aqui

inserir a descrição da imagem aqui

  • Which operating system? It is a Windows Service?

  • @qmechanik this one of the images is windows 7. I tried it on Windows Server 2012 and it didn’t work either, since I couldn’t keep working too much on the main server, I decided to touch a pc momentarily.

  • Try using the tool FUSLOGVW.EXE - view . NET Assembly Binding issues to find out exactly what is happening. <br> Use this information LINK to configure the use of the tool.

  • Are you publishing with Visual Studio? It is necessary to send the libraries to the corresponding folder on the website. <br> In case there is a specific folder for the DLL. More details go to https://msdn.microsoft.com/pt-br/library/1y1404zt(v=vs.100). aspx

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.