将数据从 Excel 导出到 Word:数据目标/占位符 [英] Exporting data from Excel to Word: data targets/placeholders

查看:64
本文介绍了将数据从 Excel 导出到 Word:数据目标/占位符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Excel 中,我有一个用户表单,其中包含一个使用 RowSource=myTable 从 Excel 表填充的 ListBox.

In Excel I have a userform that has a ListBox being populated from an Excel table using RowSource=myTable.

现在我想选择一行,按下一个按钮,然后将所选行中的每一列导出到 Word 文档中的不同位置,该文​​档是带有预格式化文本的模板.Column1应该去place1,column2去place2,无论我想要什么......

Now I want to select one line, press a button, and export every column from the selected row to different places on a Word document, which is a template with pre-formated text. Column1 should go to place1, column2 to place2, whatever I want...

Word 中有哪些数据目标/占位符可用于插入数据?

What data targets/placeholders are available in Word for inserting data?

推荐答案

Word 为开发人员编写将显示在文档中的数据提供了多种可能性.使用哪种取决于开发者的个人需求.

Word has multiple possibilities for the developer to write data that will be displayed in the document. Which to use depends on the individual requirement of the developer.

  1. 传统上,书签是最常用的.Bookmark 对象提供了一个 Range 属性,以便可以格式化或以其他方式操作插入的内容.可以在文档的其他地方引用书签作为交叉引用或多次重复信息.
  1. Traditionally, bookmarks are most often used. A Bookmark object provides a Range property so that the inserted content can be formatted or otherwise manipulated. A bookmark can be referenced elsewhere in the document as a cross-reference or to repeat the information more than one time.

用于写入书签的 VBA 代码

VBA code to write to a bookmark

 ActiveDocument.Bookmarks("BookmarkName").Range.Text = "Data as string"

写入带有内容的书签会删除书签.如果要保留书签

Writing to a bookmark with content deletes the bookmark. If the bookmark should be retained

Dim sBookmarkName as String
Dim rngBookmark as Word.Range
Dim doc as Word.Document
Dim bkm as Word.Bookmark
sBookmarkName = "BookmarkName"
Set doc = ActiveDocument
If doc.Bookmarks.Exists(sBookmarkName) Then
  Set bkm = doc.Bookmarks(sBookmarkName)
  Set rng = bkm.Range
  rng.Text = "Data as string"
  doc.Bookmarks.Add(sBookmarkName, rngBookmark)
End If

  1. 文本内容可以写入Document.VariableCustomDocumentProperty 对象,并通过使用DocVariableDocProperty 字段.这具有内容随文档移动的优点,无论用户是否编辑表面上显示的内容.格式化内容有问题.
  1. Text content can be written to Document.Variable or CustomDocumentProperty objects and the content reflected on the document surface by use of DocVariable or DocProperty fields. This has the advantage of the content travelling with the document, whether the user edits what's displayed on the surface, or not. Formatting the content is problematic.

写入文档变量和自定义文档属性:

To write to document Variables and custom document properties:

