public static void SearchFileAttachments(Uri file)
{
object missing = Type.Missing;
object fileName = file.ToString();
object VerbIndex = Microsoft.Office.Interop.Word.WdOLEVerb.wdOLEVerbOpen;
Microsoft.Office.Interop.Word.Application word = new Microsoft.Office.Interop.Word.Application();
Microsoft.Office.Interop.Word.Document docs = word.Documents.Open(ref fileName, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);
try
{
docs.Activate();
foreach (Microsoft.Office.Interop.Word.InlineShape inlineShape in docs.InlineShapes)
{
if (inlineShape.OLEFormat.ProgID != null)
{
switch (inlineShape.OLEFormat.ProgID)
{
case "PowerPoint.Show.8":
Microsoft.Office.Interop.PowerPoint.Application powerpoint = new Microsoft.Office.Interop.PowerPoint.Application();
try
{
powerpoint.WindowState =
Microsoft.Office.Interop.PowerPoint.PpWindowState.ppWindowNormal;
inlineShape.OLEFormat.DoVerb(ref VerbIndex);
powerpoint = Marshal.GetActiveObject("PowerPoint.Application") as Microsoft.Office.Interop.PowerPoint.Application;
if (powerpoint != null)
{
Guid guid = Guid.NewGuid();
string presentationName = guid + ".ppt";
powerpoint.ActivePresentation.SaveAs(presentationName, Microsoft.Office.Interop.PowerPoint.PpSaveAsFileType.ppSaveAsPresentation, Microsoft.Office.Core.MsoTriState.msoTrue);
}
}
catch (Exception ex) { //exception code here }
finally
{
if (powerpoint != null)
{
powerpoint.ActivePresentation.Close();
powerpoint.Quit();
}
}
break;
case "Excel.Sheet.8":
Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application();
try
{
excel.Visible = false;
excel.ScreenUpdating = false;
excel.Left = -1000;
inlineShape.OLEFormat.DoVerb(ref VerbIndex);
excel = Marshal.GetActiveObject("Excel.Application") as Microsoft.Office.Interop.Excel.Application;
if (excel != null)
{
Guid guid = Guid.NewGuid();
object workBookName = guid + ".xls";
excel.ActiveWorkbook.SaveAs(workBookName, missing, missing, missing, missing,
missing, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlNoChange,
missing, missing, missing, missing, missing);
}
}
catch (Exception ex) { //exception code here }
finally
{
if (excel != null)
{
excel.Workbooks.Close();
excel.Quit();
}
}
break;
case "Word.Document.8":
Microsoft.Office.Interop.Word.Application wordDocument = new Microsoft.Office.Interop.Word.Application();
try
{
wordDocument.Visible = false;
wordDocument.ScreenUpdating = false;
wordDocument.Left = -1000;
inlineShape.OLEFormat.DoVerb(ref VerbIndex);
wordDocument = Marshal.GetActiveObject("Word.Application") as Microsoft.Office.Interop.Word.Application;
if (wordDocument != null)
{
Guid guid = Guid.NewGuid();
object wordDocumentName = guid + ".doc";
wordDocument.ActiveDocument.SaveAs(ref wordDocumentName, ref missing, ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing, ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing, ref missing, ref missing);
}
}
catch (Exception ex) { //exeption code here }
finally
{
if (wordDocument != null)
{
object saveChanges = Microsoft.Office.Interop.Word.WdSaveOptions.wdDoNotSaveChanges;
object originalFormat = Microsoft.Office.Interop.Word.WdOriginalFormat.wdWordDocument;
object routeDocument = true;
wordDocument.ActiveDocument.Close(ref saveChanges, ref originalFormat, ref routeDocument);
//wordDocument.Quit(ref saveChanges, ref originalFormat, ref routeDocument);
}
}
break;
default:
break;
}
}
}
}
catch (Exception ex) { //exception code here }
finally
{
object saveChanges = Microsoft.Office.Interop.Word.WdSaveOptions.wdDoNotSaveChanges;
object originalFormat = Microsoft.Office.Interop.Word.WdOriginalFormat.wdWordDocument;
object routeDocument = true;
docs.Close(ref saveChanges, ref originalFormat, ref routeDocument);
word.Quit(ref saveChanges, ref originalFormat, ref routeDocument);
}
}