你知道,我认为这是互操作中的一个错误。我解决它的方法是使用 wdSentence 并为表编写其他代码(以识别行)。我必须为我的 DeleteWhere 包装方法执行此操作。
public bool DeleteWhere(string value, StringContentType type = StringContentType.Item, bool caseSensitive = true)
{
if (string.IsNullOrWhiteSpace(value))
{
return false;
}
bool ret = false;
if (type == StringContentType.Line)
{
ret = DeleteRowWhere(value, caseSensitive);
}
object mytype = type.ToWdUnits();
if (type == StringContentType.Line)
{
mytype = Microsoft.Office.Interop.Word.WdUnits.wdSentence;
}
Microsoft.Office.Interop.Word.Range range = doc.Content;
object matchword = true;
while (range.Find.Execute(value, caseSensitive, matchword))
{
range.Expand(ref mytype);
range.Delete();
ret = true;
}
return ret;
}
private bool DeleteRowWhere(string value, bool caseSensitive = true)
{
bool ret = false;
string search = caseSensitive ? value : value?.ToUpperInvariant();
foreach (Microsoft.Office.Interop.Word.Table table in doc.Tables)
{
for (int x = 1; x <= table.Rows.Count; x++)
{
for (int y = 1; y <= table.Columns.Count; y++)
{
string val = caseSensitive ? table.Cell(x, y).Range.Text : table.Cell(x, y).Range.Text?.ToUpperInvariant();
if (val != null && val.Contains(search))
{
table.Rows[x].Delete();
ret = true;
}
}
}
}
return ret;
}