Skip to content

Instantly share code, notes, and snippets.

@deenseth
Created December 9, 2011 23:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save deenseth/1453884 to your computer and use it in GitHub Desktop.
Save deenseth/1453884 to your computer and use it in GitHub Desktop.
Get active application and url of Word, Excel, Powerpoint, and PDF
private InfoItem GetActiveItemFromROT(IntPtr handle)
{
activeItem = new InfoItem();
String activeTitle = GetActiveWindowText(handle);
String title;
IntPtr numFetched = IntPtr.Zero;
IRunningObjectTable runningObjectTable;
IEnumMoniker monikerEnumerator;
IMoniker[] monikers = new IMoniker[1];
GetRunningObjectTable(0, out runningObjectTable);
runningObjectTable.EnumRunning(out monikerEnumerator);
monikerEnumerator.Reset();
while (monikerEnumerator.Next(1, monikers, numFetched) == 0)
{
IBindCtx ctx;
CreateBindCtx(0, out ctx);
string runningObjectName;
monikers[0].GetDisplayName(ctx, null, out runningObjectName);
object runningObjectVal;
runningObjectTable.GetObject(monikers[0], out runningObjectVal);
try
{
string ext = Path.GetExtension(runningObjectName).ToLower();
if (ext == ".pdf" ||
ext == ".docx" || ext == ".doc" ||
ext == ".xlsx" || ext == ".xls" ||
ext == ".pptx" || ext == ".ppt")
{
title = Path.GetFileNameWithoutExtension(runningObjectName);
if (activeTitle.IndexOf(title) != -1)
{
activeItem.Title = Path.GetFileNameWithoutExtension(runningObjectName);
activeItem.Uri = runningObjectName;
activeItem.Type = InfoItemType.File;
return activeItem;
}
}
}
catch (Exception)
{
continue;
}
}
return null;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment