从Word到Excel的数据提取

从Word到Excel的数据提取,excel,vba,ms-word,Excel,Vba,Ms 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 它会将这两个复选框值引入它们自己的列中。“真意义”复选框处于选中状态,而“假意义”复选框处于未选中状态。我希望只在一列中输入所选答案,而不是作

代码会引入复选框响应,但在我创建标题时,会以下面的格式忽略标题:

    1.Did you enjoy your day?
    YES ☒
    NO ☐
    Very fun
    2.Would you ever make a trip back? 
    YES ☐
    NO ☒
    Weather was too hot
它会将这两个复选框值引入它们自己的列中。“真意义”复选框处于选中状态,而“假意义”复选框处于未选中状态。我希望只在一列中输入所选答案,而不是作为真/假陈述,而是作为是/否

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

7-1-19-更新代码:

Q1 Yes  Q1 No   Comments    Q2 Yes  Q2 No   Comments
TRUE    FALSE   Very fun    FALSE   TRUE    Weather was too hot
使用新代码,宏将完成运行,但不会将数据从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
      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
这应该可以做到,尽管我没有测试它,因为我没有一个worddoc控件可以使用


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

太好了,我将报告结果。我在代码中添加了一个syntax错误:if.checkbox.value=TrueI,它修复了syntax错误,但是excel在运行代码@warcupinehm时崩溃,这些更改不应该影响我所认为的。可能是语法错误导致Excel或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
      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
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