将表从word复制粘贴到excel(copy

系统教程 行业动态 更新时间:2024-06-14 16:53:07
将表从word复制粘贴到excel(copy-paste tables from word to excel)

我有一个定期更新的word文档。 我可以进入Word文档,选择整个表的内容并复制,然后进入Excel电子表格并粘贴它。 搞砸了; 但是,我修复如下:

sht.Cells.UnMerge sht.Cells.ColumnWidth = 14 sht.Cells.RowHeight = 14 sht.Cells.Font.Size = 10

无论表是否具有合并字段,此手动复制粘贴都可以使用。 然后我可以开始手动操作它:解析,检查,计算等。

我可以一次只做一张桌子,但这很乏味,当然也容易出错。

我想自动化这个。 我找到了一些代码:

Sub read_word_document() Dim sht As Worksheet Dim WordDoc As Word.Document Dim WordApp As Word.Application Set WordApp = CreateObject("Word.Application") WordApp.Visible = False On Error GoTo ErrHandler Set WordDoc = WordApp.Documents.Open("Z:\mydir\myfile1.DOC", ReadOnly:=True) j = 0 For i = 1 To WordDoc.Tables.Count DoEvents Dim s As String s = WordDoc.Tables(i).Cell(1, 1).Range.Text Debug.Print i, s WordDoc.Tables(i). Set sht = Sheets("temp") 'sht.Cells.Clear sht.Cells(1, 1).Select sht.PasteSpecial (xlPasteAll) End If Next i WordDoc.Close WordApp.Quit GoTo done ErrClose: On Error Resume Next ErrHandler: Debug.Print Err.Description On Error GoTo 0 done: End Sub

当然,这只会一次又一次地覆盖同一张纸 - 这没关系。 这只是一个考验。 问题是这适用于那些没有合并单元格的表。 但是,如果表已合并单元格,则会失败。 我无法控制我得到的文件。 它包含近百张表。 有没有办法复制粘贴我手动执行操作时的确切方法?

I have a word document which is updated periodically. I can go into that Word document, select the contents of an entire table and copy, then go into an Excel spreadsheet and paste it. It's screwed up; however, I fix it as follows:

sht.Cells.UnMerge sht.Cells.ColumnWidth = 14 sht.Cells.RowHeight = 14 sht.Cells.Font.Size = 10

This manual copy-paste works regardless of whether the table is has merged fields. Then I can start to manipulate it manually: parsing, checking, computations, etc.

I can do this one table at a time, but it's tedious and of course error prone.

I want to automate this. I found some code:

Sub read_word_document() Dim sht As Worksheet Dim WordDoc As Word.Document Dim WordApp As Word.Application Set WordApp = CreateObject("Word.Application") WordApp.Visible = False On Error GoTo ErrHandler Set WordDoc = WordApp.Documents.Open("Z:\mydir\myfile1.DOC", ReadOnly:=True) j = 0 For i = 1 To WordDoc.Tables.Count DoEvents Dim s As String s = WordDoc.Tables(i).Cell(1, 1).Range.Text Debug.Print i, s WordDoc.Tables(i). Set sht = Sheets("temp") 'sht.Cells.Clear sht.Cells(1, 1).Select sht.PasteSpecial (xlPasteAll) End If Next i WordDoc.Close WordApp.Quit GoTo done ErrClose: On Error Resume Next ErrHandler: Debug.Print Err.Description On Error GoTo 0 done: End Sub

Of course this would just overwrite the same sheet again and again - and that's okay. This is just a test. The problem is this will work for those tables that do not have merged cells. However, it fails if the table has merged cells. I have no control over the file I get. It contains almost a hundred tables. Is there a way to do the copy paste the EXACT WAY that I do when I perform the operation manually?

最满意答案

像这样的东西:

Sub read_word_document() Const DOC_PATH As String = "Z:\mydir\myfile1.DOC" Dim sht As Worksheet Dim WordDoc As Word.Document Dim WordApp As Word.Application Dim i As Long, r As Long, c As Long Dim rng As Range, t As Word.Table Set WordApp = CreateObject("Word.Application") WordApp.Visible = False Set WordDoc = WordApp.Documents.Open(DOC_PATH, ReadOnly:=True) Set sht = Sheets("Temp") Set rng = sht.Range("A1") sht.Activate For Each t In WordDoc.Tables t.Range.Copy rng.Select rng.Parent.PasteSpecial Format:="Text", Link:=False, _ DisplayAsIcon:=False With rng.Resize(t.Rows.Count, t.Columns.Count) .Cells.UnMerge .Cells.ColumnWidth = 14 .Cells.RowHeight = 14 .Cells.Font.Size = 10 End With Set rng = rng.Offset(t.Rows.Count + 2, 0) Next t WordDoc.Close WordApp.Quit End Sub

Something like this:

Sub read_word_document() Const DOC_PATH As String = "Z:\mydir\myfile1.DOC" Dim sht As Worksheet Dim WordDoc As Word.Document Dim WordApp As Word.Application Dim i As Long, r As Long, c As Long Dim rng As Range, t As Word.Table Set WordApp = CreateObject("Word.Application") WordApp.Visible = False Set WordDoc = WordApp.Documents.Open(DOC_PATH, ReadOnly:=True) Set sht = Sheets("Temp") Set rng = sht.Range("A1") sht.Activate For Each t In WordDoc.Tables t.Range.Copy rng.Select rng.Parent.PasteSpecial Format:="Text", Link:=False, _ DisplayAsIcon:=False With rng.Resize(t.Rows.Count, t.Columns.Count) .Cells.UnMerge .Cells.ColumnWidth = 14 .Cells.RowHeight = 14 .Cells.Font.Size = 10 End With Set rng = rng.Offset(t.Rows.Count + 2, 0) Next t WordDoc.Close WordApp.Quit End Sub

更多推荐

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

发布评论

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

>www.elefans.com

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