如何解决EXCEL VBA 从 word 文档中获取材料
我正在使用 Excel 宏从 Word 文档中提取文本并进行存储。
除了一个恼人的错误外,一切正常。在例程运行时,我想查看工作表而不是从中提取数据的文档。我找不到如何做到这一点。相关的代码是:
Sub Break_down()
'
' This macro will take a whole series of word documents in the same (or different)
' folder(s) and will find the number of words and then grab some text
'
' I have prevIoUsly added from Tools > References MS Word 12.0 Object Library
'
Dim sorsfilename(1 To 50) As String
'
Dim m As Integer,n As Integer,np As Long,nw As Integer
Dim npara As Integer,para_count As Integer
'
Dim objWKB As Excel.Workbook
Dim a As Object
'
Dim objWord As Object
Dim oRng As Word.Range
'
Dim objDoc
Dim rngParagraphs As Range
'
Dim wkb1 As Workbook
'
Set objExcel = New Excel.Application
Set objWord = CreateObject("Word.Application")
objWord.Visible = True
'objWord.Visible = False ' This did not work
'
Set wkb1 = ActiveWorkbook
sj = ActiveSheet.Name
Set a = wkb1.Worksheets("Sheet2")
'
sorsfilename(10) = "E:\Mining Expts\Sorse10.docx"
'plus other file names "
解决方法
设置一个断点来暂停宏执行,当它命中时,按照您希望看到的方式在屏幕上排列窗口,然后删除断点并按 F5 继续执行。
,这种方法对我有用:
Sub GetWordData()
'Note: this code requires a reference to the Word object model.
'See under the VBE's Tools|References.
Application.ScreenUpdating = False
Dim wdDoc As Word.Document,wdRng As Word.Range
Dim strFolder As String,strFile As String,ArrList()
Dim WkSht As Worksheet,i As Long,r As Long
ArrList() = Array("E:\Mining Expts\Sorse1.docx","E:\Mining Expts\Sorse2.docx","E:\Mining Expts\Sorse3.docx",_
"E:\Mining Expts\Sorse4.docx","E:\Mining Expts\Sorse5.docx","E:\Mining Expts\Sorse6.docx",_
"E:\Mining Expts\Sorse7.docx","E:\Mining Expts\Sorse8.docx","E:\Mining Expts\Sorse9.docx",_
"E:\Mining Expts\Sorse10.docx","E:\Mining Expts\Sorse11.docx","E:\Mining Expts\Sorse12.docx")
Set WkSht = ActiveSheet
r = WkSht.Cells(WkSht.Rows.Count,1).End(xlUp).Row
Dim wdApp As New Word.Application
With wdApp
'Hide the Word session
.Visible = False
'Disable any auto macros in the documents being processed
.WordBasic.DisableAutoMacros
'Disable any alerts in the documents being processed
.DisplayAlerts = wdAlertsNone
For i = 0 To UBound(ArrList)
strFile = ArrList(i): r = r + 1
Set wdDoc = .Documents.Open(Filename:=strFile,AddToRecentFiles:=False,Visible:=False)
With wdDoc
'Get the document word count
WkSht.Cells(r,1) = .ComputeStatistics(wdStatisticWords)
'Get the other document data,for example
WkSht.Cells(r,2) = .Range.Paragraphs(1).Range.Text
.Close SaveChanges:=False
End With
Next
.Quit
End With
Set wdRng = Nothing: Set wdDoc = Nothing: Set wdApp = Nothing: Set WkSht = Nothing
Application.ScreenUpdating = True
End Sub
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。