Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vba 查找表格单元格书签是否在?_Vba_Data Binding_Ms Word_Bookmarks - Fatal编程技术网

Vba 查找表格单元格书签是否在?

Vba 查找表格单元格书签是否在?,vba,data-binding,ms-word,bookmarks,Vba,Data Binding,Ms Word,Bookmarks,我试图找出我的书签在Word文档中的哪个表单元格中。我在书签中循环没有问题,这很简单。现在,我正在尝试识别书签所在的表格单元格,但我很难做到这一点 或者,是否有方法将数据绑定到书签(如使用附带的书签),然后可以引用并复制到另一个文档?我不能使用附带的书签,因为单元格中的文本需要频繁更改,用户不希望每次都要为新文本添加书签。如上所述,更好的方法是浏览表格,找到书签。下面的脚本遍历第一个表的第二列,查找所有书签。它找到的书签对应于我在另一个文档“documenttopopulate.docx”中设置

我试图找出我的书签在Word文档中的哪个表单元格中。我在书签中循环没有问题,这很简单。现在,我正在尝试识别书签所在的表格单元格,但我很难做到这一点


或者,是否有方法将数据绑定到书签(如使用附带的书签),然后可以引用并复制到另一个文档?我不能使用附带的书签,因为单元格中的文本需要频繁更改,用户不希望每次都要为新文本添加书签。

如上所述,更好的方法是浏览表格,找到书签。下面的脚本遍历第一个表的第二列,查找所有书签。它找到的书签对应于我在另一个文档“documenttopopulate.docx”中设置的书签。ActiveDocument中的数据填充到第二个文档中,只要找到书签,如下所示:

Sub AutomateQuestionnaire2()
' Prototype 2
' Different approach was used, instead of looping through bookmarks, loop
' through the tables looking for bookmarks. This method is more flexible
' and  is better suited for our Word documents which always include tables.
' Limitations: Bookmark needs to be in both documents using the same ID, and
' data must be in a table, column 2.

Dim oRow As Row
Dim oRange As Range
Dim oFindRange As Range
Dim oBookmark As Bookmark
Dim oQuestionnaire As Word.Document
Dim oApp As Word.Application

Dim strFilePath As String
Dim strText As String

strFilePath = ActiveDocument.Path

'Open the second to populate it
Set oApp = New Word.Application
oApp.Visible = True
Set oQuestionnaire = oApp.Documents.Open(strFilePath + "\Document to Populate.docx")

'We'll loop through each row of the table looking for bookmarks, if a bookmark is found
'the text adjacent to that bookmark, in the table cell, will be copied to the same
'bookmark if found in the questionnaire.
For Each oRow In ActiveDocument.Tables(1).Rows
    'Limits the range to the middle column as is the case for the ITGC 532 form
    Set oRange = oRow.Cells(2).Range
    Set oBookmark = oRange.Bookmarks(1)

    'VBA will terminate the script if it comes across an error, instead
    'let's add some error handling to skip errors.
    On Error GoTo SkipToNext

    strText = oRange.Text
    oQuestionnaire.Bookmarks(oBookmark).Range.Text = strText

    'Find the newly inputted text and differentiate it (bold for now)
    Set oFindRange = oQuestionnaire.Content
    oFindRange.Find.Execute FindText:=strText, Forward:=True
    If oFindRange.Find.Found = True Then oFindRange.Font.ColorIndex = wdBlue

SkipToNext:

Next

我使用了此链接中提供的RowIndex和ColumnIndex-

一旦获得这些值,就可以访问表的单元格,如-

ActiveDocument.Tables(1).Cell(rIndex, cIndex)

不需要遍历表,更不用说它们的行和列了。下面是一些非常简单的代码供您使用:

Sub TestBookMark(BkMkNm As String)
Dim Rng As Range
With ActiveDocument
  Set Rng = .Range(0, 0)
  With .Bookmarks(BkMkNm).Range
  If .Information(wdWithInTable) = True Then
    Rng.End = .End
    MsgBox "Bookmark: " & BkMkNm & vbTab & "Table: " & Rng.Tables.Count & vbTab & "Row: " & .Cells(1).RowIndex & vbTab & "Column: " & .Cells(1).ColumnIndex
  End If
  End With
End With
End Sub
您可以使用如下代码调用:

Sub Demo()
Call TestTable("BkMk")
End Sub

显然,可以通过书签实现一个循环来执行多个测试。这应该比测试每个表/单元格的效率要高得多。

实际上,我通过另一种途径解决了这个问题:我不是循环遍历书签然后查找表单元格,而是循环遍历表单元格并查找书签。这样很容易,我不知道换一种方式是否可行。供其他遇到此问题的人参考。为什么不发布解决方案作为答案?不知道这是可能的,我现在发布代码。代码中的一些注释并不完美,因为我在发布时对其进行了调整。调查问卷是我试图填充的内容。
Sub Demo()
Call TestTable("BkMk")
End Sub