VSTO 2007 outlook addin crashes - com

I am creating VSTO 2007 outlook Addin using COM . It reads all the mailtems from outlook and marks(categories) the undelivered mail items as undelivered.
I am using below code for marking the undelivered mail item as undelivered. The code marked in read colour crashes sometimes. Please suggest me the problem.
HRESULT hrGetSelectedItem;
LPDISPATCH spOlSelectedItem = NULL;
CComPtr<Outlook::_Explorer> spExplorer;
//Locating the selected item
CComPtr<Outlook::Selection> spOlSel;
if(m_spApp)
{
//Get the Currently Active Explorer on the top of Desktop
hrGetSelectedItem = m_spApp->ActiveExplorer(&spExplorer);
if(SUCCEEDED(hrGetSelectedItem))
{
hrGetSelectedItem = spExplorer->get_Selection(&spOlSel);
if(FAILED(hrGetSelectedItem))
{
MessageBox(NULL,GetStringFromTable(IDS_SELECTITEM),MSGBOX_HEADER, MB_OK|MB_ICONINFORMATION);
return ;
}
iMailIndex+=1;
VARIANT covIndex;
covIndex.vt = VT_I4;
covIndex.lVal = iMailIndex;
if(spOlSel)
{
hrGetSelectedItem = spOlSel->Item(covIndex,&spOlSelectedItem);
CComQIPtr <Outlook::_MailItem> spMailItem;
if(spOlSelectedItem)
{
hrGetSelectedItem = spOlSelectedItem->QueryInterface(&spMailItem);//Get The selected item
if(spMailItem)
{
spMailItem->put_Categories(L"Undelivered");
spMailItem->Save();
}
}
}
}
} LPDISPATCH spOlSelectedItem = NULL;
CComPtr<Outlook::_Explorer> spExplorer;
//Locating the selected item
CComPtr<Outlook::Selection> spOlSel;
if(m_spApp)
{
//Get the Currently Active Explorer on the top of Desktop
hrGetSelectedItem = m_spApp->ActiveExplorer(&spExplorer);
if(SUCCEEDED(hrGetSelectedItem))
{
hrGetSelectedItem = spExplorer->get_Selection(&spOlSel);
if(FAILED(hrGetSelectedItem))
{
MessageBox(NULL,GetStringFromTable(IDS_SELECTITEM),MSGBOX_HEADER, MB_OK|MB_ICONINFORMATION);
return ;
}
iMailIndex+=1;
VARIANT covIndex;
covIndex.vt = VT_I4;
covIndex.lVal = iMailIndex;
if(spOlSel)
{
hrGetSelectedItem = spOlSel->Item(covIndex,&spOlSelectedItem);
CComQIPtr <Outlook::_MailItem> spMailItem;
if(spOlSelectedItem)
{
hrGetSelectedItem = spOlSelectedItem->QueryInterface(&spMailItem);//Get The selected item
if(spMailItem)
{
spMailItem->put_Categories(L"Undelivered");
spMailItem->Save();
}
}
}
}
}
Thanks in advance.

You need to check that spExplorer is not null. Outlook can be open with no Explorers.

Related

Google Script - Attachments to Drive

