首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将word文档解析为excel文件

将word文档解析为excel文件
EN

Stack Overflow用户
提问于 2011-06-17 18:25:55
回答 2查看 16.1K关注 0票数 1

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

我已经包括两个链接的屏幕截图。

第一个是我输入数据的示例的屏幕截图。

http://img717.imageshack.us/i/input.jpg/

第二个是我想要的输出的截图。

http://img3.imageshack.us/i/outputg.jpg/

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

代码语言:javascript
复制
    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

提前感谢你的帮助和建议

EN

回答 2

Stack Overflow用户

发布于 2011-06-17 18:35:34

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

代码语言:javascript
复制
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

单词没有“线”的概念。您可以阅读文本范围、段落和句子。尝试并找到哪种方法最适合在可管理的块中获取您的输入文本。

票数 4
EN

Stack Overflow用户

发布于 2020-01-24 22:54:01

下面是实际工作的代码。

代码语言:javascript
复制
'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
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/6390109

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档