如果变量为Null,则删除Word中的行

时间:2018-09-03 01:01:10

标签: c# console-application

我正在尝试执行以下代码,但是,当我的代码到达函数DeleteCurrentSelectionLine时,不会删除任何内容。

我希望我的方法DeleteCurrentSelectionLine()在replaceWithText为null时删除该行。如您所见,该变量为null,但不会删除该谎言。

我需要在语法上进行哪些更改以确保删除信息?

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);
}

0 个答案:

没有答案