怎么vba实现word表格批量转为excel

我现在有很多word文件,且每个word都是一个表头,每个word都是这样的,表格也一样。能否把表头和表格复制到excel中,一个word对应一个excel文件。本来能一个个复制粘贴的,但是有好几千个word?所以最好能用vba实现
大体思路应该是 :
遍历word,打开word
找到word表格,复制,
新建excel 名字筒word一样,
把复制的内容粘贴到 当前excel第一个单元格,
保存关闭excel
关闭word
循环下一个word
前几步的代码已经有了

Dim wd As New Word.Application
Dim 当前路径, 文件名, 表名
当前路径 = ThisWorkbook.Path
文件名 = Dir(当前路径 & "\报名表\*.doc")
Do While 文件名 <> ""
表名 = Mid(文件名, 1, InStr(UCase(文件名), ".DOC") - 1)
呵呵,这个我已经搞定了

For i = 1 To tableCount '设置循环次数

ActiveDocument.Select
Selection.Tables(i).Select '选中表格
Selection.Copy '表格复制
appexcel.Workbooks.Add '添加excel

appexcel.ActiveSheet.Paste ' 在当前excel粘贴

ActiveWorkbook.SaveAs FileName:=SaveFileName & i & "(" & i Mod 2 & ")" & ".xls " '当前excel另存为。。。
ActiveWorkbook.Save
ActiveWorkbook.Close
appexcel.Close
Next

注:vba偶并不太熟(偶一般是用c#和delphi的),VBA只是稍有了解,以下代码大部分是偶google到的内容拼出来的。。。。。

如下,使用时先更改test下的docpath和xlspath路径设定,docpath即你的word的目录,此目录包括子目录下的所有doc将被读取,xlspath即输出目录,需要存在

在VBA窗口中,先在视图下显示立即窗口以观察进度,程序最后的输出类似这样
正在读取[1]:->D:\1\Resume.doc
正在生成:->d:\2\Resume
正在读取[2]:->D:\1\简历(简).doc
正在生成:->d:\2\简历(简)
正在读取[3]:->D:\1\计数器说明.doc
正在生成:->d:\2\计数器说明
共耗时0分41秒

Option Explicit
Dim docpath As String, xlspath As String
'ResultFlag=0 获取路径
'ResultFlag=1 获取文件名
'ResultFlag=2 获取扩展名
Public Function SplitPath(FullPath As String, ResultFlag As Integer) As String
Dim SplitPos As Integer, DotPos As Integer
SplitPos = InStrRev(FullPath, "\")
DotPos = InStrRev(FullPath, ".")
Select Case ResultFlag
Case 0
SplitPath = Left(FullPath, SplitPos - 1)
Case 1
If DotPos = 0 Then DotPos = Len(FullPath) + 1
SplitPath = Mid(FullPath, SplitPos + 1, DotPos - SplitPos - 1)
Case 2
If DotPos = 0 Then DotPos = Len(FullPath)
SplitPath = Mid(FullPath, DotPos + 1)
Case Else
Err.Raise vbObjectError + 1, "SplitPath Function", "Invalid Parameter!"
End Select
End Function

Public Function FileFolderExists(ByVal strFullPath As String) As Boolean
On Error GoTo EarlyExit
If Not Dir(strFullPath, vbDirectory) = vbNullString Then FileFolderExists = True
EarlyExit:
On Error GoTo 0
End Function

Sub Test() '使用双字典,旨在提高速度
Dim MyName, Dic, Did, I, T, F, TT, MyFileName, Doc, Ke
Dim count As Integer
count = 0
T = Time

docpath = "D:\1\"
xlspath = "d:\2\"

Set Dic = CreateObject("Scripting.Dictionary") '创建一个字典对象
Set Did = CreateObject("Scripting.Dictionary")
Dic.Add (docpath), ""
I = 0
Do While I < Dic.count
Ke = Dic.keys '开始遍历字典
MyName = Dir(Ke(I), vbDirectory) '查找目录
Do While MyName <> ""
If MyName <> "." And MyName <> ".." Then
If (GetAttr(Ke(I) & MyName) And vbDirectory) = vbDirectory Then '如果是次级目录
Dic.Add (Ke(I) & MyName & "\"), "" '就往字典中添加这个次级目录名作为一个条目
End If
End If
MyName = Dir '继续遍历寻找
Loop
I = I + 1
Loop
'Did.Add ("文件清单"), "" '以查找D盘下所有EXCEL文件为例
For Each Ke In Dic.keys
MyFileName = Dir(Ke & "*.doc")
Do While MyFileName <> ""
Doc = Ke & MyFileName
Did.Add (Doc), ""

count = count + 1
Debug.Print "正在读取[" & count & "]:->" & Doc
doc2xls (Doc)
MyFileName = Dir
Loop
Next

' For Each Sh In ThisWorkbook.Worksheets
' If Sh.Name = "XLS文件清单" Then
' Sheets("XLS文件清单").Cells.Delete
' F = True
' Exit For
' Else
' F = False
' End If
' Next

'If Not F Then
' Sheets.Add.Name = "XLS文件清单"
'End If
'Sheets("XLS文件清单").[A1].Resize(Did.Count, 1) = WorksheetFunction.Transpose(Did.keys)

TT = Time - T
Debug.Print "共耗时" & Minute(TT) & "分" & Second(TT) & "秒"
End Sub

Sub doc2xls(filename As String)
Dim xlApp As Object, xlSheet As Object, outfile As String, c As Object
Set xlApp = CreateObject("Excel.Application")
Set xlSheet = xlApp.Workbooks.Add.Sheets(1)

Dim Wapp As Object, Doc As Object, GetDocText As Object 'Word Application 对象、Document 对象
Set Wapp = CreateObject("Word.Application") '创建Word Application 对象
Set Doc = Wapp.Documents.Open(filename, ReadOnly:=True) '打开文档,返回一个文档对象

'xlSheet.Range("A1") = Doc.Content.Text
Doc.Application.Selection.WholeStory ''''全选
Doc.Application.Selection.Copy ''''''''''复制

xlSheet.Range("A1").Select
xlSheet.Paste

outfile = xlspath + Replace(SplitPath(filename, 1), ".doc", ".xls")
Debug.Print "正在生成:->" & outfile

xlSheet.Parent.SaveAs outfile

xlApp.Quit
Set xlSheet = Nothing
Set xlApp = Nothing

Wapp.Quit
Set Doc = Nothing
Set Wapp = Nothing

End Sub
温馨提示:答案为网友推荐,仅供参考
第1个回答  2010-04-25
除了表格和表头,在表头之前还有文字吗?
如果没有,建议你全部复制到Excel中在处理。
方便的话发一个Word过来。[email protected]
第2个回答  2010-04-25
把所有word文档复制到一个文件夹里
执行下面的过程
选择所有的文件
确定后自动生成文件
试试~~

Sub 表格_打开2()

Set actb = ActiveWorkbook
Dim i As Long
With Application.FileDialog(msoFileDialogOpen)
.AllowMultiSelect = True
.Title = "请选择您要打开的文件."
.ButtonName = "确定"
.InitialView = msoFileDialogViewThumbnail
.Show
For i = 1 To .SelectedItems.Count
Set aa = Workbooks.Open(.SelectedItems(i))
aa.SaveAs Filename:=.SelectedItems(i) & ".xls", FileFormat:=56
aa.Close
Next
End With
Exit Sub
End Sub
第3个回答  2010-04-26
好复杂