Home > Net >  Active Qt - add member to Outlook distribution list
Active Qt - add member to Outlook distribution list

Time:09-12

I'm trying to use Active Qt to modify a distribution list in Outlook. I'm able to access it and list all of its members with the following code:

QAxObject* outlook = new QAxObject("Outlook.Application");
QAxObject* session = outlook->querySubObject("Session");
QAxObject* contactsFolder = session->querySubObject("GetDefaultFolder(olFolderContacts)");
QAxObject* distList = contactsFolder->querySubObject("Items(QString)", "My contact list");

int memberCount = distList->property("MemberCount").toInt();
QAxObject* member;

for (int i = 1; i <= memberCount; i  ) {
    member = distList->querySubObject("GetMember(int)", i);
    qDebug() << member->property("Name").toString() << "     "
             << member->property("Address").toString();
    delete member;
}

But when I'm trying to add a member to the list:

QAxObject* newQaxMember = session->querySubObject("CreateRecipient(QString)", "Name LastName");
IDispatch* newMember = 0;
newQaxMember->queryInterface(QUuid("00020400-0000-0000-C000-000000000046"), (void**)&newMember);
distList->querySubObject("AddMember(IDispatch*)", QVariant::fromValue(newMember));

I'm getting an error:

QAxBase::querySubObject: AddMember(IDispatch*): Error calling function or property in ({0006103C-0000-0000-C000-000000000046})

When I use dynamicCall() method instead of querySubObject(), there's no error, but also no new member appear on the list in Outlook.

What am I doing wrong?

CodePudding user response:

Use the Resolve method right after a new recipient is created. The method attempts to resolve a Recipient object against the Address Book. And if the recipient is resolved (see the corresponding property) you may call the DistListItem.AddMember method to add a new member to the distribution list in Outlook. Here is how it looks in VBA:

Sub AddNewMember() 
 'Adds a member to a new distribution list 
 Dim objItem As Outlook.DistListItem 
 Dim objMail As Outlook.MailItem 
 Dim objRcpnt As Outlook.Recipient 
 Set objMail = Application.CreateItem(olMailItem) 
 Set objItem = Application.CreateItem(olDistributionListItem)
 'Create recipient for distlist 
 Set objRcpnt = Application.Session.CreateRecipient("Eugene Astafiev")
 objRcpnt.Resolve 
 objItem.AddMember objRcpnt 
 
 'Add note to list and display 
 objItem.DLName = "Northwest Sales Manager" 
 objItem.Body = "Regional Sales Manager - NorthWest"
 objItem.Save 
 objItem.Display 
End Sub

CodePudding user response:

You're right @Eugene Astafiev, it was necessary to call Resolve() method after creating a recipient. Moreover, the Save() method should be called right after adding the recipient.

Finally I have resolved the problem this way:

QAxObject* outlookApplication = new QAxObject("Outlook.Application");
QAxObject* session = outlookApplication->querySubObject("Session");
QAxObject* contactsFolder = session->querySubObject("GetDefaultFolder(olFolderContacts)");
QAxObject* distList = contactsFolder->querySubObject("Items(QString)", "My contact list");

QAxObject* newQaxMember = session->querySubObject("CreateRecipient(QString)", person.getAddress());
bool memberResolved = newQaxMember->dynamicCall("Resolve()").toBool();

if (memberResolved) {
    distList->dynamicCall("AddMember(Recipient)", newQaxMember->asVariant());
    distList->dynamicCall("Save()");
}
  • Related