View Single Post
 
Old 04-15-2016, 05:08 PM
macropod's Avatar
macropod macropod is offline Windows 7 64bit Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 22,467
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

You'd need a quite different approach for that. And, given that your document has formfields in different columns on each row, the output needs to reflect that. Try:
Code:
Sub GetFormData()
'Note: this code requires a reference to the Word object model
Application.ScreenUpdating = False
Dim wdApp As New Word.Application
Dim wdDoc As Word.Document
Dim FmFld As Word.FormField
Dim strFolder As String, strFile As String
Dim WkSht As Worksheet, i As Long, r As Long, c As Long
strFolder = GetFolder
If strFolder = "" Then Exit Sub
Set WkSht = ActiveSheet
strFile = Dir(strFolder & "\*.doc", vbNormal)
While strFile <> ""
  i = WkSht.UsedRange.Cells.SpecialCells(xlCellTypeLastCell).Row
  Set wdDoc = wdApp.Documents.Open(Filename:=strFolder & "\" & strFile, AddToRecentFiles:=False, Visible:=False)
  With wdDoc
    With .Tables(1)
      For r = 2 To .Rows.Count
        i = i + 1
        For c = 2 To .Columns.Count
          With .Cell(r, c).Range
            If .FormFields.Count = 1 Then
              With .FormFields(1)
                If IsNumeric(.Result) Then
                  If Len(.Result) > 15 Then
                    WkSht.Cells(i, c - 1) = "'" & .Result
                  Else
                    WkSht.Cells(i, c - 1) = .Result
                  End If
                Else
                  WkSht.Cells(i, c - 1) = .Result
                End If
              End With
            End If
          End With
        Next
      Next
    End With
    .Close SaveChanges:=False
  End With
  strFile = Dir()
Wend
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
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote