在“MSWord"中创建表格.txt 文件中的文本文档 [英] Creating a table in "MSWord" document out of a text in a .txt file

查看:42
本文介绍了在“MSWord"中创建表格.txt 文件中的文本文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在以下文件E:\my_folder\my_future_table.txt"中,我有这样的文字:

In the following file "E:\my_folder\my_future_table.txt" I have this text:

#   animal  country
1   rabbit  Germany
2   wolf    France
3   koala   USA

(请注意,该文本每行中的所有单词都由制表符分隔)

(please note that all the words in each line of that text are separated by tabs)

我需要使用什么 VBS 脚本来创建带有基于该文本创建的表格的Word"文件 (.doc)?(在这个例子中,表格应该有 3 列和 4 行)

What VBS script do I need to use in order to create a "Word" file (.doc) with a table created on the basis of that text? (In this example, the table should have 3 columns and 4 rows)

推荐答案

脚本专家

  1. vbs 读取测试文件(请更改您的路径),然后通过换行符将其拆分为数组 ArrVar
  2. 这个数组中的每一行都被VbTab进一步分割成第二个数组,ArrVar2
  3. vbs 创建一个单词表,其大小等于 ArrVar 的长度和 ArrVar2
  4. 的宽度
  5. 每一项都按单元格、逐行写入表格
  1. The vbs reads the test file (pls change your path), and then splits it by line break into an array ArrVar
  2. Each line in this array is split further by VbTab into a second array, ArrVar2
  3. The vbs creates a word table equal in size to the length of ArrVar and width of ArrVar2
  4. Each item is written to the table cell by cell, row by row

Set objWord = CreateObject("Word.Application")
Set objDoc = objWord.Documents.Add()

Set objRange = objDoc.Range()
strFilePath = "c:\temp\my_future_table.txt"

Set objFSO = CreateObject("scripting.filesystemobject")
Set objTF = objFSO.opentextfile(strFilePath)
strAll = objTF.readall
arrVar = Split(strAll, vbNewLine)
numcols = UBound(Split(arrVar(0), vbTab)) + 1

objDoc.Tables.Add objRange, UBound(arrVar) - LBound(arrVar) + 1, numcols
Set objTable = objDoc.Tables(1)

For lngrow = LBound(arrVar) To UBound(arrVar)
    arrVar2 = Split(arrVar(lngrow), vbTab)
    For lngcol = LBound(arrVar2) To UBound(arrVar2)
     objTable.Cell(lngrow + 1, lngcol + 1).Range.Text = arrVar2(lngcol)
    Next
Next

objTF.Close
set objFSO = Nothing

objTable.AutoFormat (9)
objWord.Visible = True

这篇关于在“MSWord"中创建表格.txt 文件中的文本文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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