首页 > 解决方案 > 将数据从 Word 提取到 Excel

问题描述

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。

标签: excelvbams-word

解决方案


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

应该这样做,虽然我没有测试它,因为我没有准备好控制的文字文档。

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


推荐阅读