VBA从Word表复制到Excel

编程入门 行业动态 更新时间:2024-10-17 17:15:57
本文介绍了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

本文发布于:2023-10-27 11:00:24,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1533174.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:VBA   Word   Excel

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!