throws me the same message.
\nI fixed your code and tested it.
please notice the declaration of the outlook object in the first 2 rows.
also \nin order to set contacts folder i have changed it from your original code to outlook.GetNamespace(\"MAPI\").GetDefaultFolder(MyOutlook.OlDefaultFolders.olFolderContacts);
using MyOutlook= Microsoft.Office.Interop.Outlook;\n\n private void FindContactEmailByName(string firstName, string lastName)\n {\n Microsoft.Office.Interop.Outlook.Application outlook;\n outlook = new Microsoft.Office.Interop.Outlook.Application();\n\n MyOutlook.MAPIFolder contactsFolder =\n outlook.GetNamespace(\"MAPI\").GetDefaultFolder(MyOutlook.OlDefaultFolders.olFolderContacts);\n MyOutlook.Items contactItems = contactsFolder.Items;\n try\n {\n MyOutlook.ContactItem contact =\n (MyOutlook.ContactItem)contactItems.\n Find(String.Format(\"[FirstName]='{0}' and \"\n + \"[LastName]='{1}'\", firstName, lastName));\n if (contact != null)\n {\n contact.Display(true);\n }\n else\n {\n MessageBox.Show(\"The contact information was not found.\");\n }\n }\n catch (Exception ex)\n {\n throw ex;\n }\n }\n
\n","author":{"@type":"Person","name":"Jonathan Applebaum"},"upvoteCount":1}}}Reputation: 380
I found this code here to search for Outlook contacts using C# winform. I'm having a hard time understanding why the code is not working as is. VS2015 is telling me "'Myapp' does not contain a definition for 'Application' and no extension method 'Application' accepting a first argument of type 'Myapp' could be found (are you missing a using directive or an assembly reference?)"
Here is the code:
private void FindContactEmailByName(string firstName, string lastName)
{
Outlook.NameSpace outlookNameSpace = this.Application.GetNamespace("MAPI"); //VS does not like this line, particularly "Application"
Outlook.MAPIFolder contactsFolder =
outlookNameSpace.GetDefaultFolder(
Microsoft.Office.Interop.Outlook.
OlDefaultFolders.olFolderContacts);
Outlook.Items contactItems = contactsFolder.Items;
try
{
Outlook.ContactItem contact =
(Outlook.ContactItem)contactItems.
Find(String.Format("[FirstName]='{0}' and "
+ "[LastName]='{1}'", firstName, lastName));
if (contact != null)
{
contact.Display(true);
}
else
{
MessageBox.Show("The contact information was not found.");
}
}
catch (Exception ex)
{
throw ex;
}
}
Upvotes: 0
Views: 933
Reputation: 5986
the line Outlook.NameSpace outlookNameSpace = this.Application.GetNamespace("MAPI");
throws me the same message.
I fixed your code and tested it.
please notice the declaration of the outlook object in the first 2 rows.
also
in order to set contacts folder i have changed it from your original code to outlook.GetNamespace("MAPI").GetDefaultFolder(MyOutlook.OlDefaultFolders.olFolderContacts);
using MyOutlook= Microsoft.Office.Interop.Outlook;
private void FindContactEmailByName(string firstName, string lastName)
{
Microsoft.Office.Interop.Outlook.Application outlook;
outlook = new Microsoft.Office.Interop.Outlook.Application();
MyOutlook.MAPIFolder contactsFolder =
outlook.GetNamespace("MAPI").GetDefaultFolder(MyOutlook.OlDefaultFolders.olFolderContacts);
MyOutlook.Items contactItems = contactsFolder.Items;
try
{
MyOutlook.ContactItem contact =
(MyOutlook.ContactItem)contactItems.
Find(String.Format("[FirstName]='{0}' and "
+ "[LastName]='{1}'", firstName, lastName));
if (contact != null)
{
contact.Display(true);
}
else
{
MessageBox.Show("The contact information was not found.");
}
}
catch (Exception ex)
{
throw ex;
}
}
Upvotes: 1