VBA从Word表复制到Excel [英] vba copy from word table to excel

查看:912
本文介绍了VBA从Word表复制到Excel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从word文件中特定表的单元格生成一个5列的excel文件(从word表复制到excel).我的word文件有280张桌子.我对要从Word文件中复制的单元格寻址没有任何问题.但我不知道为什么结果是空白的Excel文件.也许我在粘贴方法上错了,呃,我不知道....这是我的代码:

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:

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

感谢您的帮助

推荐答案

经过一番审查,我发现我应该在粘贴中使用pastespecial,正确的代码如下所示

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

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

结束子

这篇关于VBA从Word表复制到Excel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