Runtime Error with the NS.GetSharedDefaultFolder Method - vba

Running it from MS Access (2007) via the Outlook Interop library. I get the error -2147221219 (8004011d) from the starred line on one user account, but not on another. Error appears to be related to permissions, and both accounts have Full Access permissions to the account who's calendar I'm trying to open and can open and create appointments to it via Outlook. Sample code
Public Function NewApt(MtgDate As Date, Cat As String)
Dim objOLApp As Outlook.Application
Dim objNS As Outlook.Namespace
Dim objCalendar As Outlook.Folder
Dim NewMtg As Outlook.AppointmentItem
Dim Org As Outlook.Recipient
Set objOLApp = New Outlook.Application
Set objNS = objOLApp.GetNamespace("MAPI")
Set Org = objNS.CreateRecipient("tuser#somewhere.com")
Org.Resolve
If Org.Resolved Then
** Set objCalendar = objNS.GetSharedDefaultFolder(Org, olFolderCalendar)
Else
MsgBox "Scheduling User failed to resolve, see Crimius."
Exit Function
End If
...
Any ideas why?

I know one reason why this error may appear.
Whe you use the GetSharedDefaultFolder method and the recipient in parameter 1 (Recipient) is hidden from the global address list such an error can occur:
COMException (0x8004011D):
The operation failed because of a registry or installation problem. Restart Outlook and try again. If the problem persists, reinstall.

Maybe, the Outlook-Datafile is protected by password.
Switch to Outlook, enter the Password for the Outlook-Container, and then try again.

I had exactly the same issue. A VBA module that ran during years suddenly refused to. After verification it turned out that the internal e-mail addresses changed to previous runs of the macro...
Changing to email addresses solved the problem.

Related

Outlook 2003: Connect IMAP server with VBA

Sometimes Outlook (2003) loses the connection to one or more IMAP server. With VBA scripts that are supposed to move mails to these mailboxes, for example, I get this error message:
"Runtime error '-972759285 (c604df0b)':
Connection to server is unavailable. Outlook must be online or connected to complete this operation."
I then first have to click on "File" - "Connect to [MAILBOX...]" to establish this connection manually.
I am looking for a VBA solution to automatically connect to multiple mailboxes (IMAP only), but I don't know what to look for in VBA references.
I tried this:
Sub MyTest()
Dim myNameSpace As Outlook.NameSpace
Set myNameSpace = Application.GetNamespace("MAPI")
Set Application.ActiveExplorer.CurrentFolder = myNameSpace.Folders("C-Interessenten").Folders("Interessenten")
Set myNameSpace = Nothing
End Sub
or this
Sub IsOLOffline()
'Determines whether Outlook is currently offline.
Dim myOlApp As Outlook.Application
Dim myNameSpace As Outlook.NameSpace
Set myOlApp = New Outlook.Application
Set myNameSpace = myOlApp.GetNamespace("MAPI")
Debug.Print myNameSpace.Offline
End Sub
Thank you for an idea.
The Outlook object model doesn't provide anything for that out of the box.
The Offline property of the Namespace class returns valid information only for an Exchange profile. It's not intended for non-Exchange account types such as POP3, IMAPI, and HTTP.
You may try to use SyncObjects property of the Namespace class. It returns a set of SyncObject objects representing the Send/Receive groups for a user.
The OnError event is fired when Microsoft Outlook encounters an error while synchronizing a user's folders using the specified Send\Receive group. So, it could help with detecting such cases and then initiating a new sync.
Public WithEvents mySync As Outlook.SyncObject
Sub Initialize_handler()
Set mySync = Application.Session.SyncObjects.Item(1)
mySync.Start
mySync.Stop
End Sub
Private Sub mySync_OnError(ByVal Code As Long, ByVal Description As String)
MsgBox "Unexpected sync error" & Code & ": " & Description
End Sub
A Send\Receive group lets users configure different synchronization scenarios, selecting which folders and which filters apply.
Use the Item method to retrieve the SyncObject object from a SyncObjects object. Because the Name property is the default property of the SyncObject object, you can identify the group by name.
Thank you for the food for thought. I have already experimented a bit by creating a separate group for each IMAP mailbox. Testing is taking a long time because the connections only break sporadically and I can't trigger the break manually. Nevertheless, I have more and more the impression that the SyncObject does not lead to a solution. Thanks anyway.
Is there perhaps the possibility to call the menu item "File" - "Connect to xxx" via vba, for example via FindControl()?

