从Word提取数据到Excel

时间:2019-06-28 18:53:38

标签: excel vba ms-word

Sub GetFormData()
'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
Dim FmFld As Word.FormField, CCtrl As Word.ContentControl
Dim strFolder As String, strFile As String
Dim WkSht As Worksheet, i As Long, j As Long
strFolder = GetFolder
If strFolder = "" Then Exit Sub
Set WkSht = ActiveSheet
i = WkSht.Cells(WkSht.Rows.Count, 1).End(xlUp).Row
'Disable any auto macros in the documents being processed
wdApp.WordBasic.DisableAutoMacros
strFile = Dir(strFolder & "\*.doc", vbNormal)
While strFile <> ""
  i = i + 1
  Set wdDoc = wdApp.Documents.Open(FileName:=strFolder & "\" & strFile, AddToRecentFiles:=False, Visible:=False)
  With wdDoc
    j = 0
    For Each FmFld In .FormFields
      j = j + 1
      With FmFld
        Select Case .Type
          Case Is = wdFieldFormCheckBox
            WkSht.Cells(i, j) = .CheckBox.Value
          Case Else
            If IsNumeric(FmFld.Result) Then
              If Len(FmFld.Result) > 15 Then
                WkSht.Cells(i, j) = "'" & FmFld.Result
              Else
                WkSht.Cells(i, j) = FmFld.Result
              End If
            Else
              WkSht.Cells(i, j) = FmFld.Result
            End If
        End Select
      End With
    Next
    For Each CCtrl In .ContentControls
      With CCtrl
        Select Case .Type
          Case Is = wdContentControlCheckBox
            j = j + 1
            WkSht.Cells(i, j) = .Checked
          Case wdContentControlDate, wdContentControlDropdownList, wdContentControlRichText, wdContentControlText
            j = j + 1
            If IsNumeric(.Range.Text) Then
              If Len(.Range.Text) > 15 Then
                WkSht.Cells(i, j).Value = "'" & .Range.Text
              Else
                WkSht.Cells(i, j).Value = .Range.Text
              End If
            Else
              WkSht.Cells(i, j) = .Range.Text
            End If 
          Case Else
        End Select
      End With
    Next
    .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

代码的作用是使用Excel中的宏将Word文档中的复选框控件和文本控件提取到excel。 在我的Word文件中,我有一个调查表,如下所示。

    1.Did you enjoy your day?
    YES ☒
    NO ☐
    Very fun
    2.Would you ever make a trip back? 
    YES ☐
    NO ☒
    Weather was too hot

该代码以以下格式显示复选框响应,(以我创建的标题为准):

Q1 Yes  Q1 No   Comments    Q2 Yes  Q2 No   Comments
TRUE    FALSE   Very fun    FALSE   TRUE    Weather was too hot

它将两个复选框值都带到自己的列中。 TRUE含义复选框已选中,FALSE含义复选框未选中。我希望仅将选定的答案带入“一”列,而不是作为TRUE / FALSE语句,而是作为YES / NO。

我尝试使用条件格式,但是当宏重新运行时,它不遵循条件格式规则,它只会声明TRUE / FALSE而不是Yes / No。

7-1-19-更新代码:

Sub GetFormData()
'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
Dim FmFld As Word.FormField, CCtrl As Word.ContentControl
Dim strFolder As String, strFile As String
Dim WkSht As Worksheet, i As Long, j As Long
strFolder = GetFolder
If strFolder = "" Then Exit Sub
Set WkSht = ActiveSheet
i = WkSht.Cells(WkSht.Rows.Count, 1).End(xlUp).Row
'Disable any auto macros in the documents being processed
wdApp.WordBasic.DisableAutoMacros
strFile = Dir(strFolder & "\*.doc", vbNormal)
While strFile <> ""
  i = i + 1
  Set wdDoc = wdApp.Documents.Open(Filename:=strFolder & "\" & strFile, AddToRecentFiles:=False, Visible:=False)
  With wdDoc
    j = 0
    For Each FmFld In .FormFields
      With FmFld
        Select Case .Type
          Case Is = wdFieldFormCheckBox
            If .CheckBox.Value = True Then  'Check for true
                 j = j + 1 'Moved after conditional
                 WkSht.Cells(i, j) = "Yes" 'Yes instead of True
            End If
          Case Else
            j = j + 1 'This is no longer at top of loop so you need to continue incrementing
            If IsNumeric(FmFld.Result) Then
              If Len(FmFld.Result) > 15 Then
                WkSht.Cells(i, j) = "'" & FmFld.Result
              Else
                WkSht.Cells(i, j) = FmFld.Result
              End If
            Else
              WkSht.Cells(i, j) = FmFld.Result
            End If
          End Select
      End With
Next
    For Each FmFld In .FormFields
      With FmFld
        Select Case .Type
          Case Is = wdFieldFormCheckBox
         If .CheckBox.Value = True Then 'Check for true
                 j = j + 1 'Moved after conditional
                 WkSht.Cells(i, j) = "Yes" 'Yes instead of True
            End If
          Case Else
            j = j + 1 'This is no longer at top of loop so you need to continue incrementing
            If IsNumeric(FmFld.Result) Then
              If Len(FmFld.Result) > 15 Then
                WkSht.Cells(i, j) = "'" & FmFld.Result
              Else
                WkSht.Cells(i, j) = FmFld.Result
              End If
            Else
              WkSht.Cells(i, j) = FmFld.Result
            End If
          End Select
      End With
Next
    .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

使用新代码,宏完成运行,但没有数据从word提取到excel。

1 个答案:

答案 0 :(得分:1)

For Each FmFld In .FormFields
      With FmFld
        Select Case .Type
          Case Is = wdFieldFormCheckBox
            if .checkbox.value = True then 'Check for true
                 j = j + 1 'Moved after conditional
                 WkSht.Cells(i, j) = "Yes" 'Yes instead of True
            end if     
          Case Else
            j = j + 1 'This is no longer at top of loop so you need to continue incrementing
            If IsNumeric(FmFld.Result) Then
              If Len(FmFld.Result) > 15 Then
                WkSht.Cells(i, j) = "'" & FmFld.Result
              Else
                WkSht.Cells(i, j) = FmFld.Result
              End If
            Else
              WkSht.Cells(i, j) = FmFld.Result
            End If
          End Select
      End With
Next

应该这样做,尽管我没有对它进行测试,因为我没有带有准备就绪的控件的word文档。

您还需要将其应用于带有复选框的其他循环。