|
原帖由 johncabin 于 2010-11-17 20:56 发表
感谢sylun的VBA,调试后能正常解决问题,非常感谢!只是速度有点慢?能看能否完善一下程序,就是把 某个文件夹下的所有调查表的数据(有1000多份)一起抽取出来吗,您的方法确实很好!希望能改进、
程序修改如下,可试试。请不要将程序存放于问卷文档中,处理速度可能与原调查问卷文档内存占用情况有关。- Sub test2()
- '批量提取指定文档中的窗体域数据
- Dim myDialog As FileDialog, myItem As Variant, myDoc As Document
- Dim myField As FormField, i As Byte, r As Integer
- Dim AppExcel As Object, st As Single
- On Error Resume Next
-
- Set myDialog = Application.FileDialog(msoFileDialogFilePicker)
- With myDialog
- .Title = "请选择需处理的所有文档"
- .Filters.Clear
- .Filters.Add "所有 WORD 文件", "*.doc", 1
- .AllowMultiSelect = True
- If .Show <> -1 Then Exit Sub
- End With
-
- st = Timer
- '如果Excel已运行,则数据添加在当前的Excel文档活动工作表,否则新建Excel文档
- Set AppExcel = GetObject(, "Excel.Application")
- If Err.Number <> 0 Then
- Set AppExcel = CreateObject("Excel.Application")
- AppExcel.Workbooks.Add
- AppExcel.Visible = True
- Err.Clear
- End If
- AppExcel.Application.ScreenUpdating = False
- With AppExcel.ActiveWorkbook.ActiveSheet
- r = .UsedRange.Row + .UsedRange.Rows.Count - 1
-
- For Each myItem In myDialog.SelectedItems '在所有选取项目中循环
- Set myDoc = Documents.Open(FileName:=myItem, Visible:=False)
- For Each myField In myDoc.FormFields
- i = i + 1
- .Rows(r + 1).Cells(i).Value = myField.Result
- Next
- i = 0
- r = r + 1
- myDoc.Close False
- Next
-
- End With
- AppExcel.Application.ScreenUpdating = True
- Set AppExcel = Nothing
- MsgBox "提取完毕!用时:" & Format(Timer - st, 0) & "秒。"
- End Sub
复制代码 |
|