VSTO本地化问题.Findnext
VSTO Localization issue .Findnext
我在 Excel VSTO 解决方案中遇到了 .Findnext 的一个奇怪问题。该代码在英语语言设置下运行良好,但在德语语言设置下失败并出现以下错误。
System.NullReferenceException: 'Object reference not set to an instance of an object.'
调查时,EndRng = .FindNext(EndRng) 没有返回任何内容。这个块怎么可能在英语设置下工作而不是在德国设置下?
Dim StartRng As Excel.Range, EndRng As Excel.Range
Dim wrkSheet As Worksheet
Dim wb As Excel.Workbook = Globals.ThisAddIn.Application.ActiveWorkbook
Dim xlapp As Excel.Application = Globals.ThisAddIn.Application
Dim FormulaToFind As String = FormulaToLocal(FormulaString)
wrkSheet.Range(wrkSheet.Cells(RowStart, ColStart), wrkSheet.Cells(RowEnd, ColEnd)).Select()
With xlapp.ActiveWindow.Selection
StartRng = .Find(FormulaToFind, LookIn:=Excel.XlFindLookIn.xlFormulas, LookAt:=Excel.XlLookAt.xlPart)
If Not StartRng Is Nothing Then
EndRng = StartRng
StartAddress = StartRng.Address
Do
EndAddress = EndRng.Address
EndRng = .FindNext(EndRng)
Loop While Not EndRng Is Nothing And EndRng.Address <> StartAddress
End If
End With
下面是将公式转换为本地的函数
Public Function FormulaToLocal(ByVal RefFormula As String) As String
Dim wb As Excel.Workbook = Globals.ThisAddIn.Application.ActiveWorkbook
Dim TmpSheet As String = "TmpSheet"
wb.Worksheets(TmpSheet).Range("AZ1").Formula = RefFormula
FormulaToLocal= wb.Worksheets(TmpSheet).Range("AZ1").Formulalocal
wb.Worksheets(TmpSheet).Range("AZ1").value = ""
End Function
Microsoft 承认与此有关的一些问题。他们正在与高级工程师一起调查。以下是 Microsoft Community 的 Andreas 建议的替代解决方案。
With rngData
StartRng = .Find(FormulaToFind, LookIn:=Excel.XlFindLookIn.xlFormulas, LookAt:=Excel.XlLookAt.xlPart,
SearchOrder:=Excel.XlSearchOrder.xlByRows, SearchDirection:=Excel.XlSearchDirection.xlNext)
If Not StartRng Is Nothing Then
EndRng = StartRng
StartAddress = StartRng.Address
Do
EndAddress = EndRng.Address
EndRng = .Find(FormulaToFind, After:=EndRng, LookIn:=Excel.XlFindLookIn.xlFormulas, LookAt:=Excel.XlLookAt.xlPart,
SearchOrder:=Excel.XlSearchOrder.xlByRows, SearchDirection:=Excel.XlSearchDirection.xlNext)
Loop While Not EndRng Is Nothing And EndRng.Address <> StartAddress
End If
End With
我在 Excel VSTO 解决方案中遇到了 .Findnext 的一个奇怪问题。该代码在英语语言设置下运行良好,但在德语语言设置下失败并出现以下错误。
System.NullReferenceException: 'Object reference not set to an instance of an object.'
调查时,EndRng = .FindNext(EndRng) 没有返回任何内容。这个块怎么可能在英语设置下工作而不是在德国设置下?
Dim StartRng As Excel.Range, EndRng As Excel.Range
Dim wrkSheet As Worksheet
Dim wb As Excel.Workbook = Globals.ThisAddIn.Application.ActiveWorkbook
Dim xlapp As Excel.Application = Globals.ThisAddIn.Application
Dim FormulaToFind As String = FormulaToLocal(FormulaString)
wrkSheet.Range(wrkSheet.Cells(RowStart, ColStart), wrkSheet.Cells(RowEnd, ColEnd)).Select()
With xlapp.ActiveWindow.Selection
StartRng = .Find(FormulaToFind, LookIn:=Excel.XlFindLookIn.xlFormulas, LookAt:=Excel.XlLookAt.xlPart)
If Not StartRng Is Nothing Then
EndRng = StartRng
StartAddress = StartRng.Address
Do
EndAddress = EndRng.Address
EndRng = .FindNext(EndRng)
Loop While Not EndRng Is Nothing And EndRng.Address <> StartAddress
End If
End With
下面是将公式转换为本地的函数
Public Function FormulaToLocal(ByVal RefFormula As String) As String
Dim wb As Excel.Workbook = Globals.ThisAddIn.Application.ActiveWorkbook
Dim TmpSheet As String = "TmpSheet"
wb.Worksheets(TmpSheet).Range("AZ1").Formula = RefFormula
FormulaToLocal= wb.Worksheets(TmpSheet).Range("AZ1").Formulalocal
wb.Worksheets(TmpSheet).Range("AZ1").value = ""
End Function
Microsoft 承认与此有关的一些问题。他们正在与高级工程师一起调查。以下是 Microsoft Community 的 Andreas 建议的替代解决方案。
With rngData
StartRng = .Find(FormulaToFind, LookIn:=Excel.XlFindLookIn.xlFormulas, LookAt:=Excel.XlLookAt.xlPart,
SearchOrder:=Excel.XlSearchOrder.xlByRows, SearchDirection:=Excel.XlSearchDirection.xlNext)
If Not StartRng Is Nothing Then
EndRng = StartRng
StartAddress = StartRng.Address
Do
EndAddress = EndRng.Address
EndRng = .Find(FormulaToFind, After:=EndRng, LookIn:=Excel.XlFindLookIn.xlFormulas, LookAt:=Excel.XlLookAt.xlPart,
SearchOrder:=Excel.XlSearchOrder.xlByRows, SearchDirection:=Excel.XlSearchDirection.xlNext)
Loop While Not EndRng Is Nothing And EndRng.Address <> StartAddress
End If
End With