vba提取word内容到excel
时间: 2023-08-06 07:09:03 浏览: 282
excel通过vba 批量读取word内表格的内容并写入excel
5星 · 资源好评率100%
可以使用VBA代码从Word文档中提取内容并将其导入到Excel表格中。以下是示例代码:
```
Sub ExtractWordContentToExcel()
Dim wrdApp As Word.Application
Dim wrdDoc As Word.Document
Dim i As Integer, j As Integer, k As Integer
'Create a new instance of Word
Set wrdApp = CreateObject("Word.Application")
'Open the Word document
Set wrdDoc = wrdApp.Documents.Open("C:\Path\To\Your\Word\Document.docx")
'Set the range to the entire document
wrdDoc.Range(0, 0).Select
'Loop through each paragraph in the document
i = 1
For Each para In wrdDoc.Paragraphs
'Loop through each sentence in the paragraph
j = 1
For Each sent In para.Range.Sentences
'Loop through each word in the sentence
k = 1
For Each wrd In sent.Words
'Copy the word to Excel
Worksheets("Sheet1").Cells(i, j) = wrd
k = k + 1
j = j + 1
Next wrd
'Add a blank cell between sentences
Worksheets("Sheet1").Cells(i, j) = ""
j = j + 1
Next sent
'Add a blank cell between paragraphs
Worksheets("Sheet1").Cells(i, j) = ""
i = i + 1
Next para
'Close the Word document and quit Word
wrdDoc.Close
wrdApp.Quit
'Clean up the object variables
Set wrdDoc = Nothing
Set wrdApp = Nothing
End Sub
```
请将代码中的文件路径替换为您的Word文档的实际路径,并将Excel工作表的名称替换为您要将内容导入的工作表的名称。
阅读全文