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)