Home > Software engineering >  In the word how to obtain the index number of the selected form?
In the word how to obtain the index number of the selected form?

Time:09-20

Have more than one table in a document, I want to get the mouse the index number of the selected form, namely the ActiveDocument. Tables (I) in the I

CodePudding user response:

If Selection. Information (wdWithInTable)=True Then Selection. The Tables (1). Select

Through infomation whether the cursor is inside the table
If true then selection table (1) is this table objects, as for his index value in the entire document tables I temporarily also don't know how to obtain,

CodePudding user response:

Fix, "stickers to

 
Sub test ()
Dim t1 As Object
Dim t2 As Object
Dim As Integer I


If Selection. Information (wdWithInTable)=True Then
Set the t1=Selection. Tables (1)
For I=1 To ActiveDocument. Tables. Count
The Set t2=ActiveDocument. Tables (I)
If t1=t2 Then
MsgBox "the tables (" & amp; I & amp;
")"End the If
Next
The Else
MsgBox "cursor is not in the table"
End the If
End Sub

CodePudding user response:

refer to the second floor chewinggum response:
, and stick to points

 
Sub test ()
Dim t1 As Object
Dim t2 As Object
Dim As Integer I


If Selection. Information (wdWithInTable)=True Then
Set the t1=Selection. Tables (1)
For I=1 To ActiveDocument. Tables. Count
The Set t2=ActiveDocument. Tables (I)
If t1=t2 Then
MsgBox "the tables (" & amp; I & amp;
")"End the If
Next
The Else
MsgBox "cursor is not in the table"
End the If
End Sub


I just test, there may be the problem of "mistakes",



Test method is as follows:
1. Create a Word document, and just insert a table (any size is fine, such as 3 x 2 rows);
2. The selected table, Ctrl + C and then Ctrl + V (optional, also line 1) twice, now in the document is "three forms",
Set the "cursor" in any one of these forms,
3. The Alt + F11 opened the VBE varies, insert a standard module, paste the code above,
Run, you can see: how many tables, how many times will MsgBox, has nothing to do with the cursor in what form,


Of course I was "actively"... You can ignore my response,

CodePudding user response:

reference Chen8013 reply: 3/f
Quote: refer to the second floor chewinggum response:

Fix, "stickers to

 
Sub test ()
Dim t1 As Object
Dim t2 As Object
Dim As Integer I


If Selection. Information (wdWithInTable)=True Then
Set the t1=Selection. Tables (1)
For I=1 To ActiveDocument. Tables. Count
The Set t2=ActiveDocument. Tables (I)
If t1=t2 Then
MsgBox "the tables (" & amp; I & amp;
")"End the If
Next
The Else
MsgBox "cursor is not in the table"
End the If
End Sub


I just test, there may be the problem of "mistakes",



Test method is as follows:
1. Create a Word document, and just insert a table (any size is fine, such as 3 x 2 rows);
2. The selected table, Ctrl + C and then Ctrl + V (optional, also line 1) twice, now in the document is "three forms",
Set the "cursor" in any one of these forms,
3. The Alt + F11 opened the VBE varies, insert a standard module, paste the code above,
Run, you can see: how many tables, how many times will MsgBox, has nothing to do with the cursor in what form,


Of course I was "actively"... You can ignore my response,


Well, didn't test the situation

CodePudding user response:

Revised, add a condition

 
Sub test ()
Dim t1 As Object
Dim t2 As Object
Dim As Integer I


If Selection. Information (wdWithInTable)=True Then
Set the t1=Selection. Tables (1)
For I=1 To ActiveDocument. Tables. Count
The Set t2=ActiveDocument. Tables (I)
If t1=t2 And t1. Range. End=t2. Range. The End Then
MsgBox "the tables (" & amp; I & amp;
")"End the If
Next
The Else
MsgBox "cursor is not in the table"
End the If
End Sub

CodePudding user response:

reference 5 floor chewinggum reply:
revised, add a condition

 
Sub test ()
Dim t1 As Object
Dim t2 As Object
Dim As Integer I


If Selection. Information (wdWithInTable)=True Then
Set the t1=Selection. Tables (1)
For I=1 To ActiveDocument. Tables. Count
The Set t2=ActiveDocument. Tables (I)
If t1=t2 And t1. Range. End=t2. Range. The End Then
MsgBox "the tables (" & amp; I & amp;
")"End the If
Next
The Else
MsgBox "cursor is not in the table"
End the If
End Sub

Fierce!


But what really don't have to "add condition", the code line 11 just like this:
If the t1. Range. End=t2. Range. The End Then

CodePudding user response:

refer to 6th floor Chen8013 response:
Quote: refer to fifth floor chewinggum response:

Revised, add a condition

 
Sub test ()
Dim t1 As Object
Dim t2 As Object
Dim As Integer I


If Selection. Information (wdWithInTable)=True Then
Set the t1=Selection. Tables (1)
For I=1 To ActiveDocument. Tables. Count
The Set t2=ActiveDocument. Tables (I)
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  •  Tags:  
  • VBA
  • Related