在outlook中使用visual basic来移动附件 [英] Using visual basic in outlook to move an attachment

查看:96
本文介绍了在outlook中使用visual basic来移动附件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在处理一个脚本,该脚本从电子邮件中获取附件并将​​其移动到本地或网络文件夹。 我所使用的代码很有效,除了我必须登录Outlook才能使用它。 有没有办法可以在没有登录的情况下获得
这项工作? 以下是我正在使用的内容:

I am working on a script that take an attachment from an email and move it to a local or network folder.  The code that I have works great except for the fact that I have to be logged into Outlook for it to work.  Is there a way that I can have this work without being logged in?  Below is what I'm using:

Private WithEvents Items As Outlook.Items



Private Sub Application_Startup()

    Dim olApp As Outlook.Application

    Dim objNS As Outlook.NameSpace

   设置olApp = Outlook.Application

   设置objNS = olApp.GetNamespace(" MAPI")

    Set Items = objNS.GetDefaultFolder(olFolderInbox).Items

End Sub



Private Sub Items_ItemAdd(ByVal item As Object)



错误GoTo ErrorHandler



    '只有当它是MailItem时才会行动

    Dim Msg As Outlook.MailItem

   如果TypeName(item)=" MailItem"然后

       设置消息=项目



    '改变变量以匹配需求。评论或删除不必要的任何部分。

       如果(Msg.SenderName =" nbalsamo@woh.rr.com")和_

        (Msg.Subject =" test")和_

        (Msg.Attachments.Count> = 1)然后

       

    '设置要保存的文件夹。

    Dim olDestFldr As Outlook.MAPIFolder

    Dim myAttachments As Outlook.Attachments

    Dim Att As String

       

    '要保存的位置。 可以是根驱动器或映射网络驱动器。

    Const attPath As String =" C:\"

      

      

    '保存附件

  设置myAttachments = item.Attachments

    Att = myAttachments.item(1).DisplayName

    myAttachments.item(1).SaveAsFile attPath&请加
       

    '标记为已读<
   Msg.UnRead = False

结束如果

结束如果

   



ProgramExit:

 退出Sub $
 

ErrorHandler:

  MsgBox Err.Number& " - " &安培;错误描述

  Resume ProgramExit

End Sub



$

Private WithEvents Items As Outlook.Items

Private Sub Application_Startup()
    Dim olApp As Outlook.Application
    Dim objNS As Outlook.NameSpace
    Set olApp = Outlook.Application
    Set objNS = olApp.GetNamespace("MAPI")
    Set Items = objNS.GetDefaultFolder(olFolderInbox).Items
End Sub

Private Sub Items_ItemAdd(ByVal item As Object)

On Error GoTo ErrorHandler

    'Only act if it's a MailItem
    Dim Msg As Outlook.MailItem
    If TypeName(item) = "MailItem" Then
        Set Msg = item

    'Change variables to match need. Comment or delete any part unnecessary.
        If (Msg.SenderName = "nbalsamo@woh.rr.com") And _
        (Msg.Subject = "test") And _
        (Msg.Attachments.Count >= 1) Then
       
    'Set folder to save in.
    Dim olDestFldr As Outlook.MAPIFolder
    Dim myAttachments As Outlook.Attachments
    Dim Att As String
       
    'location to save in.  Can be root drive or mapped network drive.
    Const attPath As String = "C:\"
      
      
    ' save attachment
   Set myAttachments = item.Attachments
    Att = myAttachments.item(1).DisplayName
    myAttachments.item(1).SaveAsFile attPath & Att
       
    ' mark as read
   Msg.UnRead = False
End If
End If
   

ProgramExit:
  Exit Sub
 
ErrorHandler:
  MsgBox Err.Number & " - " & Err.Description
  Resume ProgramExit
End Sub


推荐答案

不,除非Outlook正在运行,否则Outlook VBA代码无法执行。 
No, Outlook VBA code can't execute unless Outlook is running. 


这篇关于在outlook中使用visual basic来移动附件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