简体   繁体   English

在 Outlook 中使用 VBA,如何将 Excel 范围转换为表格?

[英]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.

相关问题 在Excel中使用VBA,如何在Outlook中粘贴表格,然后将表格转换为文本? - Using VBA in Excel, how to I paste a Table in Outlook then convert the table to text? 如何在Excel VBA 2007中为表设置变量范围? - How can I set a variable range to a table in excel VBA 2007? 如何使用Excel VBA打开在特定时间范围内发送到特定Outlook文件夹的Outlook excel附件? - How to open an Outlook excel attachment using Excel VBA, sent in a particular time range to a specific Outlook folder? 如何将范围转换为字符串 (VBA)? - How can I convert a range to a string (VBA)? VBA将Excel范围转换为图片并发送到Outlook,将文本写入正文 - Vba convert Excel range into picture and send to Outlook, write text into body 如何使用VBA将带有按钮的行添加到excel表中? - How can I add a row with buttons to an excel table using VBA? 如何使用Excel VBA将日期循环到此sql表中 - How can I loop the date into this sql table using Excel VBA VBA如何强制用户使用Excel的内置范围选择来选择范围? - VBA How can I force the user to select a range using Excel's built in range selection? Excel VBA:使用地址将单元格转换为范围 - Excel VBA: Convert Cells to Range using Address 如何使用 vba 代码动态自动填充 excel 表数据范围 - How do I auto-fill excel Table data range to right dynamically using vba code
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM