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.
В моем файле слов у меня есть вопросник, который выглядит следующим образом.
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 вместо Да / Нет.
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
С новым кодом макрос завершает работу, но из слова в Excel данные не извлекаются.