Created
December 9, 2011 23:51
-
-
Save deenseth/1453884 to your computer and use it in GitHub Desktop.
Get active application and url of Word, Excel, Powerpoint, and PDF
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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