Advertisement
Guest User

Untitled

a guest
Dec 18th, 2012
2,989
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.55 KB | None | 0 0
  1. var excel = new Microsoft.Office.Interop.Excel.Application();
  2. excel.Workbooks.Open(filePath);
  3. excel.Run("RefreshAllStaticData");
  4.  
  5. var excel = new Microsoft.Office.Interop.Excel.Application();
  6. var addIn = ex.AddIns.Add(filename);
  7. addIn.Installed = true;
  8. Console.WriteLine(addIn.IsOpen);
  9.  
  10. //From http://blogs.officezealot.com/whitechapel/archive/2005/04/10/4514.aspx
  11. public class ExcelInteropService
  12. {
  13. private const string EXCEL_CLASS_NAME = "EXCEL7";
  14.  
  15. private const uint DW_OBJECTID = 0xFFFFFFF0;
  16.  
  17. private static Guid rrid = new Guid("{00020400-0000-0000-C000-000000000046}");
  18.  
  19. public delegate bool EnumChildCallback(int hwnd, ref int lParam);
  20.  
  21. [DllImport("Oleacc.dll")]
  22. public static extern int AccessibleObjectFromWindow(int hwnd, uint dwObjectID, byte[] riid, ref Microsoft.Office.Interop.Excel.Window ptr);
  23.  
  24. [DllImport("User32.dll")]
  25. public static extern bool EnumChildWindows(int hWndParent, EnumChildCallback lpEnumFunc, ref int lParam);
  26.  
  27. [DllImport("User32.dll")]
  28. public static extern int GetClassName(int hWnd, StringBuilder lpClassName, int nMaxCount);
  29.  
  30. public static Microsoft.Office.Interop.Excel.Application GetExcelInterop(int? processId = null)
  31. {
  32. var p = processId.HasValue ? Process.GetProcessById(processId.Value) : Process.Start("excel.exe");
  33. try
  34. {
  35. return new ExcelInteropService().SearchExcelInterop(p);
  36. }
  37. catch (Exception)
  38. {
  39. Debug.Assert(p != null, "p != null");
  40. return GetExcelInterop(p.Id);
  41. }
  42. }
  43.  
  44. private bool EnumChildFunc(int hwndChild, ref int lParam)
  45. {
  46. var buf = new StringBuilder(128);
  47. GetClassName(hwndChild, buf, 128);
  48. if (buf.ToString() == EXCEL_CLASS_NAME) { lParam = hwndChild; return false; }
  49. return true;
  50. }
  51.  
  52. private Microsoft.Office.Interop.Excel.Application SearchExcelInterop(Process p)
  53. {
  54. Microsoft.Office.Interop.Excel.Window ptr = null;
  55. int hwnd = 0;
  56.  
  57. int hWndParent = (int)p.MainWindowHandle;
  58. if (hWndParent == 0) throw new ExcelMainWindowNotFoundException();
  59.  
  60. EnumChildWindows(hWndParent, EnumChildFunc, ref hwnd);
  61. if (hwnd == 0) throw new ExcelChildWindowNotFoundException();
  62.  
  63. int hr = AccessibleObjectFromWindow(hwnd, DW_OBJECTID, rrid.ToByteArray(), ref ptr);
  64. if (hr < 0) throw new AccessibleObjectNotFoundException();
  65.  
  66. return ptr.Application;
  67. }
  68. }
  69.  
  70. var excel = ExcelInteropService.GetExcelInterop();
  71. excel.Workbooks.Open(filename);
  72. excel.Run("RefreshAllStaticData");
  73. // works "like a charm"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement