[英]Use a mergefield to import data from a text file (MS Word 2010)
I am using mail merge in MS Word 2010. Data comes from an Excel spreadsheet: 我在MS Word 2010中使用邮件合并。数据来自Excel电子表格:
Last Name First Name Group
Smith Oliver F1
Jones Amelia B2
Taylor Emma B2
Williams Jack C1
Let us say that we have the following mergefields: "LASTNAME" , "FIRSTNAME" and "GROUP" . 我们假设我们有以下合并域: “LASTNAME” , “FIRSTNAME”和“GROUP” 。
In my document, I would like to add a paragraph, specific to the group. 在我的文档中,我想添加一个专门针对该组的段落。 Word would look for this paragraph in a file called "GROUP" .txt:
Word会在名为“GROUP” .txt的文件中查找此段落:
B1.txt:
Lorem ipsum dolor sit amet, consectetur adipiscing elit.
B2.txt:
Sed auctor turpis sed nisl ultricies volutpat.
C1.txt:
Vestibulum vel interdum metus, quis accumsan tortor.
This is one idea among others. 这是其中一个想法。 These paragraphs could also come another source, such as an Excel worksheet or a CSV file.
这些段落也可以是另一个来源,例如Excel工作表或CSV文件。
Do you have an idea how I could do this? 你知道我怎么能这样做吗?
I am glad to tell you that I found a satisfactory solution to my problem by writing a small VBA script. 我很高兴告诉你,我通过编写一个小的VBA脚本找到了一个令人满意的解决方案。 It works in three steps:
它分三步进行:
Get the value of the desired mergefield (eg. "B2") 获取所需合并域的值(例如“B2”)
' source: 4th post, from dmaruca, on VBForum thread Nr 549587 GroupVal = ActiveDocument.MailMerge.DataSource.DataFields("Group").Value
Import the content of the related textfile (eg. "C:\\B2.TXT") 导入相关文本文件的内容(例如“C:\\ B2.TXT”)
GroupText = ImportTextFile("C:\\" & GroupVal & ".Txt") ' VBA Code Snippet #22 from Allan Chara (Excel MVP) Function ImportTextFile(strFile As String) As String Open strFile For Input As #1 ImportTextFile = Input$(LOF(1), 1) Close #1 End Function
Insert the text 插入文字
' see Microsoft KB #212682 Selection.TypeText (GroupText)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.