2011-06-17 37 views
1

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

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

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

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

我开发了一个我想完成的算法。我只是有困难编码。下面是我开发的伪代码。提前为您的帮助和建议

回答

3

的fopen和输入

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 

由于命令通常只对纯文本文件(东西,你可以在记事本中读出)工作。如果要以编程方式从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 

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

+0

这个答案缺乏“oWrd”的定义, – jumpjack