ActiveDocument.Variables("VariableName") = "Data as string"
'See the language reference for more about values for Type
ActiveDocument.CustomDocumentProperties.Add Name:="PropertyName", _
                                            LinkToContent:=False,  _
                                            Type:=msoPropertyTypeString, _
                                            Value:="Data as string"

  1. 从 Word 2007 开始,内容控件被 Microsoft 推荐为数据占位符.以编程方式处理它们类似于使用书签.主要区别:
    • 内容控件的标题或标签可以在文档中多次使用
    • 写入内容控件不需要(但可以)删除内容控件(写入书签会删除书签,但如果将来需要引用或写入数据,可以重新创建)
    • 内容控件可以链接到文档中的自定义 XML 部分
    • 写入内容控件的数据可以通过 Office Open XML 文件格式更轻松地从 封闭 Word 文档中提取.(如果内容控件链接到自定义 XML 部分,则更容易)
    • 推论:还可以将数据直接写入链接到文档中的内容控件的自定义 XML 部分.如果数据应该写入关闭文件,这一点尤其有趣.
  1. Starting in Word 2007, content controls are recommended by Microsoft as data placeholders. Programmatically addressing them is similar to working with bookmarks. Main differences:
    • the title or tag of a content control can be used more than once in the document
    • writing to a content control need not (but can) remove the content control (writing to a bookmark removes the bookmark, but it can be recreated if it's needed for referencing or writing data at a future time)
    • a content control can be linked to a Custom XML Part in the document
    • data written to content controls can be more easily extracted from the closed Word document, via the Office Open XML file format. (Even more easily if the content control is linked to a Custom XML Part)
    • Corollary: it's also possible to write data directly to a Custom XML Part that's linked to content controls in a document. This is especially interesting if the data should be written to a closed file.

由于内容控件可以具有相同的标题或标签,对象模型为这些属性返回一个数组.Word 还为每个内容控件分配一个唯一 ID (GUID) - 如果 GUID 已知,则可以直接寻址内容控件.

Since content controls can have the same title or tag, the object model returns an array for these properties. Word also assigns each content control a unique ID (GUID) - if the GUID is known, the content control can be addressed directly.

Dim doc as Word.Document
Dim sCC_ID as String
Dim sCC_Title as String
Dim sCC_Tag as String

Set doc = ActiveDocument
'ID value is passed as a string
doc.ContentControls("1691881769").Range.Text = "Data as String"

'Get the first content control with the given title
sCC_Title = "Content Control Title"
doc.ContentControls.SelectContentControlByTitle(sCC_Title).Item(1).Range.Text = "Data as String"

'Loop all content controls having the same tag
Dim aCC as Word.ContentControls
Dim cc as Word.ContentControl

sCC_Tag = "Content Control Tag"
Set aCC = doc.SelectContentControlsByTag("Content Control Tag")
For Each cc In aCC
    cc.Range.Text = "Data as string"
Next

  1. 可以将数据写入合并字段,尽管这不是合并字段的用途.当想法是用户可以轻松"设置文档时,最常使用这种方法.为合并字段编写代码比其他类型的占位符更复杂.
  1. It's possible to write data to merge fields, although this NOT how merge fields are meant to be used. This approach is most often used when the idea is that a user can set up a document "easily". Writing the code to target the merge fields is more complex than for the other types of placeholders.

以下代码段演示了如何循环文档中未链接到邮件合并数据源的所有字段,搜索合并字段并将其替换为数据

The following snippet demonstrates how to loop all fields in document not linked to a mail merge data source, search for the merge field and replace it with data

Dim fld As Word.Field
Dim fldRange As Word.Range
Dim sFldCode As String, sDataField As String, sDataContent

sDataField = "MergefieldName"
sDataContent = "Data as string"
For Each fld In ActiveDocument.Fields
    sFldCode = fld.code
    Select Case InStr(sFldCode, sDataField)
        Case Is > 0
          Set fldRange = fld.result
          fld.Delete
          fldRange = sDataContent
    End Select
Next

  1. 查找/替换也是一种选择.通常,某种特殊字符分隔占位符"的开始和结束.代码搜索这些字符,读取占位符并在找到"位置插入数据.
  1. Find/Replace is also an option. Usually, some kind of special character delimits the start and end of the "placeholder". The code searches these characters, reads the placeholder and inserts data at the "found" position.

简单的搜索代码:

Dim wdDoc As Word.Document
Dim sPlaceholder as String, sData as String

sData = "Data as string"
sPlaceholder = "DataFieldName"
Set wdDoc = ActiveDocument

With wdDoc.content.Find
    .ClearFormatting
    .Text = "<<" & sPlaceholder & ">>"
    With .Replacement
        .ClearFormatting
        .Text = sData
    End With
    .Execute Replace:=wdReplaceAll
End With

这篇关于将数据从 Excel 导出到 Word:数据目标/占位符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