0
votes

Just out of interest.

The code with "Visible: true" executes correctly and replaces the content:

Word.Document doc = Utilities.wordEngine.getChangesWordApp.Documents.Open(approvedFileName, ReadOnly: false, Visible:true);
            //execute find and replace
            Utilities.wordEngine.getChangesWordApp.Selection.Find.Execute(ref findText, ref matchCase, ref matchWholeWord,
                ref matchWildCards, ref matchSoundsLike, ref matchAllWordForms, ref forward, ref wrap, ref format, ref replaceWithText, ref replace,
                ref matchKashida, ref matchDiacritics, ref matchAlefHamza, ref matchControl);
            doc.Save();

The code with "Visible: false" doesn't execute correctly, the content remains unchanged:

Update: I added a try catch around the code, and it now actually works and saves the document changes, but an exception is thrown

       Word.Document doc = Utilities.wordEngine.getChangesWordApp.Documents.Open(approvedFileName, ReadOnly: false, Visible:false);
            //execute find and replace
            try
            {
                Utilities.wordEngine.getChangesWordApp.Selection.Find.Execute(ref findText, ref matchCase, ref matchWholeWord,
               ref matchWildCards, ref matchSoundsLike, ref matchAllWordForms, ref forward, ref wrap, ref format, ref replaceWithText, ref replace,
               ref matchKashida, ref matchDiacritics, ref matchAlefHamza, ref matchControl);
                doc.Save();
            }
            catch (Exception ex)
            {
                Trace.TraceInformation(DateTime.Now.ToString("yyyy-MM-dd HH-mm-ss-fff") + " Exception: " + ex.StackTrace);
            }

The only change is Visible:true - works, Visible:false fails.

Why is this needed? I don't want to see Word or the document whilst this is running (as it goes, luckily the document in this case doesn't actually show during processing)

Edit: Adding "Utilities.wordEngine" class.

public class wordEngine
{
    public static Microsoft.Office.Interop.Word.Application wordClientApp = new Microsoft.Office.Interop.Word.Application();
    public static Word.Document sourceWord_Doc;
    public static Microsoft.Office.Interop.Word.Application compWordApp = new Microsoft.Office.Interop.Word.Application();
    public  static Microsoft.Office.Interop.Word.Application massCompWordApp = new Microsoft.Office.Interop.Word.Application();
    public static Microsoft.Office.Interop.Word.Application getChangesWordApp = new Microsoft.Office.Interop.Word.Application();
}

Update: I added a try catch around the code: The output is this:

Information: 0 : 2017-05-11 16-12-37-657 Exception: at LoadFile.FindAndReplace(String approvedFileName, String start, String end, object findText) in C:\vsCode\wordNameReplacer\wordNameReplacer\Replacer\LoadWordFile.xaml.cs:line 685

Screen-grab of the contents of the exception. I cannot find any object that is not set (unless Word requires some of the missing parameter objects for Search and replace)

Word interop Search replace exception

1
"doesn't work" and "fails" are not appropriate problem descriptions.Hans Passant
Sorry Hans, not quite sure what you want? The question title combined with doesn't work / works is a reasonable explanation isn't it? Essentially "Visible: true" will permit Search and replace to run, "Visible: false" failsJerry Weeks
OK, running debug the exception gives: {"Object reference not set to an instance of an object."}Jerry Weeks

1 Answers

0
votes

First of all, there is no need to create an instance of the Word Application class multiple times:

public class wordEngine
{
    public static Microsoft.Office.Interop.Word.Application wordClientApp = new Microsoft.Office.Interop.Word.Application();
    public static Word.Document sourceWord_Doc;
    public static Microsoft.Office.Interop.Word.Application compWordApp = new Microsoft.Office.Interop.Word.Application();
    public  static Microsoft.Office.Interop.Word.Application massCompWordApp = new Microsoft.Office.Interop.Word.Application();
    public static Microsoft.Office.Interop.Word.Application getChangesWordApp = new Microsoft.Office.Interop.Word.Application();
}

Looks like there is no Selection exists when the document is not visible. So, I'd suggest breaking the chain of property and methods calls and declaring them on new lines of code. Thus, you will be able to find what property or method exactly fired an exception in the code.