I have the code below, but i've been struggling to get it to work properly, either it creates duplicate folders every time i run it, or it doesn't upload the attachments and just creates the folders... I am also getting an error now that the newMail Uploads object does not have a .hasnext() function.
What i want to do, is have this script running and it puts attachments in a folder relating to their label -- So in the code below, all mail with the newMail label would go to a single folder, but i want to be able to extend the code further to run for multiple labels ect, so i want to check if the relevant folders exist and if not create them, if they exist they should be used.
-Edit, Now it is only taking the attachment from the first email from a certain address.
function startProcess()
{
var gmailLabels = "newLabel";
var driveFolder = "newFolder";
var archiveLabel = "Processed";
var moveToLabel = GmailApp.getUserLabelByName(archiveLabel);
if ( ! moveToLabel )
{
moveToLabel = GmailApp.createLabel(archiveLabel);
}
findFolder(gmailLabels, driveFolder, archiveLabel, moveToLabel);
}
function findFolder(gmailLabels, driveFolder, archiveLabel, moveToLabel)
{
var filter = "has:attachment label:" + gmailLabels;
var folder = DriveApp.getFoldersByName(driveFolder);
if (folder.hasNext()) {
folder = folder.next();
} else {
folder = DriveApp.createFolder(driveFolder);
}
callThreads(gmailLabels, driveFolder, archiveLabel, moveToLabel, filter, folder)
}
function callThreads(gmailLabels, driveFolder, archiveLabel, moveToLabel, filter, folder)
{
var threads = GmailApp.search(filter);
for (var x=0; x<threads.length; x++) {
var label = GmailApp.getUserLabelByName(gmailLabels);
var message = threads[x].getMessages()[x];
var desc = message.getSubject() + " #" + message.getId();
var att = message.getAttachments();
for (var z=0; z<att.length; z++) {
try {
file = folder.createFile(att[z]);
file.setDescription(desc);
}
catch (e) {
Logger.log(e.toString());
}
}
//threads[x].addLabel(moveToLabel);
label.removeFromThreads(threads);
//threads[x].moveToTrash();
}
}
This is just a suggestion. I haven't tested this code, it probably doesn't work. But that's not the point. I'm trying to show how you might create some order to your code so that it's easier to understand and debug:
function sendToGoogleDrive() {
makeNewFolders();
putEmailsIntoFolders();
};
function makeNewFolders() {
var gmailLabels = "newMail";
var driveFolder = "newMail";
var archiveLabel = "Processed";
var rootUploadFolders ="newMail Uploads";
var rootDriveFolder = DriveApp.getFolders();
var rootExist = false;
var childExist = false;
while (rootDriveFolder.hasNext()) {
var folder = rootDriveFolder.next();
if(folder.getName()==rootUploadFolders) {
var folderId = folder.getId();
if(folder.getName()!==driveFolder) {
var child = DriveApp.getFolderById(folderId).createFolder(driveFolder);
};
};
};
};
function putEmailsIntoFolders() {
};

how to over come System out memory exception in windows phone 8?

