vba vba从word表复制到excel

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12138958/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 17:28:55  来源:igfitidea点击:

vba copy from word table to excel

excelvbams-word

提问by David Peterson

I'm trying to generate an excel file with 5 column from specific tables' cells in a word file (copy from word table to excel). My word file has 280 tables. I have no problem on addressing the cells that i want to copy from my word file. but i don't know why the result is an blank excel file. Maybe I'm wrong in the paste method uh i don't know... . This is my code:

我正在尝试从 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

Thanks for your help

谢谢你的帮助

采纳答案by David Peterson

After some review, i;ve found that i shoud use pastespecial in my paste the corrected code is bellow

经过一些,我发现我应该在我的粘贴中使用 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

结束子