我正在尝试生成一个excel文件,其中包含来自word文件中特定表格单元格的5列(从word表格复制到excel)。我的word文件有280个表。我在解决我要从word文件中复制的单元格时没有问题。但我不知道为什么结果是一个空白的Excel文件。也许我在粘贴方法上错了呃我不知道....这是我的代码:
Sub copyfromwordtoexcel()
Dim exApp As Excel.Application
Dim exDoc As Excel.Workbook
Set exApp = CreateObject("Excel.Application")
Set exDoc = exApp.Workbooks.Add
For xx = 1 To ActiveDocument.Tables.Count
On Error Resume Next
ActiveDocument.Tables(xx).Cell(2, 2).Range.Copy
exApp.Visible = True
Cells(xx, 1).Select
ActiveSheet.Paste
Application.Visible = True
exApp.Visible = False
ActiveDocument.Tables(xx).Cell(3, 2).Range.Copy
exApp.Visible = True
Cells(xx, 2).Select
ActiveSheet.Paste
i = ActiveDocument.Tables(xx).Rows.Count
ActiveDocument.Tables(xx).Cell(i - 2, 2).Range.Copy
exApp.Visible = True
Cells(xx, 3).Select
ActiveSheet.Paste
Application.Visible = True
ActiveDocument.Tables(xx).Cell(i - 1, 2).Range.Copy
exApp.Visible = True
Cells(xx, 4).Select
ActiveSheet.Paste
Application.Visible = True
ActiveDocument.Tables(xx).Cell(i, 2).Range.Copy
exApp.Visible = True
Cells(xx, 5).Select
ActiveSheet.Paste
Application.Visible = True
exApp.Visible = True
Next
End Sub
感谢您的帮助
答案 0 :(得分:0)
经过一番审查后,我发现我应该在我的粘贴中使用pastespecial,更正的代码如下:
Sub copyfromwordtoexcel()
Dim exApp As Excel.Application
Dim exDoc As Excel.Workbook
Set exApp = CreateObject("Excel.Application")
Set exDoc = exApp.Workbooks.Add
For xx = 1 To ActiveDocument.Tables.Count
On Error Resume Next
If ActiveDocument.Tables(xx).Columns.Count = 2 Then
ActiveDocument.Tables(xx).Cell(2, 2).Range.Copy
exApp.Visible = True
Cells(xx, 1).Select
ActiveSheet.PasteSpecial (xlPasteAll)
Application.Visible = True
exApp.Visible = False
ActiveDocument.Tables(xx).Cell(3, 2).Range.Copy
exApp.Visible = True
Cells(xx, 2).Select
ActiveSheet.PasteSpecial (xlPasteAll)
i = ActiveDocument.Tables(xx).Rows.Count
ActiveDocument.Tables(xx).Cell(i - 2, 2).Range.Copy
exApp.Visible = True
Cells(xx, 3).Select
ActiveSheet.PasteSpecial (xlPasteAll)
Application.Visible = True
ActiveDocument.Tables(xx).Cell(i - 1, 2).Range.Copy
exApp.Visible = True
Cells(xx, 4).Select
ActiveSheet.PasteSpecial (xlPasteAll)
Application.Visible = True
ActiveDocument.Tables(xx).Cell(i, 2).Range.Copy
exApp.Visible = True
Cells(xx, 5).Select
ActiveSheet.PasteSpecial (xlPasteAll)
Application.Visible = True
exApp.Visible = True
End If
Next
End Sub