C# 使用Microsoft.Office.Interop.Word查找选定单元格的索引
注意:我已经使用蛮力双for循环解决了这个问题。我需要一种更快、更有效的方法来解决这个问题,特别是在C#中 考虑下表中的单词 我需要一种有效的方法查找给定的列和行,并将文本放置在相交单元格中。例如,假设我的列为1,行为A。然后文本将进入索引行为2、列为2的单元格 我尝试过的事情: 迭代所有行和列。这是缓慢的,需要大量的时间。 为了加快速度,我尝试使用C# 使用Microsoft.Office.Interop.Word查找选定单元格的索引,c#,ms-word,office-interop,C#,Ms Word,Office Interop,注意:我已经使用蛮力双for循环解决了这个问题。我需要一种更快、更有效的方法来解决这个问题,特别是在C#中 考虑下表中的单词 我需要一种有效的方法查找给定的列和行,并将文本放置在相交单元格中。例如,假设我的列为1,行为A。然后文本将进入索引行为2、列为2的单元格 我尝试过的事情: 迭代所有行和列。这是缓慢的,需要大量的时间。 为了加快速度,我尝试使用Range.Find,但很难知道找到的单元格在表上下文中的位置。这是我所想的一些粗略的代码。我很清楚,如果找不到物品或有多个物品,这是缺少检查,但
Range.Find
,但很难知道找到的单元格在表上下文中的位置。这是我所想的一些粗略的代码。我很清楚,如果找不到物品或有多个物品,这是缺少检查,但我可以稍后处理
int col;
int row;
var searchRange = table.Range;
var isFound = searchRange.Find.Execute(FindText:rowText);
if(isFound){
searchRange.Select();
col = searchRange.SOME_FUNCTION_THAT_WILL_REVEAL_THE_CELLS_COL_INDEX_WITHIN_THE_TABLE();
}
var searchRange = table.Range;
var isFound = searchRange.Find.Execute(FindText:rowText);
if(isFound){
searchRange.Select();
row = searchRange.SOME_FUNCTION_THAT_WILL_REVEAL_THE_CELLS_ROW_INDEX_WITHIN_THE_TABLE();
}
table.Cell(row, col).Text = "Some text For Desired Location";
这正是你想要的。将其粘贴到VBA模块中,首先在表外选择,然后通过代码执行F8。接下来,让鼠标光标选择表中的一个单元格,然后通过以下代码按F8键:
Sub SelectionInfo()
'
Dim iSelectionRowEnd As Integer
Dim iSelectionRowStart As Integer
Dim iSelectionColumnEnd As Integer
Dim iSelectionColumnStart As Integer
Dim lngStart As Long
Dim lngEnd As Long
' Check if Selection IS in a table
' if not, exit Sub after message
If Selection.Information(wdWithInTable) = False Then
MsgBox "Selection is not in a table. Exiting macro."
Else
lngStart = Selection.Range.Start
lngEnd = Selection.Range.End
' get the numbers for the END of the selection range
iSelectionRowEnd = Selection.Information(wdEndOfRangeRowNumber)
iSelectionColumnEnd = Selection.Information(wdEndOfRangeColumnNumber)
' collapse the selection range
Selection.Collapse Direction:=wdCollapseStart
' get the numbers for the END of the selection range
' now of course the START of the previous selection
iSelectionRowStart = Selection.Information(wdEndOfRangeRowNumber)
iSelectionColumnStart = Selection.Information(wdEndOfRangeColumnNumber)
' RESELECT the same range
Selection.MoveEnd Unit:=wdCharacter, Count:=lngEnd - lngStart
' display the range of cells covered by the selection
MsgBox "The selection covers " & Selection.Cells.Count & " cells, from Cell(" & _
iSelectionRowStart & "," & iSelectionColumnStart & ") to Cell(" & _
iSelectionRowEnd & "," & iSelectionColumnEnd & ")."
End If
End Sub
REF:我将此标记为正确答案,因为它与答案最接近。我一直在寻找C语言的东西,但由于VBA和C语言非常相似,我会接受它。对于稍后了解此内容的人,将为您提供表中位置的函数是:(ROW)
(int)searchRange.Information[Word.WdInformation.wdStartOfRangeRowNumber]
(COL)(int)searchRange.Information[Word.wdstartofrangecolumnname]
我可以将其翻译为c#,也许今天下午吧。我想这会很好的,它结束了整个问题,谢谢你的帮助