首先,我是一个从头开始工作的VB新手,但过去曾编辑过一些代码.我能找到的最接近的问题是
this one,但它并不像我希望的那么具体.
所以我正在使用Outlook / Excel 2007,我收到的每日电子邮件中包含一些固定格式的数据.我希望做的是设置一个宏/脚本来搜索我的Outlook收件箱,然后根据正确的邮件主题,查看邮件正文并将某些部分提取到Excel工作表中.
根据我的知识,我认为VB可能是最好的方法,但我不太清楚从哪里开始.任何关于代码的一般结构或其他类似示例的帮助将非常感激.只是想着手开始,希望能在我自己的未来练习中找到它.谢谢!
非常感谢你的帮助!我大部分时间都在使用它,当我收到新消息时,我无法让它自动更新.我有一个规则设置,将相关的电子邮件移动到他们自己的文件夹中,我能够设置一个我可以运行的公共宏,将所有数据拉出(对于每个电子邮件)并将它们转储到.csv文件中.
我尝试将该宏调整到上面发布的示例中,该示例应该在我收到新消息时自动运行,但我还没有成功.电子邮件的解析不应该改变(并且肯定适用于手动运行的宏),所以没关系,它只是让自动更新宏在新消息上运行.我错过了什么吗?这是我所拥有的,除了新文件夹(并且是一个类模块)之外,它基本上与上面的示例相同:
Public WithEvents myOlItems As Outlook.Items Public Sub Application_Startup() ' Reference the items in the InBox. Because myOlItems is declared ' "WithEvents" the ItemAdd event will fire below. Set myOlItems = Outlook.Session.GetDefaultFolder(olFolderInBox).Folders("FolderX").Items End Sub Private Sub myOlItems_ItemAdd(ByVal Item As Object) Dim objOutlook As New Outlook.Application Dim objNameSpace As Outlook.NameSpace Dim objFolder As Outlook.MAPIFolder Dim objMail As MailItem Dim count As Integer Dim myTitlePos As Integer Dim myTitleLen As Integer Dim myVarPos As Integer Dim myVarLen As Integer Dim strPrice As String Dim strYear As String Dim myVarCRLF As Integer Dim myDate As Date Dim newLineTest As String ' Check to make sure it is an Outlook mail message,otherwise ' subsequent code will probably fail depending on what type ' of item it is. If TypeName(Item) = "MailItem" Then ' Data processing and parsing is done here End Sub
VB可能是最容易解决问题的语言,因为您不熟悉所有这些并且VBA(Visual Basic for Applications)是针对特定问题的最简单且最具互操作性的语言.
您将首先创建一个新的Outlook宏,只要新邮件到达您的收件箱就会触发该宏.
首先在Outlook中创建一个新的类模块(ALT-F11)并复制以下代码:
Public WithEvents myOlItems As Outlook.Items Public Sub Application_Startup() ' Reference the items in the InBox. Because myOlItems is declared ' "WithEvents" the ItemAdd event will fire below. Set myOlItems = Outlook.Session.GetDefaultFolder(olFolderInBox).Items End Sub Private Sub myOlItems_ItemAdd(ByVal Item As Object) ' Check to make sure it is an Outlook mail message,otherwise ' subsequent code will probably fail depending on what type ' of item it is. If TypeName(Item) = "MailItem" Then If Item.Subject = "My required Subject Line" Then ' Here's where you want to do some stuff. End If End If End Sub
接下来的部分是打开Excel并做任何你想做的事情.确保使用“工具:引用…”菜单项并选择Microsoft Excel xx.xx对象库来建立对excel对象库的引用.
你可能想要一些如下代码:
Private Sub Do_Excel_Stuff(MyContent As Object) Dim myXLApp As Excel.Application Dim myXLWB As Excel.Workbook Set myXLApp = New Excel.Application Set myXLWB = New Excel.Workbook ' Do your data processing here Set myXLWB = Nothing Set myXLApp = Nothing End Sub
有些人在Google或Stack Overflow上四处寻找应该为您提供有关如何处理Excel方法的实际数据处理部分的足够指示.
希望这足以让你入门.