2016-04-06 2 views
0

Hier ist der ursprüngliche Code ist, wie beabsichtigt funktioniert:Outlook VBA ignoriert original Wenn Argument, wenn Else Argument hinzugefügt wird

Dim ContactsFolder As Folder 
Set ContactsFolder = Session.GetDefaultFolder(olFolderContacts) 
Dim SearchContactName As String 
Set myItem = Outlook.CreateItem(olMailItem) 
Dim NewContactEmail As String 
Dim NewMail As Outlook.MailItem 
Set NewMail = Application.ActiveInspector.currentItem 

' Ask user for inputs 
SearchContactName = InputBox("What is the name of your contact you want to comment to on Trello?") 

Dim Contact As ContactItem 

'search contacts' 
For Each Contact In ContactsFolder.Items 
    If Contact.FullName = SearchContactName Then 
     If Contact.IMAddress = "" Then 'add trello card link' 
      Contact.IMAddress = InputBox("What is the Trello card e-mail of this contact?") 
      Contact.Save 
      myItem.To = Contact.IMAddress 
     Else 
      myItem.To = Contact.IMAddress 
     End If 
    End If 
Next 

myItem.Body = NewMail.Body 
myItem.Subject = NewMail.Subject 

myItem.Display 

Meine Absicht dafür ist, dass, wenn es einen Kontakt findet, wird es sehen, ob es hat alles in der IMadresse. Wenn es einen Link hat, dann wird das in das "An:" der E-Mail gehen. Wenn dies nicht der Fall ist, wird Outlook den Benutzer nach einem Link fragen, die Kontaktinformationen speichern und den Link zum "An:" der E-Mail hinzufügen. Wie bereits erwähnt, funktioniert dieser Code für alle Kontakte in Outlook mit oder ohne IM-Adresse.

Das Problem, auf das ich stoße, ist, wenn keine Kontaktinformationen gespeichert sind. Hier ist der Code als abgespeckte wie ich den Fehler replizieren konnte:

Sub LeaveAComment() 
Dim ContactsFolder As Folder 
Set ContactsFolder = Session.GetDefaultFolder(olFolderContacts) 
Dim SearchContactName As String 
Set myItem = Outlook.CreateItem(olMailItem) 
Dim NewContactEmail As String 
Dim NewMail As Outlook.MailItem 
Set NewMail = Application.ActiveInspector.currentItem 

' Ask user for inputs 
SearchContactName = InputBox("What is the name of your contact you want to comment to on Trello?") 

Dim Contact As ContactItem 

'search contacts' 
For Each Contact In ContactsFolder.Items 
    If Contact.FullName = SearchContactName Then 
     If Contact.IMAddress = "" Then 'add trello card link' 
      Contact.IMAddress = InputBox("What is the Trello card e-mail of this contact?") 
      Contact.Save 
      myItem.To = Contact.IMAddress 
     Else 
      myItem.To = Contact.IMAddress 
     End If 
    Else 
     If MsgBox("Contact does not exist. Would you like to add one?", vbYesNo, "Confirm") = vbYes Then 
      End 
     Else 
      End 
     End If 
    End If 
Next 

myItem.Body = NewMail.Body 
myItem.Subject = NewMail.Subject 

myItem.Display 

Es ist, wenn ich das hinzufügen

Else 
     If MsgBox("Contact does not exist. Would you like to add one?", vbYesNo, "Confirm") = vbYes Then 
      End 
     Else 
      End 
     End If 
End If 

, die ich auf Probleme stoßen. Wenn ich jetzt das Makro ausführe, wird die msgbox ausgelöst, egal ob der Kontakt bereits existiert oder nicht. Aus irgendeinem Grund wird das ursprüngliche If Contact.FullName = SearchContactName von der Else überschrieben, obwohl die Else nicht ausgelöst werden sollte (zumindest in meinem Denkprozess). Ich habe versucht, die Logik umzukehren und zu tun:

For Each Contact In ContactsFolder.Items 
    If Contact.FullName <> SearchContactName Then 

Aber das brachte das gleiche Ergebnis heraus.

Irgendwelche Gedanken?

Antwort

0

Sie können den folgenden Teil der Innenseite für jede Schleife Code platzieren:

Else 
    If MsgBox("Contact does not exist. Would you like to add one?", vbYesNo, "Confirm") = vbYes Then 
     End 
    Else 
     End 
    End If 
End If 

Nehmen wir an, Sie 100 Kontakte haben und wollen eine mit dem Namen „Charlie“ zu finden. Der Teil des obigen Codes gibt das Meldungsfeld an, dass der Kontakt nicht existiert, nachdem nur 1 von 100 Kontakten überprüft wurde.

Sie müssen das Meldungsfeld außerhalb der Schleife platzieren. Sie können boolean verwenden zu überprüfen, ob "Charlie" Kontakt gefunden wurde:

Dim contactFound as boolean 
For Each Contact In ContactsFolder.Items 
    If Contact.FullName = SearchContactName Then 
     contactFound = true 

Dann kurz nach der Schleife:

If contactFound = false then 
     If MsgBox("Contact does not exist. Would you like to add one?", vbYesNo, "Confirm") = vbYes Then 
     End 
     Else 
     End 
     End If 
    End if 
Verwandte Themen