Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/macos/9.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宏将文本框添加到Word文档表_Vba_Ms Word_Report_Reporting - Fatal编程技术网

通过VBA宏将文本框添加到Word文档表

通过VBA宏将文本框添加到Word文档表,vba,ms-word,report,reporting,Vba,Ms Word,Report,Reporting,情况如下: 我正在使用SQL Server Reporting Service生成导出到Word 2010的报告。报告本身显示为一组嵌套表。我需要能够进入其中一个内部表格,并在表格中的特定单元格中添加一个文本框 我需要以一种我可以循环的方式来做这件事,循环特定表中的所有行。该表可以有n行,每个行中都需要修改此单元格。因此,问题是双重的。我需要能够索引到正确的表中,并获得一个指向特定单元格的指针,然后我需要修改单元格的内容,使其具有单个文本框控件。据我所知,您使用shapes集合来添加文本框本身,

情况如下:

我正在使用SQL Server Reporting Service生成导出到Word 2010的报告。报告本身显示为一组嵌套表。我需要能够进入其中一个内部表格,并在表格中的特定单元格中添加一个文本框

我需要以一种我可以循环的方式来做这件事,循环特定表中的所有行。该表可以有n行,每个行中都需要修改此单元格。因此,问题是双重的。我需要能够索引到正确的表中,并获得一个指向特定单元格的指针,然后我需要修改单元格的内容,使其具有单个文本框控件。据我所知,您使用shapes集合来添加文本框本身,但我不知道如何获取对特定表的特定单元格的引用,以及如何查找与其关联的shapes集合

在此方面的任何帮助都将不胜感激

我正在使用这段代码尝试迭代文档中的表,但是没有HasTable属性,只有一个用于HasChart和HasMartart的属性

Dim Shp As Shape
For Each Shp In ThisDocument.InlineShapes 
    If Shp.HasTable Then 
        MsgBox "Found Table" 
    End If 
Next Shp
这段代码将添加一个文本框,但我不知道如何将其放在我正在使用的表的右列中,也不知道如何在添加文本框的表中的所有行中建立索引:

ActiveDocument.Shapes.AddTextbox _
    Orientation:=msoTextOrientationHorizontal, _
    Left:=lLeft, 
    Top:=6, _
    Width:=72, _
    Height:=12
我尝试了以下方法,但似乎没有效果:

为ActiveDocument.Tables中的每个tbl将tbl标注为Word.Table

    tbl.Columns.Select


    If tbl.Tables.Count > 0 Then
      tbl.Tables(1).Select

          ActiveDocument.Shapes.AddTextbox Orientation:=msoTextOrientationHorizontal, _
          Left:=tbl.Tables(1).Columns.Borders.DistanceFromLeft, _
          Top:=tbl.Tables(1).Columns.Borders.DistanceFromTop, _
          Width:=72, _
          Height:=12

    End If
Next tbl
这将添加文本框,但不会将其放在正确的单元格中


XAML中的可视树是一个层次数据结构,包含XAML页面的所有可视元素。您可以递归地遍历它以查找特定节点,然后在找到该节点后修改给定节点的内容。这就是我在这里试图做的,但我没有看到这种结构从这里找到的代码中被大量修改:

尝试此操作,可能需要对特定用途进行一些微调,但这将添加一个相对于表中任何单元格的文本框,并将其定位点设置到表中

未测试的修订版应查找多个占位符在strText变量中指定占位符文本,并在每个表中为每个占位符添加具有相应单元格的文本框

Option Explicit
Sub AddTextBoxToTableCell()
Dim tbl As Table
Dim tblRow As Row
Dim t As Integer
Dim bkmark As String
Dim tblCell As Cell
Dim clTop As Long
Dim clLeft As Long
Dim tbWidth As Single
Dim tbHeight As Single
Dim strText as String


'## Specify dimensions of textbox:
    tbWidth = 72
    tbHeight = 10

'## Specify the placeholder text
    strText = "[placeholder for textbox]"

'## Iterate the tables
For Each tbl In ActiveDocument.Tables

    t = t + 1


    '## Define the cell we want to use (modify as needed)
    For r = 1 to tbl.Rows.Count
        For c = 1 to tbl.Columns.Count
            If tbl.Cell(r,c).Range.Text = strText then
               '## Construct a string to use as a Bookmark
                bkmark = "table_" & t & "_Row" & r & "_Col" & c
                Set tblCell = tbl.Cell(r, c)


                '## Get the position of the cell
                clLeft = GetCellAbsoluteLeft(tblCell)
                clTop = tblCell.Range.Information(wdVerticalPositionRelativeToPage)

                '## Add a bookmark if it does not already exist
                If ActiveDocument.Bookmarks.Exists(bkmark) Then
                    ActiveDocument.Bookmarks(bkmark).Delete
                End If

                '## Add/Update the bookmark location
                ActiveDocument.Bookmarks.Add "table_" & t, tblCell.Range

                '## Add a textbox to your table:
                ActiveDocument.Shapes.AddTextbox msoTextOrientationHorizontal, _
                    clLeft, clTop, tbWidth, tbHeight, _
                    Anchor:=ActiveDocument.Bookmarks(bkmark).Range
            End If
        Next
    Next


Next


End Sub

Function GetCellAbsoluteLeft(cl As Cell) As Long
'## Custom function to return the absolute left position in points, of a table cell
Dim col As Integer
Dim c As Integer
Dim ret As Long

ret = cl.Range.Information(wdHorizontalPositionRelativeToPage)

col = cl.Range.Information(wdStartOfRangeColumnNumber)

c = 1

'## Add up the column widths with the position wdHorizontalPositionRelativeToPage
Do
    ret = ret + cl.Row.Cells(c).Width
    c = c + 1
Loop While Not c >= col

'## Return the value:
GetCellAbsoluteLeft = ret

End Function

到目前为止你试过什么?我确信表是文档的InlineShapes集合的成员。迭代该集合,检查每个形状的.HasTable=True属性,然后您应该能够以这种方式处理该表。谢谢!我尝试了各种方法,试图通过ActiveDocument.Shapes集合和ThisDocument.Tables集合建立索引。不过,我还没有试过InlineShapes。这似乎让我得到了一些形状,但形状对象没有HasTable属性。这是我用来测试的代码:[quote]将Shp设置为本文档中每个Shp的形状。InlineShapes如果Shp.HasTable,那么MsgBox找到表格结束如果下一个Shp[/quote]现在有HasTable属性,HasChart和HasSmartArtOK只有一个。请稍候。。。我主要是在PowerPoint中工作,所以可能会有点不同。我会跟进……再补充一点信息,这段代码将添加一个文本框:[code]ActiveDocument.Shapes.AddTextbox方向:=msoTextOrientationHorizontal,Left:=lLeft,uUft,uuUt:=6,Width:=72,Height:=12[/code],但我不知道如何将其放在我正在处理的表的右列中,这也不能让我通过添加文本框来索引表中的所有行。我来自C/XAML背景。Word中是否有类似于可视树的内容?请您将代码放在问题中,而不是放在无法阅读的注释中……这让我找到了正确的方向。我仍然需要在表格中进行排序,寻找一个占位符字符串,但我认为当我找到正确的单元格时,这会将文本框放在我需要的位置。非常感谢您的帮助。每个表中只有一个占位符吗?修改为在每个表中查找一个占位符文本。我现在基本上在那里。人战胜机器:再次感谢你的帮助。我已经在这个问题上绕了一天多了/表中有n个占位符。我正在处理的特定报表只有一个,但在生产环境中,它可以显示任意数量的行,每个行都有自己的占位符。