[英]Using VBA in Outlook, how can I convert an Excel range to a table?
I am trying to convert an Excel range to a table using a VBA script run from Outlook.我正在尝试使用从 Outlook 运行的 VBA 脚本将 Excel 范围转换为表格。
This worked when run from Excel but not from Outlook:这在从 Excel 运行但从 Outlook 运行时有效:
ListObjects.Add(xlSrcRange, Range("A1:D10"), , xlYes).Name = "myTable"
I tried this next code in Outlook but it failed:我在 Outlook 中尝试了下一个代码,但失败了:
Dim excApp As Excel.Application
Set excApp = GetObject(, "Excel.Application")
excApp.ListObjects.Add(xlSrcRange, Range("A1:D10"), , xlYes).Name = "myTable"
This code threw run-time error '1004' Method 'Range' of object '_Global' failed.此代码抛出运行时错误 '1004' 对象 '_Global' 的方法 'Range' 失败。
I want to know how to make the code work, but I also want to understand why my code failed so that I can learn from it.我想知道如何使代码工作,但我也想了解为什么我的代码失败,以便我可以从中学习。 I appreciate any suggestions.
我很感激任何建议。 Thanks!
谢谢!
This is because in Excel you already have a sheet open.这是因为在 Excel 中您已经打开了一个工作表。 The ActiveSheet is the default when you just stick
Range("somerange")
without specifying which sheet, workbook, or excel application object that range exists in. In outlook there is no such thing as activesheet
and so it has no idea what you mean by that range.当您只粘贴
Range("somerange")
而不指定该范围所在的工作表、工作簿或 Excel 应用程序对象时,ActiveSheet 是默认值。在 Outlook 中没有activesheet
这样的东西,因此它不知道您的意思那个范围。
You'll have to be more explicit:你必须更明确:
Dim excApp As Excel.Application
Set excApp = GetObject(, "Excel.Application")
Dim excWB as Object
Set excWB = excApp.Workbooks.Add
excApp.ListObjects.Add(xlSrcRange, excWB.sheets("sheet1").Range("A1:D10"), , xlYes).Name = "myTable"
Or something along those lines或者类似的东西
In short, an excel application has workbooks, workbooks have sheets, and sheets have ranges.简而言之,excel 应用程序有工作簿,工作簿有工作表,工作表有范围。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.