在原基础上勉强做了出来,看看是否合适。因首次对excel进行VBA操作,水平很有限,估计做法不怎么规范,也未作进一步处理。 Sub test2() '请将所有问卷文档保存于指定的文件夹(暂定为E:\ddd) Dim ndoc As String, odoc As Document Dim mytext As String, info() As String Dim i As Integer, n As Integer, c As Integer Dim AppExcel As Excel.Application, myWbook As Excel.Workbook
ChDrive "E" '设置当前驱动器 ChDir "E:\ddd" '确定目录路径 ndoc = Dir("*.doc") Application.ScreenUpdating = False Do While ndoc <> "" '在指定目录内各文档循环操作 n = n + 1 Set odoc = Documents.Open(ndoc) With odoc.Content.Find '查找红色数字 .ClearFormatting .Font.Color = wdColorRed .Format = True .MatchWildcards = True '如果找到,则记录其题号及数字选项 Do While .Execute(findtext:="[1-3]", Forward:=True) = True With .Parent mytext = mytext & Replace(odoc.Name, ".doc", "") _ & ":no" & .Cells(1).RowIndex - 1 & vbTab & .Text & vbCr .Move Unit:=wdRow, Count:=1 End With Loop End With '字符串中每个文件的记录数据以“|”分隔 mytext = mytext & "|" odoc.Close False ndoc = Dir() Loop '将全部答卷记录数据按每份答卷作为一个元素生成数组,在excel表格中每元素数据占一行 info = Split(Left(mytext, Len(mytext) - 1), "|") '创建excel文档并插入全部记录数据 Set AppExcel = CreateObject("Excel.Application") AppExcel.Visible = True Set myWbook = AppExcel.Workbooks.Add With myWbook.ActiveSheet .Cells(1, 1).Value = "文件名" For i = 1 To 32 .Cells(1, i + 1).Value = "No." & i Next i For c = 0 To UBound(info) .Cells(c + 2, 1).Value = Split(info(c), ":no")(0) For i = 1 To 32 .Cells(c + 2, Split(Split(info(c), "no")(i), vbTab)(0) + 1).Value = Right(Split(info(c), vbCr)(i - 1), 1) Next i Next c End With Set AppExcel = Nothing Application.ScreenUpdating = True MsgBox "共统计了" & n & "个文档。", vbInformation End Sub
[此贴子已经被作者于2007-5-21 13:56:54编辑过] |