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

查看:80
本文介绍了在"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)

推荐答案

类似的东西对

  • vbs读取测试文件(请更改路径),然后通过换行将其拆分为数组ArrVar
  • 该数组中的每一行被VbTab进一步拆分为第二个数组ArrVar2
  • vb创建一个字表,其大小等于ArrVar的长度和ArrVar2
  • 的宽度
  • 每一项都逐行写到表中
    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天全站免登陆