Parse a word document into an excel file

后端 未结 2 854
感情败类
感情败类 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.

    0 讨论(0)
  • 2021-01-24 13:12

    Here is code that actually works.

    '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 讨论(0)
提交回复
热议问题