I'm Developing windows phone 8 application.
In my application i'm using listbox.I bind listbox value form Webservice. Webservice return json format data's.
The webservice return 40 to 50 records.I bind all the values into listbox.
Everthing work fine.Only on first time run.
For example:-
My project page structure.
1.welcomepage
2.Menu Page (it contain six buttons. click on any one particular button it's redirect to sub-menupage)
3.Sub-menupage(Six different page present)
In menu page Hotels button is present . if hotels button is clicked it's navigate to Hotels Page.
Now my problem is:-
First Time - welcomepage -> Menu Page -> HotelSubmenu [List of hotels is bind in listbox from webservice]
Now I goback to Menupage by click on hardwareback button.Now it's in Menu page
If i click again the same Hotels button
It show me the error
e.ExceptionObject {system.OutofMemoryException:Insufficient memory to continue the execution of the program.
[System.outofmemoryException]
Data {system.Collections.ListDictionaryInternal}
HelpLink null
Hresult -2147024882
Message "insufficient memory to continue the execution of the program"
My C# code for bind values in listbox
public void commonbind()
{
try
{
this.loadimg.Visibility = System.Windows.Visibility.Visible;
loadcheck();
string common_url = "http://xxxxxx.com/Service/bussinesscitynamesub.php?bcatid=" + businessid + "&cityname=" + cityname + "&bsubid=" + filterid;
WebClient common_wc = new WebClient();
common_wc.DownloadStringAsync(new Uri(common_url), UriKind.Relative);
common_wc.DownloadStringCompleted += common_wc_DownloadStringCompleted;
}
catch (Exception ex)
{
}
}
void common_wc_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
{
try
{
lbbusiness.Items.Clear();
var common_val = e.Result;
if (common_val != "null\n\n\n\n")
{
lbopen = 2;
var jsonconvertvalue = JsonConvert.DeserializeObject<List<common_bindclass>>(common_val);
List<common_bindclass> ls = new List<common_bindclass>();
ls = jsonconvertvalue;
for (int i = 0; i < ls.Count; i++)
{
lbbusiness.Items.Add(ls[i]);
}
this.loadimg.Visibility = System.Windows.Visibility.Collapsed;
loadcheck();
}
else
{
if (lbopen == 0)
{
MessageBoxResult resultmsg = MessageBox.Show("No Result present based on your current Location! " + System.Environment.NewLine + "Now showing result without Location based", "Sorry!", MessageBoxButton.OK);
if (resultmsg == MessageBoxResult.OK)
{
lbbusiness.Items.Clear();
cityname = "";
**commonbind();**
}
else
{
NavigationService.GoBack();
}
}
else if (lbopen == 1)
{
MessageBox.Show("No Result Present In this Categories");
LPfilter.Open();
}
else if (lbopen == 2)
{
MessageBox.Show("No Result Present In this City");
Lpcity.Open();
}
}
}
catch (Exception ex)
{
}
}
I try with following method for solve the memory exception
Try1:-
Clear the List box value before bind every time.
Try2:-
Create the new list every time
common_bindclass bind = new common_bindclass();
foreach (common_bindclass bind in jsonconvertvalue)
{
lbbusiness.Items.Add(bind);
}
I change the above code to
List<common_bindclass> ls = new List<common_bindclass>();
ls = jsonconvertvalue;
for (int i = 0; i < ls.Count; i++)
{
lbbusiness.Items.Add(ls[i]);
}
MY Output For single List
But my try's not help for me .
any one tell me how to solve it. or alternate way .
How to find in which line the error occur .[I try with break point but it's not help me]

Treeviews QueueDraw doesn't render current row?

I'm working with a treeview, which contains several columns, also one displaying a pixbuf, if audio is playing or paused. If the user double clicks on one row, audio playback starts and the row needs to be rerendered in order to display the pixbuf icon. I used QueueDraw for this, but that does only work, if the cursor leaves the current row. How can I update the pixbuf directly?
CODE:
protected void trvMainCuesheetRowActivated (object o, RowActivatedArgs args)
{
log.debug("trvMainCuesheetRowActivated called");
TreeIter ti = TreeIter.Zero;
this.lsCuesheetData.GetIter(out ti,args.Path);
if (this.lsCuesheetData.GetValue(ti,0) != null)
{
Track tCurTrack = (Track)this.lsCuesheetData.GetValue(ti,0);
if (this.objProgram.getAudioManager().getPlayState() == AudioCuesheetEditor.AudioBackend.PlayState.Stopped)
{
this.objProgram.getAudioManager().play(tCurTrack);
this.refresh();
}
else
{
if (this.objProgram.getAudioManager().getPlayState() == AudioCuesheetEditor.AudioBackend.PlayState.Playing)
{
this.objProgram.getAudioManager().seek(tCurTrack);
this.refresh();
}
}
}
}
private void renderPlaying(TreeViewColumn _tvcColumn, CellRenderer _crCell, TreeModel _tmModel, TreeIter _tiIter)
{
Track tCurTrack = (Track)_tmModel.GetValue (_tiIter, 0);
//Just display an icon, if we are playing
if (this.objProgram.getAudioManager().getPlayState() == AudioCuesheetEditor.AudioBackend.PlayState.Playing)
{
if (this.objProgram.getAudioManager().getCurrentlyPlayingTrack() == tCurTrack)
{
Gdk.Pixbuf icon = this.RenderIcon(Stock.MediaPlay, IconSize.SmallToolbar, null);
(_crCell as CellRendererPixbuf).Pixbuf = icon;
}
else
{
(_crCell as CellRendererPixbuf).Pixbuf = null;
}
}
else
{
if (this.objProgram.getAudioManager().getPlayState() == AudioCuesheetEditor.AudioBackend.PlayState.Paused)
{
if (this.objProgram.getAudioManager().getCurrentlyPlayingTrack() == tCurTrack)
{
Gdk.Pixbuf icon = this.RenderIcon(Stock.MediaPause, IconSize.SmallToolbar, null);
(_crCell as CellRendererPixbuf).Pixbuf = icon;
}
else
{
(_crCell as CellRendererPixbuf).Pixbuf = null;
}
}
else
{
(_crCell as CellRendererPixbuf).Pixbuf = null;
}
}
}
//Purpose: Function used to refresh the MainWindow depending on new options set.
public void refresh()
{
//QueueDraw is needed since it fires a signal to cellrenderers to update
this.trvMainCuesheet.QueueDraw();
this.sbMainWindow.Visible = this.objProgram.getObjOption().getBShowStatusbar();
this.mwToolbar.Visible = this.objProgram.getObjOption().getBToolbarVisible();
}
Greetings
Sven
Found the error myself.
this.objProgram.getAudioManager().getCurrentlyPlayingTrack()
didn't always return a track, where I expected one, so the renderer worked right. Bug is fixed, thanks anyway ;).

Adding category to new mails

I am creating a VSTO 2007 Addin using COM. My requirement is to mark all the new mails into Blue category. I have the following code in OnNewMailEx handler
HRESULT hrGetNewMail;
_NameSpacePtr pMAPI = NULL;
hrGetNewMail = spApp->GetNamespace((_bstr_t)GetStringFromTable(147),&pMAPI);
if(FAILED(hrGetNewMail))
{
if(spApp!=NULL)
spApp.Release ();
return OPERATION_FAILED;
}
if(spApp!=NULL)
spApp.Release ();
CComBSTR EntryStoreID;
MAPIFolderPtr spMAPIFolder = NULL;
hrGetNewMail = pMAPI->GetDefaultFolder (olFolderInbox, &spMAPIFolder);
if(FAILED(hrGetNewMail))
{
if(pMAPI!=NULL)
pMAPI.Release ();
return OPERATION_FAILED;
}
hrGetNewMail = spMAPIFolder->get_StoreID (&EntryStoreID);
if(FAILED(hrGetNewMail))
{
if(spMAPIFolder!=NULL)
spMAPIFolder.Release ();
if(pMAPI!=NULL)
pMAPI.Release ();
}
if(spMAPIFolder!=NULL)
spMAPIFolder.Release ();
VARIANT varEntryStoreID;
hrGetNewMail = EntryStoreID.CopyTo (&varEntryStoreID);
if(FAILED(hrGetNewMail))
{
return OPERATION_FAILED;
}
IDispatch* spLatestMailitem;
hrGetNewMail = pMAPI->GetItemFromID (EntryID,varEntryStoreID,&spLatestMailitem);
if(FAILED(hrGetNewMail))
{
if(pMAPI!=NULL)
pMAPI.Release ();
}
if(pMAPI!=NULL)
pMAPI.Release ();
CComQIPtr <Outlook::_MailItem> spMailItem;
hrGetNewMail=spLatestMailitem->QueryInterface(&spMailItem);
HRESULT hrCat = spMailItem->put_Categories(_T("Blue Category"));
//spMailItem->put_FlagIcon(olRedFlagIcon);
hrCat = spMailItem->Save();
after execution when i open the new mails it is showing the category as Blue but in the inbox it is not marked in any category. When i close and open the outlook the category is gone even when i open the mail which was earlier marked as blue category. however i could add a flag which is there whenever i close and open the outlook. please suggest me the problem
If the category does not exist in the master category list I don't think it keeps it. To add a category to the master category list. See for more information http://msdn.microsoft.com/en-us/library/microsoft.office.interop.outlook.categories.aspx
Also you example will overwrite any existing categories. you should check if a category is already specified and if so seperate the existing value and value you wish to add with a comma.
var existingCategories = item.Categories;
if (string.IsNullOrWhiteSpace(existingCategories))
{
item.Categories = "MyCategory";
}
else
{
if (item.Categories.Contains("MyCategory") == false)
{
item.Categories = existingCategories + ", MyCategory";
}
}
item.Save();

SharePoint get current list item from a collection

ive made a small app using c# for sp 2010, what i am doing is getting items from a collection and viewing specific fields that i want to, the problem is when i click an item it shows me the details of every item assigned to the current user, how can i show only the details of the current item which the user clicks, below is my code...thanks
foreach (SPListItem myItem in myItemCollection)
{
if (myList.Fields.ContainsField("Title"))
{
EntreeListItemDetailNameValue l = lGrp.AddListItem<EntreeListItemDetailNameValue>();
SPField myField1 = myList.Fields.GetField("Title");
l.Name = myField1.Title;
try
{
l.Value = myField1.GetFieldValueAsText(myItem["Title"]);
}
catch
{
l.Value = "";
}
}
if (myList.Fields.ContainsField("Priority"))
{
EntreeListItemDetailNameValue l2 = lGrp.AddListItem<EntreeListItemDetailNameValue>();
SPField myField = myList.Fields.GetField("Priority");
l2.Name = myField.Title;
try
{
l2.Value = myField.GetFieldValueAsText(myItem["Priority"]);
}
catch
{
l2.Value = "";
}
You could use GetItemById()
http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.splistitemcollection.getitembyid.aspx
void list_Click(object sender, EventArgs e) {
int clickedid = 0; //get the id from the clicked item
ShowForm(); //show detail form
DataBind(clickedid); //databind detail form
}
void DataBind(int id) {
SPListItemCollection myItemCollection = showthing; //load the list items, query using SPQuery, or SPList.Items
SPListItem item = myItemCollection.GetItemById(id);
form.Title = item["Title"];
}