2015-01-16 81 views
0

(这应该propably简单,但不知何故,我无法找到一个解决方案。)当前读取选择线

我只是想读我的选择的当前行进入VBA的变量。我不知道当前的段落。选择是在线的开始。

我的文档看起来像这样。所有的

My Word doc

首先,我选中表格的第一行。然后我移动一个段落。现在这就是我想要的路线。正如你在我的第二个img中看到的,我只有第一个字符。

For Each tbl In fileInsertRange.Tables 
    tbl.Rows(1).Select 
    ' save caption 
    Selection.Collapse 
    Selection.MoveUp WdUnits.wdParagraph, 1 

    tableCaption = Selection.Text 

Debugging

+0

你的编辑改变了很多你以前的问题的内容,我删除了我的答案。 –

回答

1

如果您希望将所有表格标题存储在比试试这个代码的变量。请记住,您需要立即使用tableCaption变量,然后才能被下一个表格标题覆盖,或者添加一个数组来存储所有标题。

Sub get_table_caption() 

Dim currentTable As Table 
Dim tableCaption As String 

'Loop through all tables on active document 
For Each currentTable In ActiveDocument.Tables 

    'Get tables caption and store in a variable called "tableCaption" 
    currentTable.Select 
    Selection.Collapse 
    Selection.MoveUp WdUnits.wdParagraph, 1 
    Selection.Expand wdLine 
    tableCaption = Selection.Text 
    Debug.Print tableCaption 

    'Do stuff with the tables caption 

Next 
End Sub 

如果你想继续通过选择表的第一行,发现表的标题比试试这个代码做你的方式:

Sub get_table_caption() 

Dim tableCaption As String 

    'Get tables caption and store in a variable called "tableCaption" 
    Selection.Collapse 
    Selection.MoveUp WdUnits.wdParagraph, 1 
    Selection.Expand wdLine 
    tableCaption = Selection.Text 
    Debug.Print tableCaption 

End Sub 

希望有所帮助。祝你好运。

+0

'Selection.Expand wdLine'这就是我需要的。谢谢。 – blckbird