Results 1 to 20 of 42

Thread: To find word in Microsoft Word Table and copy Offsets to Excel Cells

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #15
    VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,273
    Location
    Try the following:
    Sub GetTableData()
    'Note: this code requires a reference to the Word object model.
    'See under the VBE's Tools|References.
    Application.ScreenUpdating = False
    Dim wdApp As New Word.Application, wdDoc As Word.Document, wdTbl As Word.Table
    Dim strFolder As String, strFile As String, WkSht As Worksheet, c As Long, r As Long
    strFolder = GetFolder: If strFolder = "" Then GoTo ErrExit
    Set WkSht = ActiveSheet
    r = WkSht.Cells(WkSht.Rows.Count, 1).End(xlUp).Row
    strFile = Dir(strFolder & "\*.docx", vbNormal)
    While strFile <> ""
      Set wdDoc = wdApp.Documents.Open(Filename:=strFolder & "\" & strFile, AddToRecentFiles:=False, Visible:=False)
      r = r + 1: c = 1
      WkSht.Cells(r, c).Value = Split(strFile, ".docx")(0)
      With wdDoc
        For Each wdTbl In .Tables
          With wdTbl.Range
            With .Find
              .Text = "Fruits"
              .Wrap = wdFindStop
              .Execute
            End With
            If .Find.Found = True Then
              c = c + 1
              WkSht.Cells(r, c).Value = Split(wdTbl.Cell(.Cells(1).RowIndex + 1, .Cells(1).ColumnIndex + 1).Range.Text, vbCr)(0)
            End If
          End With
        Next
        .Close SaveChanges:=False
      End With
      strFile = Dir()
    Wend
    ErrExit:
    wdApp.Quit
    Set wdDoc = Nothing: Set wdApp = Nothing: Set WkSht = Nothing
    Application.ScreenUpdating = True
    End Sub
    
    Function GetFolder() As String
        Dim oFolder As Object
        GetFolder = ""
        Set oFolder = CreateObject("Shell.Application").BrowseForFolder(0, "Choose a folder", 0)
        If (Not oFolder Is Nothing) Then GetFolder = oFolder.Items.Item.Path
        Set oFolder = Nothing
    End Function
    
    PS:
    Split(WrdDoc.Tables(Cnt).Cell(TblCell.RowIndex, TblCell.ColumnIndex + 1), vbCr, 0)

    should have been
    Split(WrdDoc.Tables(Cnt).Cell(TblCell.RowIndex, TblCell.ColumnIndex + 1), vbCr)(0)
    Last edited by macropod; 09-22-2019 at 04:02 PM. Reason: Code Revision
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •