2

I am attempting to execute the below code, however when my code hits my function DeleteCurrentSelectionLinenothing is deleted.

I expect my method DeleteCurrentSelectionLine() to delete the line if replaceWithText is null. And as you see, the variable is null, but the lie is not deleted.

What do I need to alter in the syntax to make sure the information is deleted?

static void Main(string[] args)
{
    Object oMissing = System.Reflection.Missing.Value;
    Object oFName;
    Microsoft.Office.Interop.Word.Application wrdApp = new Microsoft.Office.Interop.Word.Application();

    if (wrdApp == null)
        return;

    Microsoft.Office.Interop.Word._Document wrdDoc;
    wrdApp.Visible = true;
    oFName = "C:\\Example.docx";
    wrdDoc = wrdApp.Documents.OpenOld(ref oFName, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);
    FindReplaceInWord(wrdDoc, wrdApp);
}

private static void ReplaceText(Microsoft.Office.Interop.Word.Application doc, Microsoft.Office.Interop.Word._Document abc, object findText, object replaceWithText)
{
    object matchCase = false;
    object matchWholeWord = false;
    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 oClassType = "Word.DOcument.8";
    Object oTrue = true;
    Object oFalse = false;
    Object oMissing = System.Reflection.Missing.Value;

    if (replaceWithText == null)
    {
        DeleteCurrentSelectionLine(doc);
    }
    else
    {
        //Do Plan B
    }
}

private static void FindReplaceInWord(Microsoft.Office.Interop.Word._Document wrdDoc, Microsoft.Office.Interop.Word.Application wrdApp)
{
    string fv4 = null;
    ReplaceText(wrdApp, wrdDoc, "@@Replace4", fv4);
}
private static void DeleteCurrentSelectionLine(Microsoft.Office.Interop.Word.Application wrdApp)
{
    Object oMissing = System.Reflection.Missing.Value;

    object wdLine = Microsoft.Office.Interop.Word.WdUnits.wdLine;
    object wdCharacter = Microsoft.Office.Interop.Word.WdUnits.wdCharacter;
    object wdExtend = Microsoft.Office.Interop.Word.WdMovementType.wdExtend;
    object count = 1;

    Microsoft.Office.Interop.Word.Selection selection = wrdApp.Selection;
    selection.HomeKey(ref wdLine, ref oMissing);
    selection.MoveDown(ref wdLine, ref count, ref wdExtend);
    selection.Delete(ref wdCharacter, ref oMissing);
}
AustinWBryan
  • 3,249
  • 3
  • 24
  • 42
Timmy Turner
  • 99
  • 1
  • 8
  • TheGeneral is trying to suggest you use the debugger. – nicomp Sep 03 '18 at 01:24
  • 1
    Really confused by you're choice to use `object` for all variables, and even more confused that you're inconsistenly using `Object` for some and then using hungarian notiation. Why are you doing either of those? I don't think think it means what you think it means. You're setting `bool` values, a lot of them`, to `object`, which means you can't do `if (matchCase)` thus defeating the whole point of them.You could cast but that's just pure needless complications. If you only use `object` in your code, you loose type safety, IDE autocomplete and have to cast everywhere to actually use the them. – AustinWBryan Sep 03 '18 at 06:24
  • I don't want to be rude, but you need to review your knowledge about oriented object programming. You use objects when you should use booleans, strings and ints. You're even trying to initialize an interface.... – Lewis86 Sep 03 '18 at 09:12

0 Answers0