通过VBA宏删除Tabe列的单词出现错误 [英] Word deleting tabe column via vba macros gives an error
本文介绍了通过VBA宏删除Tabe列的单词出现错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想将数据从excel复制到word表,然后从表中删除一些列.我可以将数据复制到表中,但是当我删除列时会出现错误:
I want to copy data from excel to the word table and then delete some columns from table. I can copy data to table, but when I delete column it gives error:
由于表格的单元格宽度混合,因此无法访问此集合中的各个列.
Cannot access individual columns in this collection because the table has mixed cell widths.
我的代码:
Public Tbl1 As Table
Sub callExcel()
Dim objExcel As New Excel.Application
Dim exWb As Excel.Workbook
Set exWb = objExcel.Workbooks.Open("C:\Users\ismayilov\Desktop\test")
roomNumber = InputBox("Enter the room number to copy word:", "Room Number")
ActiveDocument.FormFields("Text1").Result = roomNumber
exWb.Sheets("Sheet1").Range("$A:$H").AutoFilter Field:=8, Criteria1:=roomNumber
Dim rVis As Range, rData As Range
exWb.Sheets("Sheet1").Range("$A1:$H600").Copy
Set Tbl1 = ActiveDocument.Tables.Add _
(Range:=Selection.Range, numRows:=1, NumColumns:=8)
Tbl1.Range.Paste
Tbl1.Columns(1).Delete
exWb.Close SaveChanges:=True
Set exWb = Nothing
Set Tbl1 = Nothing
End Sub
推荐答案
尝试更改此行:
Tbl1.Range.Paste
分为以下一个:
tbl1.Range.PasteAppendTable
编辑:似乎.PasteAppendTable
需要一些时间才能被调用.因此,请尝试添加此附加部分:
EDIT It seems that .PasteAppendTable
requires some time to be invoked. Therefore try to add this additional section:
'...your code
'let's wait a second before pasting (could be cut to 0.5 sec)
Dim tmpStart
tmpStart = Timer
Do
DoEvents
Loop While (tmpStart + 1) > Timer
tbl1.Range.PasteAppendTable
'...your code
这篇关于通过VBA宏删除Tabe列的单词出现错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文