VBA为什么if函数不能按特定短语过滤我的数据

时间:2018-09-02 18:51:01

标签: excel vba outlook outlook-vba

我的代码有问题。我想显示一个特定的电子邮件,并按特定的短语(包含在邮件的正文中)将excel.files中的email.bodys保存。以下代码中的if函数不起作用。它正在从我的邮箱下载所有电子邮件。

你能指出我出什么问题了吗?

Dim o As Outlook.Application
Set o = New Outlook.Application

Dim ons As Outlook.Folder
Set ons = o.GetNamespace("mapi")

Dim myfol As Outlook.Folder
Set myfol = ons.getdefaulfolder(olFolderDrafts)

Dim omail As Outlook.MailItem
Set omail = o.CreateItem(oimailItem)

Dim r As Long
r = 2
For Each omail In myflo.Items

    If omail.Body = "specific text" Then
        omail.Display
        Cells(r, 1).Value = omail.Body
        r = r + 1
    End If
Next omail    

2 个答案:

答案 0 :(得分:1)

您可以使用InStr函数检查电子邮件正文是否包含特定文本...

    self.master = master
    self.my_font = tkinter.font.Font(self.master, family="My Girl Is Retro Regular")
    self.master.grid_rowconfigure(0, weight=2)
    self.master.grid_columnconfigure(0, weight=1)
    self.playPhoto = PhotoImage(file='toppic.png')
    self.toppic = Label(self.master,image=self.playPhoto)
    self.toppic.grid(pady=(0,50))
    self.youtubeLinkLabel = Label(self.master, text="Paste the YouTube Link Below: ", font=("My Girl Is Retro Regular", 30),fg='red')
    self.youtubeLinkLabel.grid(pady=(0,20))
    self.youtubeEntryVar = StringVar()
    self.youtubeEntry = Entry(self.master, width=70, textvariable=self.youtubeEntryVar,font=("Agency FB", 25),fg='green')
    self.youtubeEntry.grid(pady=(0, 15),ipady=10)

    self.youtubeEntryError = Label(self.master, text="", font=("Concert One", 20))
    self.youtubeEntryError.grid(pady=(0, 8))

    # Asking where to save file label
    self.youtubeFileLocationLabel = Label(self.master, text="Choose Directory: ",font=("My Girl Is Retro Regular", 60))
    self.youtubeFileLocationLabel.grid()

    self.youtubeFileLocationEntry = Button(self.master, text="Directory", command=self.openDirectory)
    self.youtubeFileLocationEntry.grid(pady=(10, 3))

    # Entry label if user don`t choose directory
    # openDirectory func
    self.fileLocationLabelError = Label(self.master, text="", font=("My Girl Is Retro Regular", 20))
    self.fileLocationLabelError.grid()

    # What to download MP3 or MP4 label
    self.youtubeChooseLabel = Label(self.master, text="Please choose what to download: ",
                                        font=("My Girl Is Retro Regular", 30))
    self.youtubeChooseLabel.grid()

对于区分大小写的搜索,请将vbTextCompare替换为vbBinaryCompare。

答案 1 :(得分:-1)

根据您要求的报价,“从我的邮箱中完成电子邮件”(收件箱?)在引用“ olFolderDrafts”时 您需要添加要下载的邮件的ID。