简体   繁体   English

使用合并域从文本文件导入数据(MS Word 2010)

[英]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: 它分三步进行:

  1. 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 
  2. 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 
  3. Insert the text 插入文字

     ' see Microsoft KB #212682 Selection.TypeText (GroupText) 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 MERGEFIELD 的 MS Word MAILMERGE 自动编号 - MS Word MAILMERGE autonumbering for MERGEFIELD 如何从MS-Word文档文件中提取文本数据 - How to extract text data from MS-Word doc file 导入制表符分隔的文本文件并插入到 MS Word 中的表格中 - Import tab-delimited text file and insert to a table in MS Word 将数据从DataGrid导出到MS Word文件 - Export data from DataGrid to MS Word file MS Word:带查询的Mailmerge超链接使用MERGEFIELD获取URL字符串 - MS Word: Mailmerge hyperlinks with query get URL string with a MERGEFIELD 使用VBA将Unicode字符从txt文件导入到MS-Word - Import Unicode characters from txt file to MS-Word with VBA 如何自动将MS Word 2010中突出显示的文本提取到新文件中? - How can I automatically extract highlighted text in MS Word 2010 to a new file? 从ms word文件捕获所选文本并将其保存到XBRL文件 - Capture the Selected Text from ms word file and save it to XBRL file MS Access 2010-如何将输入文本框绑定到基于用户输入打开MS Word文件的VBA命令? - MS Access 2010 - How can I tie an entry text box to a VBA command that opens a MS Word file based on user's entry? 如何在Visual Studio 2010的MS Word中保存文本框值? - How to save text box values in MS Word from Visual Studio 2010?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM