2017-02-08 1 views
1

Je ne suis pas un expert VBA, mais un ami a écrit une macro qui lors de l'envoi d'un e-mail à une personne à partir d'une liste (MoveList) déplacerait automatiquement l'e-mail des éléments envoyés vers un autre dossier.Outlook VBA non exécuté

Jusqu'à aujourd'hui, cela a bien fonctionné - J'ai vérifié les paramètres de macro dans Outlook (modifié pour exécuter toutes les macros) et cela ne fonctionne toujours pas.

Des idées? (Je luttais pour coller la macro, il se plaignait sur le formatage, donc ive uploadés ici)

Option Explicit 
Dim objXL As Object 
Dim objWB As Object 
Dim objWS As Object 
Dim objRange As Object 

Private Function InList(ByVal ToList As String, ByVal DistList As Outlook.DistListItem) As Boolean 
################################################### 
## this function checks if the To list contains # 
## Any of the names in the supplied Distribution # 
## list using a string compare     # 
################################################### 
Dim i As Integer 
Dim test As String 
InList = False 

    For i = 1 To DistList.MemberCount # check if each name is in the to list 
     test = DistList.GetMember(i).Name 
     If InStr(1, ToList, test) Then 
      InList = True  # if name is in the to list then set function to true 
     End If 
    Next i 

End Function 

Private Function TwoMonths() As String 
################################################### 
## this function returns the date 2 months before # 
## today. This does not return the time elelment # 
##            # 
################################################### 

Dim today As String 
Dim day As Integer 
Dim month As Integer 
Dim year As Integer 

today = Now #now returns todays date in the format dd/mm/yyyy hh:mm:ss 

day = Left(today, 2) 
month = Mid(today, 4, 2) 
year = Mid(today, 7, 4) 

If month < 2 Then # checks if 2 months ago is in previous year and corrects for this 
    year = year - 1 
    month = 10 + month 
Else 
    month = month - 2 
End If 

TwoMonths = day & "/" & month & "/" & year 

End Function 

Sub MoveEmails() #(ByVal MoveFrom As String, ByVal MoveTo As String, Distributionlist As String) 

#################################################### 
## This subroutine will move any mail that is sent # 
## any person in the distribution list MoveList in # 
## the last 2 months from the Sent folders   # 
#################################################### 


    Dim DefaultInbox As Outlook.Folder 
    Dim folDefaultSentItems As Outlook.Folder 
    Dim folDestFolder As Outlook.Folder 
    Dim DefaultContacts As Outlook.Folder 
    Dim dlContactList As Outlook.DistListItem 
    Dim TopFolder As Outlook.Folder 
    Dim itSentEmails As Outlook.Items 
    Dim myItem As Object 
    Dim i As Long 
    Dim counter As Integer 
    Dim filterCriteria As String 
    Dim filteredItemsCollection As Outlook.Items 
    Dim Last2Months As String 
    Dim imail 
    Dim mynamespace 

     Set mynamespace = Application.GetNamespace("MAPI") 

     Set DefaultInbox = mynamespace.Folders("my [email protected]") # Change for your primary inbox name 
     Set DefaultContacts = mynamespace.GetDefaultFolder(olFolderContacts) 

     Set folDefaultSentItems = DefaultInbox.Folders("Sent Items") #selects "Sent Items" folder to move from 

     Set TopFolder = mynamespace.Folders("Misc") # Change for your Second inbox name 

     Set folDestFolder = TopFolder.Folders("Sent (Other)") # Set destination folder 

     Set dlContactList = DefaultContacts.Items("MoveList") # Selects the distribution list to use for check 

     Set itSentEmails = folDefaultSentItems.Items  # select all items in "Sent Items" 

     # the next section restricts search to only items sent in the last 2 months 
     # This is to limit the number of emails checked. Assumes that 
     # this macro is run at a frequency less than 2 months 

     Last2Months = TwoMonths 
     filterCriteria = "[ReceivedTime] > """ & Last2Months & " 12:00 AM""" 
     Set filteredItemsCollection = itSentEmails.Restrict(filterCriteria) 

    #loop until all emails are checked 

     i = 1 
     While i <= filteredItemsCollection.Count  
    #loop until all emails are checked 


    # check if it is a mail item 
     If filteredItemsCollection(i).Class = olMail Then 

    # check if to list contains one of the emails in the distribution list 

      If InList(filteredItemsCollection(i).To, dlContactList) Then 

    # If it is in the list move the email to the destination folder 

       filteredItemsCollection(i).Move folDestFolder 

    # Reset the restricted list. When the email list is moved it changes the indexing 
    # in the restricted list so the index loop needs to be decramented and the restriction 
    # list reset. (Error cataching) 

       Set filteredItemsCollection = itSentEmails.Restrict(filterCriteria) 
       i = i - 1 

      End If 

     End If 
     i = i + 1 # incrament index reference 
     Wend 
    End Sub 
+0

Bienvenue dans StackOverflow! Le script affiche-t-il des erreurs? Est-il possible que les paramètres régionaux (par exemple, le format de date) aient changé? – Lyth

+0

Les hachages ne sont pas les indicateurs de commentaire dans VBA donc cela ne compilerait pas. Ce qui m'a un peu confus. Il y a un module dans Outlook appelé 'ThisOutlookSession'. Quoi de neuf? Je suppose que vous utilisiez l'événement ItemSend pour déclencher tout cet autre code et quelque chose est arrivé à cela. –

+0

@Lyth aucun paramètre n'a changé, tout est toujours dans le même format (jj/mm/aaaa et h: mm: ss tt – metaljay

Répondre

1

En Février le mois calcule à zéro dans la fonction TwoMonths

Ajouter ceci:

If month = 0 Then 
    month = 12 
    year = year - 1 
End If 
+0

il y a déjà un commentaire pour cela dans la macro: Si le mois <2 Puis 'vérifie s'il ya 2 mois est dans l'année précédente et corrige pour cela année = année - 1 mois = 10 + mois – metaljay

+0

merci pour l'aide, il fonctionne si je l'exécute via l'éditeur VBA, mais dans Outlook, il doesn' doesn ' t travail si en appuyant sur envoyer – metaljay

+0

Vous devriez avoir le code ItemSend dans ThisOutlookSession qui appelle MoveEmails Mettre un point d'arrêt à MoveEmails dans ItemSend et voir si vous arrivez à cette ligne. – niton