簡體   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