|
本帖最后由 zhanglei1371 于 2014-4-27 22:09 编辑
你的意思是否是提取嵌入式域中的word文档的表格?
你的表述有问题,因为从代码来看,已经是成型代码了。提供的附件中的文件打开总提示需要转换器。
至于表格,最后用2010打开后发现是内嵌入的控件,可以使用ole对象方法:Set olindoc = lindoc.InlineShapes(1).OLEFormat.Object来取得图像控件里表格的内容,稍作修改如下:【具体自己再修改下】
最终完成代码:- Public astring As String
- '互函得到对话框中所有的文件路径+文件名
- Function filename1() '此代码功能为列出指定文件夹中所有选取的WORD文件全路径名
- Dim MyDialog As FileDialog
- On Error Resume Next '忽略错误
- '定义一个文件夹选取对话框
- Set MyDialog = Application.FileDialog(msoFileDialogFilePicker)
- With MyDialog
- .Filters.Clear '清除所有文件筛选器中的项目
- .Filters.Add "所有 WORD 文件", "*.doc;*.docx", 1 '增加筛选器的项目为所有WORD文件
- .AllowMultiSelect = True '允许多项选择
- If .Show = -1 Then '确定
- For Each vrtSelectedItem In .SelectedItems '在所有选取项目中循环
- filename1 = filename1 & Chr(13) & vrtSelectedItem '列出所有文件名
- Next vrtSelectedItem
- End If
-
- End With
- End Function
- Sub 主程序()
- Dim arr
- Dim arr1
-
- Dim astring As String
- Application.ScreenUpdating = False
- ' On Error Resume Next '忽略错误
-
- Application.StatusBar = "程序正在运行,请稍等!......(看到这个,说明一切正常)"
-
- astring = filename1 '文件名
- If astring = "" Then
- MsgBox "你没有选择任何文件"
- Exit Sub
- Else
- astring = Mid(astring, 2, Len(astring) - 1) '去掉第一个chr(13)
- arr = Split(astring, Chr(13))
- 具体 (arr)
- End If
- Application.StatusBar = "程序已正确运行完毕!"
- Application.ScreenUpdating = True
- End Sub
- Function 具体(arr)
- Dim lindoc As Word.Document
- Dim appword As Word.Application
- Dim i As Long
- Dim a
- Dim aend As Long
- Dim wordcell%, alin As String
- Dim arr2, j%, k%
- Set appword = GetObject(, "Word.Application")
- If Err.Number > 0 Then
- Set appword = CreateObject("word.Application")
- End If
- aend = Range("A65536").End(xlUp).Row + 1 '算得下一行行号
- ReDim arr2(UBound(arr), 40) '重定位数组,40是分工加的,可以改。改的话,要计算表格有多少个单元格
- For Each arr1 In arr
- arr2(i, 0) = aend - 2
- Range("P3:P3").Formula = "=A1 &2"
- Cells.HorizontalAlignment = xlCenter
- Cells.WrapText = True '自动换行
- Cells.EntireColumn.AutoFit '行高根据内容自动调整
- Cells.EntireRow.AutoFit
- k = 1
- Set lindoc = appword.Documents.Open(Filename:=arr1, Visible:=True)
- Application.StatusBar = "正在处理第" & i + 1 & "个文档,共" & UBound(arr) + 1 & "文档。..."
- With lindoc.Tables(1).Range
- For wordcell = 2 To .Cells.Count Step 2 '以2为循环
- Debug.Print .Cells(wordcell).Range.Fields.Count
- If .Cells(wordcell).Range.Fields.Count > 0 Then
- Dim f As Field, s As InlineShape
- For Each s In .Cells(wordcell).Range.InlineShapes
- If s.OLEFormat.Object.Value = True Then
- alin = s.OLEFormat.Object.Caption
- arr2(j, k) = alin
- End If
- Next
- Else
- alin = .Cells(wordcell).Range.Text
- arr2(j, k) = Mid(alin, 1, Len(alin) - 2) '去掉后面的制表符
- End If
- k = k + 1 '转入下一列
- Next
- End With
- lindoc.Close '关闭文档
- i = i + 1 '累加要处理的处理数
- j = j + 1 '转入下一行
- Next
- Range("a" & aend & ":M" & aend + UBound(arr)) = arr2 '数组附值
- Columns("H:H").Hidden = True
- Columns("K:K").Hidden = True
- Columns("L:L").Hidden = True
- Columns("N:N").Hidden = True
- Columns("P:P").Hidden = True
- Columns("r:r").Hidden = True
- appword.Quit '退出Word
- Set lindoc = Nothing
- Set appword = Nothing
- End Function
复制代码
|
|