Range.Find - 类型不匹配。搜索长字符串时
Range.Find - Type mismatch. when searching for long strings
在 excel 的 C# VSTO 中,当我在非常长的字符串上使用 Find
方法时,我得到:
Type mismatch. (Exception from HRESULT: 0x80020005 (DISP_E_TYPEMISMATCH))
有什么想法吗?
在使用 Excel 和谷歌搜索后,我发现 Find
有 255 个字符的限制 - 您可以按 CTRL-F 并查看输入名额有限
我找不到该限制的 Microsoft 文档。
所以这是 C# 中的解决方法扩展方法:
public static Range FindLongStr(this Range rng,
string what,
object after,
object lookIn,
object lookAt,
object searchOrder,
XlSearchDirection searchDirection = XlSearchDirection.xlNext,
object matchCase = null,
object matchByte = null,
object searchFormat = null){
Range retVal;
if (what.Length < 255)
{
retVal = rng.Find(what, after, lookIn, lookAt, searchOrder, searchDirection, matchCase, matchByte, searchFormat);
}
else
{
var whatSearch = what.Substring(0, 254) + "*";
retVal = rng.Find(whatSearch, after, lookIn, lookAt, searchOrder, searchDirection, matchCase, matchByte, searchFormat);
while (retVal != null && retVal.Cells.Value2 != what)
{
retVal = rng.Find(whatSearch, retVal, lookIn, lookAt, searchOrder, searchDirection, matchCase, matchByte, searchFormat);
}
}
return retVal;
}
在 excel 的 C# VSTO 中,当我在非常长的字符串上使用 Find
方法时,我得到:
Type mismatch. (Exception from HRESULT: 0x80020005 (DISP_E_TYPEMISMATCH))
有什么想法吗?
在使用 Excel 和谷歌搜索后,我发现 Find
有 255 个字符的限制 - 您可以按 CTRL-F 并查看输入名额有限
我找不到该限制的 Microsoft 文档。 所以这是 C# 中的解决方法扩展方法:
public static Range FindLongStr(this Range rng,
string what,
object after,
object lookIn,
object lookAt,
object searchOrder,
XlSearchDirection searchDirection = XlSearchDirection.xlNext,
object matchCase = null,
object matchByte = null,
object searchFormat = null){
Range retVal;
if (what.Length < 255)
{
retVal = rng.Find(what, after, lookIn, lookAt, searchOrder, searchDirection, matchCase, matchByte, searchFormat);
}
else
{
var whatSearch = what.Substring(0, 254) + "*";
retVal = rng.Find(whatSearch, after, lookIn, lookAt, searchOrder, searchDirection, matchCase, matchByte, searchFormat);
while (retVal != null && retVal.Cells.Value2 != what)
{
retVal = rng.Find(whatSearch, retVal, lookIn, lookAt, searchOrder, searchDirection, matchCase, matchByte, searchFormat);
}
}
return retVal;
}