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  "

person davidmorley    schedule 08.04.2021    source источник


Ответы (2)


Установите точку останова, чтобы приостановить выполнение макроса, когда он сработает, расположите окна на экране так, как вы хотите их видеть, затем удалите точку останова и нажмите F5, чтобы продолжить выполнение.

person Nicholas Hunter    schedule 08.04.2021
comment
Ни F5, ни F8 не перезапускаются с точки останова. Мне сейчас интересно узнать о моем КБ, так как я редко использую клавиши F. - person davidmorley; 08.04.2021
comment
Эквивалент меню — «Выполнить/Продолжить». Кстати, я не могу придумать ни одной причины, по которой вы когда-либо захотите создать экземпляр Excel.Application в приложении Excel. - person Nicholas Hunter; 08.04.2021

Этот подход работает для меня:

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
person macropod    schedule 09.04.2021