Parse a word document into an excel file

后端 未结 2 857
感情败类
感情败类 2021-01-24 12:50

I have a word document that has data that I would like to parse into an excel file. The source files are hundreds of pages long. I have been working with VBA, but I just start

2条回答
  •  南笙
    南笙 (楼主)
    2021-01-24 12:56

    fopen and input commands generally only work on plain text files (things you can read in Notepad). If you want to programatically read from Microsoft word documents, you'll have to add the Microsoft Word 12.0 Object Library (or most recent version on your system) to your VBAProject references, and use the Word API to open and read the document.

    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 doesn't have a concept of "Lines". You can read text ranges, and paragraphs, and sentences. Experiment and find what works best for getting your input text in manageable blocks.

提交回复
热议问题