VSTO:怎么判断光标仍在某个表格中

wgmok 2016-01-24 07:24:07
最近工作中经常会使用嵌套表格,如图,需要在首列进行排序及其他操作,我刚刚接触VSTO,无法判断光标在表格中,还是在外面的表格中.
以下为我的代码,请大家帮忙,谢谢!
Dim WordApp As Word.Application = Global.WgmWordAss.Globals.WgmWordAss.Application
Dim oDoc As Word.Document = WordApp.ActiveDocument
Dim M As Integer = 1

Dim RowNo, ColNo As Integer
Dim Tb As Word.Table
Dim Txt As String
If WordApp.Selection.Information(Word.WdInformation.wdWithInTable) Then
Tb = WordApp.Selection.Tables(1)
Dim Num As Integer = Tb.Rows.Count
Dim N As Integer = WordApp.Selection.Information(Word.WdInformation.wdEndOfRangeRowNumber)

While WordApp.Selection.Information(Word.WdInformation.wdWithInTable)
RowNo = WordApp.Selection.Information(Word.WdInformation.wdEndOfRangeRowNumber)
ColNo = WordApp.Selection.Information(Word.WdInformation.wdEndOfRangeColumnNumber)
Txt = Tb.Cell(RowNo, ColNo).Range.Text
If Strings.Left(Txt, 1) = "(" Then
Tb.Cell(RowNo, ColNo).Range.Text = "(" & CStr(Format(M, "000")) & ")"
Else
Tb.Cell(RowNo, ColNo).Range.Text = CStr(Format(M, "000"))
End If
M = M + 1
If M > Num - N + 1 Then
Exit While
End If
WordApp.Selection.MoveDown(Word.WdUnits.wdLine)
End While
End If
...全文
274 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
赵4老师 2016-01-26
  • 打赏
  • 举报
回复
帮结帖+蹭分
wgmok 2016-01-25
  • 打赏
  • 举报
回复
我知道了,修改代码如下:
Dim WordApp As Word.Application = Global.WgmWordAss.Globals.WgmWordAss.Application
Dim oDoc As Word.Document = WordApp.ActiveDocument
Dim M As Integer = 1

Dim RowNo, ColNo As Integer
Dim Tb As Word.Table
Dim Txt As String
If WordApp.Selection.Information(Word.WdInformation.wdWithInTable) Then
Tb = WordApp.Selection.Tables(1)
Dim Num As Integer = Tb.Rows.Count
Dim N As Integer = WordApp.Selection.Information(Word.WdInformation.wdEndOfRangeRowNumber)
Dim LastRowNo as Integer=0
While WordApp.Selection.Information(Word.WdInformation.wdWithInTable)
RowNo = WordApp.Selection.Information(Word.WdInformation.wdEndOfRangeRowNumber)
ColNo = WordApp.Selection.Information(Word.WdInformation.wdEndOfRangeColumnNumber)
if RowNo>LastRowNo then
Txt = Tb.Cell(RowNo, ColNo).Range.Text
If Strings.Left(Txt, 1) = "(" Then
Tb.Cell(RowNo, ColNo).Range.Text = "(" & CStr(Format(M, "000")) & ")"
Else
Tb.Cell(RowNo, ColNo).Range.Text = CStr(Format(M, "000"))
End If
M = M + 1
LastRowNo=RowNo
If M > Num - N + 1 Then
Exit While
WordApp.Selection.MoveDown(Word.WdUnits.wdLine)
end if
else
Exit While
End if
End While
End If

但我不知道是不是有更简单的.

863

社区成员

发帖
与我相关
我的任务
社区描述
VB COM/DCOM/COM+
c++ 技术论坛(原bbs)
社区管理员
  • COM/DCOM/COM+社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