VB: Generate E-Mail via outlook

Microsoft outlines how to leverage the Microsoft.Office.Interop.Outlook namespace quite well. example
I currently have a window form app created in VB, with a series of text fields requiring input. Upon submission of form, I have a text file that is created locally, but I also want to chain that event with the creation of an email, in a template fashion, having the user values embedded in the body of said email.
In my initial testing, to simply just generate the email, I have imported the appropriate function based on MS's documentation.
Imports Outlook = Microsoft.Office.Interop.Outlook
However when I enter the code provided after my text file creation code, there are many errors highlighted. I was prompted to create a friendclass for "Office."
Build does not like the CType(Application.CreateItem(Outlook.OlItemType - I observe the error CreateItem is not a member of Application.
Is this due to the windows form I selected when building this project?
Dim mail As Outlook.MailItem = CType(Application.CreateItem(Outlook.OlItemType.olMailItem), Outlook.MailItem)
Any help would be appreciated..
this appears to work - different approach but appears to work..
Dim oApp As Outlook.Application
oApp = New Outlook.Application
Dim oMsg As Outlook.MailItem
oMsg = oApp.CreateItem(Outlook.OlItemType.olMailItem)
oMsg.Recipients.Add("test#test.com")
oMsg.Subject = "test"
oMsg.HTMLBody = "<HTML><BODY>test - test</BODY></HTML>"
oMsg.Display()
now it is to figure out how to define a variable within the htmlbody section...

Send Email from MS Project via Outlook

I am trying to open and populate emails from MS Project via Outlook. I have done this before from Excel, but when I drop the code into Project I get an error.
'Create and Set New Mail Item
Dim OutlookApp As Outlook.Application
Set OutlookApp = CreateObject("Outlook.Application")
Dim OutlookMail As Outlook.MailItem
Set OutlookMail = OutlookApp.CreateItem(OutlookMailItem)
The error triggers on the Dim OutlookApp line and reads, 'User-Defined Type Not Defined'
What am I forgetting here?
I'm not sure I understand the requirement, so I'm sending a blind shot. In Project 2007 you'll see a combo box at the top right of the screen, probably defaulting to "All Tasks". Choose the date range you want with the info you need and send this to your team along with any encouraging words that you need to motivate them.
Will this help?

Outlook does not recognize one or more names

I have following vba code which reads a mailbox and sends reply to any users who send a invalid code as a reply to the mailbox, but sometimes the run time error (Outlook does not recognize one or more names) is received. My questions are,
Will creation of new MAPI profile resolve the issue or do i need to add a code that resolves the address and ignores if the email id no longer exist. if yes how do i do that?
Also in general whats the parameter to not send emails for specific condition?
Below is the code that we currently have:
Sub ResponseCodeError(Item As Outlook.MailItem)
'If not a valid code then send email to the User
If (Left(Item.Subject, 2) <> "S;" And Left(Item.Subject, 2) <> "N;") Then
Dim outobj, mailobj
Set outobj = CreateObject("Outlook.Application")
Set mailobj = outobj.CreateItem(0)
With mailobj
.To = Item.SenderEmailAddress
.Subject = "Invalid Code"
.Body = "Please use a valid CODE"
.Send
End With
'Move Email to Error Folder
mailboxNameString = "mailboxname"
FolderName = "Error"
Dim olApp As New Outlook.Application
Dim olNameSpace As Outlook.NameSpace
Dim olCurrExplorer As Outlook.Explorer
Dim olCurrSelection As Outlook.Selection
Dim olDestFolder As Outlook.MAPIFolder
Set olNameSpace = olApp.GetNamespace("MAPI")
Set olCurrExplorer = olApp.ActiveExplorer
Set olCurrSelection = olCurrExplorer.Selection
Set olDestFolder = olNameSpace.Folders(mailboxNameString).Folders(FolderName)
Item.Move olDestFolder
End If
Set outobj = Nothing
Set mailobj = Nothing
End Sub
I had once the same error, and I resolved it after 5 hours searching like crazy in the code. But it was much simpler: 1 email address had an error missing the .(dot) in the domain name.
Instead of setting the To property, call MailItem.Recipients.Add (returns Recipient object). Call Recipient.Resolve - it will return false if the name cannot be resolved.
The issue might be due to incorrect names, extra characters or spaces in some property of Item.(especially To, BCC, CC or collective property Recipients)
It might also be due to names not resolved yet before sending the mail. I am not sure but would assume that the error was due to trying to resolve names while sending the mail and probably being unable to resolve them due to some issue. Explicitly resolving names like the code below before the mail is sent should solve the issue.
Item.Recipient.ResolveAll can be used to resolve the names before sending the mail. It returns true if all names were successfully resolved.
Code: (Reference)
If Not myRecipients.ResolveAll Then
For Each myRecipient In myRecipients
If Not myRecipient.Resolved Then
MsgBox myRecipient.Name
End If
Next
End If
I have tested the code without adding and resolving recipients 1 by 1.(suggested by Dmitry)
I used Item.To, Item.BCC properties. Then used ResolveAll and send the mail only if all names are resolved.
I just ran into this error; code that had been working for years suddenly triggered the "Outlook does not recognize one or more names" error.
I discovered that the recipient was an Outlook shared folder name, ie. "My Shared Folder" and whether it is an Access 2016 or Outlook issue, the name could no longer resolve to its associated email address. Changing the recipient to "mysharedfolder#blahblah.com" resolved the issue for me.
SOLVED In my case in Outlook I had several contacts with the same e-mail (two companies that are run by one contact/e-mail) and that created the problem. I deleted one of the contacts so no e-mail is repeated in the contact list in Outlook and now it works. Note that my batch still sends an e-mail twice to that contact which info for each company which is what I wanted it.

Outlook code is working when manually called but giving trouble from Application_ItemSend

I have a code that checks the recipient of the mail, looks what organization is set in the address book for the recipient and dependent on that sets the "SentOnBehalfOfName"-property of the item. If the recipient is working for client2, he will get the mail from "we_love_to_serve_client2#domain.com".
I call the code either before sending the mail via a button in my ribbon, that calls this Sub:
Sub Signatur()
Dim olApp As Outlook.Application
Dim objMail As Outlook.MailItem
Set olApp = Outlook.Application
Set objMail = Application.ActiveInspector.CurrentItem
Call Signatur_auto(objMail)
End Sub
I do this if I want to know which mail-adress is going to be chosen.
In the itemSend-section of thisOutlookSession I also call the same sub
Call Signatur_auto(Item)
Part of the Signatur_auto (i do not copy that in, the question is too long already...) is dealing with the SentOnBehalfOfName-property, the other part is putting the item into the right folder. The Folder is chosen depending on the SentOnBehalfOfName-property.
Now comes the interesting part: Although the folder-part is always working (which can only be when the SentOnBehalfOfName has worked before), the SentOnBehalfOfName only works "half". In the preview-line the mail sent is shown as from "we_serve_client2#domain.com", but when I open the mail it says it was sent by me. The Client always only sees my address, and also answers to my address - which I do not want....
How cant be, that the same code is having different results dependent on where it is called? Is it a Problem to change the sendonbehalf-field in the item send-section?
Thanks for any Inputs!
Max
Why it does not work?
Try this in ItemSend.
Dim copiedItem As mailItem
Set copiedItem = Item.Copy
copiedItem.SentOnBehalfOfName = "we_love_to_serve_client2#domain.com"
copiedItem.Send
Item.delete
Cancel = True ' In case your setup generates an error message as described in the comments
Why it works? Appears "copiedItem.Send" bypasses ItemSend.