【问题标题】:Inserting tab character into word document from VBA从VBA将制表符插入word文档
【发布时间】:2016-04-22 20:43:04
【问题描述】:

我有一个包含 5 行 2 列的表格结构的 Word 模板文件。每行的第一列都有一个标识符,模板标签,例如 。每个标签都不一样,所以 5 个是唯一的。

我打开模板文件,根据Excel工作表中的数据创建一个新的基于模板的word文档,模板标签替换为实际值。

我想“制表”到表格的第二列,而不是第一列中的所有文本。

这是我当前用于输出从 Excel 工作表中提取的值的代码。

    With wrdDoc
        .Content.Find.Execute FindText:="<PNAME>", ReplaceWith:="<Project Name> " & strProjectName
        .Content.Find.Execute FindText:="<PID>", ReplaceWith:="<Project ID>=" & strProjectID
        .Content.Find.Execute FindText:="<DNAME>", ReplaceWith:="<Department Name>=" & strDepartmentName
        .Content.Find.Execute FindText:="<A>", ReplaceWith:="<Active>=" & strActive
        .Content.Find.Execute FindText:="<HO>", ReplaceWith:="<Head Office>=" & strHeadoffice
    End With

我尝试过使用 chr(9) 和 vbTab,但它们只是在第一列中创建了一个选项卡。

【问题讨论】:

    标签: vba excel


    【解决方案1】:

    如果插入点在表格单元格中,您可以使用以下方式移动到下一个表格单元格:

    Selection.MoveRight Unit:=wdCell
    

    这会将光标移动到下一个单元格。

    【讨论】:

    • 插入点未知,见原代码,使用查找/替换
    • 我认为您不能使用查找/替换选项卡切换到下一个单元格。
    • 使用 .Find.Execute 依次查找(而不是替换)每个模板标签应该很简单,用合适的替换它,然后将选择移动到下一列并插入该列中还有一些文本(假设这是 OP 想要的,因为它并不完全清楚)。你不能做的是在一次调用 .Find.Execute 中完成所有操作。
    【解决方案2】:

    您可以使用多种方法将数据写入 Word 中的表格,其中一种基于您已有的:

    1.使用 Find.Execute

    如果您必须坚持使用模板中带有预定义标签的表格,那么您应该使用Range 对象进行查找,而不是使用Document.Content。这使您可以操作文档中的“目标位置”,类似于使用范围而不是从工作簿级别定位工作簿中的单元格的方式。

    Dim rngSearch as Word.Range
    Set rngSearch = wrdDoc.Content
    rngSearch.Find.Execute FindText:="<PNAME>", ReplaceWith:="<Project Name> " & strProjectName
    'Move to next cell
    rngSearch.Collapse wdCollapseEnd
    rngSearch.Text = "text for the second column"
    rngSearch.Find.Execute FindText:="<PID>", ReplaceWith:="<Project ID>=" & strProjectID
    'and repeat collapsing, assign text and Find.Execute
    

    代码结构考虑:将 Find/Collapse/Text 步骤放在单独的过程 (Sub) 中可能是有意义的。这样可以省去为每个“标签”重复这三个步骤:

      'code in main procedure
      Dim rngFind as Word.Range
      Set rngFind = wrdDoc.Content
      WriteToTagsInWordTable "<PNAME>", "<Project Name> " & strProjectName, 
                           "text in column two", rngFind
      WriteToTagsInWordTable "<PID>", "<Project Name>=" & strProjectID, 
                           "other text in column two", rngFind
      'and so on...
    
    Sub WriteToTagsInWordTable(sFind as String, sReplace as String, _
                               sColTwo as String, ByRef rngSearch as Word.Range)
    
      rngSearch.Find.Execute FindText:=sFind, ReplaceWith:=sReplace
      'Move to next cell
      rngSearch.Collapse wdCollapseEnd
      rngSearch.Text = sColTwo
    End Sub
    

    2。使用书签

    不要在 Word 文档中的字符串标签上使用 Find,而是将标签替换为 书签 并直接在代码中定位书签。这应该更快。您可以为第二列插入额外的书签,或者使用上述技术移动到下一个单元格。

    With wrdDoc
      .Bookmarks("PName").Range.Text = "<Project Name> " & strProjectName
      .Bookmarks("PNameInfo").Range.Text = "text for the second column"
    End With
    

    3.在代码中创建表格

    生成 in VBA 代码通常是最有效的,作为字符分隔的字符串,然后分配给目标 Range(通常由 Bookmark 表示),并转换为表格。这在我的 MSDN 文章 https://msdn.microsoft.com/en-us/library/aa537149%28v=office.11%29.aspx?f=255&MSPPError=-2147217396 的“使用数据填充单词表”部分中有详细描述。

    这是那篇文章中的相关代码。它展示了如何从数组中为多行、多列表构建字符串,将字符串传递给 Word Range 并将其转换为表格。

    Sub CreateTableFromString(ByRef rng As Word.Range, _
        ByRef adata() As Variant)
        Dim tbl As Word.Table
    
        rng.Text = BuildDataString(adata)
        Set tbl = rng.ConvertToTable(vbTab, _
            AutoFitBehavior:=wdAutoFitFixed, _
                DefaultTableBehavior:=wdWord8TableBehavior)
    End Sub
    
    Function BuildDataString(aData() As Variant) As String
        Dim dataString As String
        Dim nrRow As Long, nrCol As Long
    
        For nrRow = 0 To UBound(aData, 1)
            For nrCol = 0 To UBound(aData, 2)
                dataString = dataString & aData(nrRow, nrCol)
                If nrCol < UBound(aData, 2) _
                    Then dataString = dataString & vbTab
            Next nrCol
            If nrRow < UBound(aData, 1) _
                Then dataString = dataString & vbCr
        Next nrRow
        BuildDataString = dataString
    End Function 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-03-17
      相关资源
      最近更新 更多