2

我正在尝试从 word 文件中的特定表的单元格生成一个包含 5 列的 excel 文件(从 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

谢谢你的帮助

4

1 回答 1

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

结束子

于 2012-08-27T10:10:08.257 回答