繁体   English   中英

将word文档解析为excel文件

[英]Parse a word document into an excel file

我有一个 word 文档,其中包含我想解析为 excel 文件的数据。 源文件长达数百页。 我一直在使用 VBA,但我刚刚开始学习这门语言,并且在尝试输入 .doc 文件时遇到了很多困难。 我已经能够使用OpenLine Input语句从 .txt 文件中检索,但在我尝试 .doc 文件时只会出现乱码。

我已经包含了两个屏幕截图链接。

第一个是我的输入数据样本的屏幕截图。
http://img717.imageshack.us/i/input.jpg/

第二个是我想要的输出的屏幕截图。
http://img3.imageshack.us/i/outputg.jpg/

我已经开发了一个我想要完成的算法。 我只是在编码时遇到困难。 下面是我开发的伪代码。

    Variables:
         string     line = blank
         series_title = blank
         folder_title = blank

         int  series_number = 0
              box_number = 0
              folder_number = 0
              year = 0
    do while the <end_of_document> has not been reached
        input line
        If the first word in the line is “series” 
            store <series_number>
            store the string after “:”into the <series_title>
        end if
        call parse_box(rest of line)
        output < series_number > <series_title> < box_number > < folder_number ><folder_title> <year>
    end do while

    function parse_box(current line)
        If the first word in the line is “box” 
            store <box_number>
        end if
        call parse_folder(rest of line)
    end function

    function parse_folder(current line)
        If first word is “Folder”
            store <folder_number>
        end if
        call parse_folder_title(rest of line)
    end function

    function parse_folder_title_and_year(current line)
        string temp_folder_title
        store everything as <temp_folder_title> until end of line
        if last word in <temp_folder_title> is a year
            store <year>
        end if
        if < temp_folder_title> is empty/blank
            //use <folder_title> from before
        else
            <folder_title> is < temp_folder_title> minus <year>
        end if
    end parse_folder_title_and_year

提前感谢您的所有帮助和建议

fopen 和 input 命令通常仅适用于纯文本文件(您可以在记事本中阅读的内容)。 如果要以编程方式读取 Microsoft Word 文档,则必须将 Microsoft Word 12.0 对象库(或系统上的最新版本)添加到 VBAProject 引用中,并使用 Word API 打开和阅读文档。

Dim odoc As Word.Document
Set odoc = oWrd.Documents.Open(Filename:=DocumentPath, Visible:=False)

Dim singleLine As Paragraph
Dim lineText As String

For Each singleLine In ActiveDocument.Paragraphs
    lineText = singleLine.Range.Text
    'Do what you've gotta do
Next singleLine

Word 没有“行”的概念。 您可以阅读文本范围、段落和句子。 试验并找出最适合在可管理块中获取输入文本的方法。

这是实际工作的代码。

'Create a New Object for Microsoft Word Application
Dim objWord As New Word.Application
'Create a New Word Document Object
Dim objDoc As New Word.Document
'Open a Word Document and Set it to the newly created object above
Set objDoc = objWord.Documents.Open(Filename:=DocFilename, Visible:=False)

Dim strSingleLine As Paragraph
Dim strLineText As String

For Each strSingleLine In objDoc.Paragraphs
    strLineText = strSingleLine.Range.Text
    'Do what you've gotta do
Next strSingleLine

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM