Excel 是否使用vba删除mailItem表的第一行?

Excel 是否使用vba删除mailItem表的第一行?,excel,vba,datatables,rows,Excel,Vba,Datatables,Rows,我是新来做宏的。我想要一个宏,它将电子邮件数据附加到excel文件并删除邮件项目的标题 Set oTable = oRng.Tables(1) 'And copy it to the clipboard oTable.Range.Copy 完整代码 Option Explicit Sub ProcessMessage() 'Graham Mayor 8 June 2015 'This macro is used to process a selection of messages from

我是新来做宏的。我想要一个宏,它将电子邮件数据附加到excel文件并删除邮件项目的标题

Set oTable = oRng.Tables(1)
'And copy it to the clipboard
oTable.Range.Copy
完整代码

Option Explicit

Sub ProcessMessage()
'Graham Mayor 8 June 2015
'This macro is used to process a selection of messages from an Outlook folder
Dim olItem As MailItem
For Each olItem In Application.ActiveExplorer.Selection
'Ensure the selcetd item is an e-mail message
If olItem.Class = OlObjectClass.olMail Then
'Then run the main process
TableToExcel olItem
End If
Next olItem
Set olItem = Nothing
lbl_Exit:
'Tell the user the job is done.
MsgBox "Selected message(s) processed."
Exit Sub
End Sub

Sub TableToExcel(olItem As MailItem)
'Graham Mayor 8 June 2015
'This macro is the main process
Dim xlApp As Object
Dim xlWB As Object
Dim xlSheet As Object
Dim olInsp As Outlook.Inspector
Dim wdDoc As Object
Dim oRng As Object
Dim oTable As Object
Dim strWorkBookName As String
Dim strPath As String
Dim xlRng As Object
Dim LastRow As Long
'Name the folder in which the workbook will reside
strPath = "C:\Path\Tables"
'Name the workbook
strWorkBookName = "Table.xlsx"
'Ensure the folder exists, and if it doesn't run the CreateFolders
'Function to create it.
CreateFolders strPath
With olItem
'Access the message body
Set olInsp = .GetInspector
Set wdDoc = olInsp.WordEditor
Set oRng = wdDoc.Range
'If there are no tables then end the process
If oRng.Tables.Count = 0 Then GoTo lbl_Exit
'Indicate the first table in the message
Set oTable = oRng.Tables(1)
'And copy it to the clipboard
oTable.Range.Copy
'Close the message
.Close 0
End With

'See if Excel is running and if it is use the running version
On Error Resume Next
Set xlApp = GetObject(, "Excel.Application")
If Err <> 0 Then
'Excel is not running, so start it up
Set xlApp = CreateObject("Excel.Application")
End If
'xlApp.Visible = True 'Make true while testing
On Error GoTo 0
'Add a new and empty workbook

Set xlWB = xlApp.workbooks.Open(strPath + strWorkBookName) 'You might want to use a template here?
'Indicate to the process to use the first sheet
Set xlSheet = xlWB.Sheets(1)
LastRow = xlSheet.Range("A1").CurrentRegion.Rows.Count
xlSheet.Range("A" & LastRow + 1).Select

'Paste the clipboard content
xlSheet.Paste
'Optional section to format the table
Set xlRng = xlSheet.UsedRange
With xlRng
.WrapText = False
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = -5002
.MergeCells = False
.HorizontalAlignment = 1
.VerticalAlignment = -4160
.Columns.Autofit
End With
'end of optional section
'Ensure the filename doesn't exist and if it does append a
'bracketed number to the name

'Save in the indicated folder
xlWB.SaveAs strPath & strWorkBookName
'Close the workbook
xlWB.Close SaveChanges:=False
lbl_Exit:
'and clean up
Set olInsp = Nothing
Set wdDoc = Nothing
Set oRng = Nothing
Set oTable = Nothing
Set xlWB = Nothing
Set xlSheet = Nothing
Set xlApp = Nothing
Exit Sub
End Sub

Private Function CreateFolders(strPath As String)
'Graham Mayor
'A function to create a named path if it doesn't exist
Dim strTempPath As String
Dim lngPath As Long
Dim vPath As Variant
vPath = Split(strPath, "\")
strPath = vPath(0) & "\"
For lngPath = 1 To UBound(vPath)
strPath = strPath & vPath(lngPath) & "\"
If Not FolderExists(strPath) Then MkDir strPath
Next lngPath
lbl_Exit:
Exit Function
End Function

Private Function FileNameUnique(strPath As String, _
strFileName As String, _
strExtension As String) As String
'Graham Mayor
'A function to create unique filenames (works in all Office apps that run VBA)
'strPath is the folder in which the file will be saved e.g. C:\Path\
'strFileName is the original name of the file to be saved
'strExtension is the filename extension e.g. "xlsx", "docx" etc
Dim lngF As Long
Dim lngName As Long
lngF = 1
lngName = Len(strFileName) - (Len(strExtension) + 1)
strFileName = Left(strFileName, lngName)
Do While FileExists(strPath & strFileName & Chr(46) & strExtension) = True
strFileName = Left(strFileName, lngName) & "(" & lngF & ")"
lngF = lngF + 1
Loop
FileNameUnique = strFileName & Chr(46) & strExtension
lbl_Exit:
Exit Function
End Function

Private Function FileExists(filespec) As Boolean
'Graham Mayor
'A function to establish if a file exists
'(works in all Office apps that run VBA)
Dim fso As Object
Set fso = CreateObject("Scripting.FileSystemObject")
If fso.FileExists(filespec) Then
FileExists = True
Else
FileExists = False
End If
lbl_Exit:
Exit Function
End Function

Private Function FolderExists(strFolderName As String) As Boolean
'Graham Mayor
'A function to establish if a folder exists
'(works in all Office apps that run VBA)
Dim fso As Object
Set fso = CreateObject("Scripting.FileSystemObject")
If (fso.FolderExists(strFolderName)) Then
FolderExists = True
Else
FolderExists = False
End If
lbl_Exit:
Exit Function
End Function




选项显式
子进程消息()
格雷厄姆市长2015年6月8日
'此宏用于处理Outlook文件夹中的选定邮件
以邮件形式发送邮件
对于Application.ActiveExplorer.Selection中的每个olItem
'确保selcetd项目是电子邮件
如果olItem.Class=OlObjectClass.olMail,则
'然后运行主进程
TableToExcel-olItem
如果结束
下一代
设置m=无
lbl_出口:
'告诉用户作业已完成。
MsgBox“已处理选定邮件。”
出口接头
端接头
子表TOEXCEL(作为邮件项的olItem)
格雷厄姆市长2015年6月8日
'此宏是主进程
将xlApp作为对象
作为对象的Dim xlWB
将图纸作为对象
将SP设置为Outlook.Inspector
Dim wdDoc作为对象
作为物体的暗角
作为对象可旋转
Dim strWorkBookName作为字符串
将strPath设置为字符串
作为对象的Dim xlRng
最后一排一样长
'命名工作簿将驻留的文件夹
strPath=“C:\Path\Tables”
'为工作簿命名
strWorkBookName=“Table.xlsx”
'确保文件夹存在,如果没有运行CreateFolders
'函数来创建它。
CreateFolders strPath
用沸石
'访问消息正文
设置olInsp=.GetInspector
设置wdDoc=olInsp.WordEditor
设置oRng=wdDoc.范围
'如果没有表,则结束该过程
如果oRng.Tables.Count=0,则转到lbl\U退出
'指示消息中的第一个表
Set oTable=oRng.表格(1)
'并将其复制到剪贴板
oTable.Range.Copy
'关闭消息
.关闭0
以
'查看Excel是否正在运行,以及是否正在运行,请使用正在运行的版本
出错时继续下一步
Set xlApp=GetObject(,“Excel.Application”)
如果错误为0,则
'Excel未运行,请启动它
设置xlApp=CreateObject(“Excel.Application”)
如果结束
“xlApp.Visible=True”在测试时变为True
错误转到0
'添加新的空工作簿
设置xlWB=xlApp.workbooks.Open(strPath+strWorkBookName)'您可能希望在此处使用模板?
'指示流程使用第一张图纸
设置xlSheet=xlWB.Sheets(1)
LastRow=xlSheet.Range(“A1”).CurrentRegion.Rows.Count
xlSheet.Range(“A”&LastRow+1)。选择
'粘贴剪贴板内容
活页贴
'设置表格格式的可选部分
设置xlRng=xlSheet.UsedRange
带xlRng
.WrapText=False
.方向=0
.AddIndent=False
.1级别=0
.ShrinkToFit=False
.ReadingOrder=-5002
.MergeCells=False
.水平对齐=1
.垂直对齐=-4160
.Columns.Autofit
以
'可选部分的结尾
'确保文件名不存在,如果确实存在,请附加
“名称的括号内数字
'保存在指定的文件夹中
xlWB.SaveAs strPath和strWorkBookName
'关闭工作簿
xlWB.Close SaveChanges:=False
lbl_出口:
"清理,
设置olInsp=Nothing
设置wdDoc=Nothing
设为零
设置可旋转=无
设置xlWB=Nothing
Set xlSheet=无
设置xlApp=Nothing
出口接头
端接头
私有函数CreateFolders(strPath作为字符串)
“格雷厄姆市长
'如果命名路径不存在,则创建该路径的函数
将strTempPath设置为字符串
暗淡的lngPath尽可能长
Dim vPath作为变体
vPath=Split(strPath,“\”)
strPath=vPath(0)和“\”
对于lngPath=1到UBound(vPath)
strPath=strPath&vPath(lngPath)&“\”
如果不存在FolderExists(strPath),则MkDir strPath
下一个lngPath
lbl_出口:
退出功能
端函数
私有函数FileNameUnique(strPath作为字符串_
strFileName作为字符串_
strExtension作为字符串)作为字符串
“格雷厄姆市长
'创建唯一文件名的函数(适用于所有运行VBA的Office应用程序)
'strPath是保存文件的文件夹,例如C:\Path\
'strFileName是要保存的文件的原始名称
'strExtension是文件扩展名,例如“xlsx”、“docx”等
变暗lngF为长
暗淡的鼻孔和长鼻孔一样长
lngF=1
lngName=Len(strFileName)-(Len(strExtension)+1)
strFileName=Left(strFileName,lngName)
当文件存在时执行(strPath&strFileName&Chr(46)&strExtension)=True
strFileName=Left(strFileName,lngName)和“(&lngF&”)
lngF=lngF+1
环
filenameinque=strFileName&Chr(46)&strExtension
lbl_出口:
退出功能
端函数
私有函数FileExists(filespec)为布尔值
“格雷厄姆市长
'用于确定文件是否存在的函数
'(适用于所有运行VBA的Office应用程序)
作为对象的Dim fso
设置fso=CreateObject(“Scripting.FileSystemObject”)
如果存在fso.files(filespec),则
FileExists=True
其他的
FileExists=False
如果结束
lbl_出口:
退出功能
端函数
私有函数FolderExists(strFolderName作为字符串)作为布尔值
“格雷厄姆市长
'用于确定文件夹是否存在的函数
'(适用于所有运行VBA的Office应用程序)
作为对象的Dim fso
设置fso=CreateObject(“Scripting.FileSystemObject”)
如果(fso.FolderExists(strFolderName)),那么
FolderExists=True
其他的
FolderExists=False
如果结束
lbl_出口:
退出功能
端函数

提前感谢您的帮助。

您的问题是什么?我正在从mail中提取表格,不想复制邮件表格的第一行?从mailItem表复制数据时,如何能够忽略第一行?在
xlSheet.Paste
之后,只需使用
xlSheet.Rows(1)删除第一行即可。