コード例 #1
0
        public void RunPPTMacro(string pptFileName, string excelFile, string macro, bool visible)
        {
            //### improve & cleanup code

            // Define Workbooks
            PowerPoint.Application   oPP         = null;
            PowerPoint.Presentations oPresSet    = null;
            PowerPoint._Presentation _activePres = null;
            object oMissing = System.Reflection.Missing.Value;

            string mess = "";

            try
            {
                //open Excel
                //System.Diagnostics.Process excelProc = System.Diagnostics.Process.Start(excelFile);

                Excel2.Application oExcel = new Excel2.Application();
                oExcel.Visible       = false;
                oExcel.DisplayAlerts = false;
                Excel2.Workbooks oBooks = oExcel.Workbooks;
                Excel2._Workbook oBook  = oBooks.Open(excelFile, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing,
                                                      oMissing, oMissing, oMissing, oMissing, oMissing, oMissing);

                FileInfo fi = new FileInfo(pptFileName);

                //string pptTempName = fi.DirectoryName + @"\TEMP.pptm";

                oPP = new PowerPoint.Application();
                oPP.DisplayAlerts = Microsoft.Office.Interop.PowerPoint.PpAlertLevel.ppAlertsNone;
                //oPP.Visible = MsoTriState.msoFalse;

                oPresSet    = oPP.Presentations;
                _activePres = oPresSet.Open(fi.FullName, MsoTriState.msoFalse, MsoTriState.msoFalse, MsoTriState.msoTrue);

                //Object[] oRunArgs = { "'" + fi.Name + "'!M2" };
                Object[] oRunArgs = { "'" + fi.Name + "'!" + macro };
                oPP.GetType().InvokeMember("Run", System.Reflection.BindingFlags.Default | System.Reflection.BindingFlags.InvokeMethod, null,
                                           oPP, oRunArgs);

                //close Excel
                try
                {
                    //excelProc.CloseMainWindow();
                    //excelProc.Close();
                    //excelProc.Dispose();
                    //excelProc.Kill();
                }
                catch (Exception exCloseExcel)
                {
                    //excelProc.Kill();
                }

                //save excel file
                //oBook.Save();

                // Quit Excel and clean up.
                Thread.Sleep(1000);
                oBook.Close(false, oMissing, oMissing);
                System.Runtime.InteropServices.Marshal.ReleaseComObject(oBook);
                oBook = null;
                System.Runtime.InteropServices.Marshal.ReleaseComObject(oBooks);
                oBooks = null;
                oExcel.Quit();
                System.Runtime.InteropServices.Marshal.ReleaseComObject(oExcel);
                oExcel = null;


                mess += "kill excel" + Environment.NewLine;

                //_activePres.Final = false;

                Thread.Sleep(2000);

                _activePres.Save();

                //_activePres.SaveAs(pptTempName);

                mess += "save ppt temp" + Environment.NewLine;

                Thread.Sleep(2000);

                //_activePres.Final = true;

                // Quit PPT and clean up.
                if (_activePres != null)
                {
                    mess += "check activepres = null" + Environment.NewLine;
                    try
                    {
                        _activePres.Close();
                        mess += "activepres close" + Environment.NewLine;
                    }
                    catch (Exception exClosePresentation) { }

                    System.Runtime.InteropServices.Marshal.ReleaseComObject(_activePres);
                    mess       += "release activepres" + Environment.NewLine;
                    _activePres = null;
                    mess       += "activepres = null" + Environment.NewLine;
                    System.Runtime.InteropServices.Marshal.ReleaseComObject(oPresSet);
                    mess    += "release presset" + Environment.NewLine;
                    oPresSet = null;
                    mess    += "presset null" + Environment.NewLine;
                    try
                    {
                        Thread.Sleep(1000);
                        oPP.Quit();
                        mess += "opp quit" + Environment.NewLine;
                    }
                    catch (Exception exQuitPPT) { }

                    System.Runtime.InteropServices.Marshal.ReleaseComObject(oPP);
                    mess += "release opp" + Environment.NewLine;
                    oPP   = null;
                    mess += "opp = null" + Environment.NewLine;
                }

                GC.Collect();
                mess += "garbage" + Environment.NewLine;

                //Kill PPT File
                var processes = from p in Process.GetProcessesByName("POWERPNT") select p;

                foreach (var process in processes)
                {
                    //if (process.MainWindowTitle == "TEMP.pptm")
                    try
                    {
                        mess += "test process null" + Environment.NewLine;
                        if (process != null)
                        {
                            process.Close();
                            Thread.Sleep(1000);
                            mess += "close process" + Environment.NewLine;

                            if (process != null)
                            {
                                process.Kill();
                                mess += "kil process" + Environment.NewLine;
                            }
                        }
                    }
                    catch (Exception exCloseProc) { }
                }

                //rename file & delete temp file
                //if (File.Exists(pptFileName))
                //{
                //    File.Delete(pptFileName);
                //    mess += "delete ppt" + Environment.NewLine;
                //    File.Move(pptTempName, pptFileName);
                //    mess += "move temp" + Environment.NewLine;
                //}

                //if (File.Exists(pptTempName))
                //    File.Delete(pptTempName);
                //mess += "delete temp" + Environment.NewLine;
            }
            catch (Exception ex)
            {
                if (_activePres != null)
                {
                    try
                    {
                        _activePres.Close();
                    }
                    catch (Exception ex2) { }
                    System.Runtime.InteropServices.Marshal.ReleaseComObject(oPresSet);
                    oPresSet = null;
                    System.Runtime.InteropServices.Marshal.ReleaseComObject(_activePres);
                    _activePres = null;
                }

                if (oPP != null)
                {
                    try
                    {
                        oPP.Quit();
                    }
                    catch (Exception ex2) { }
                    System.Runtime.InteropServices.Marshal.ReleaseComObject(oPP);
                    oPP = null;
                }

                log.Error("Error :: RunPPTMacro : " + ex.Message);
                throw ex;
            }
        }
コード例 #2
0
ファイル: TriggerForm.cs プロジェクト: GCRGCR/SSV
        /*      delegate void SetTextCallback(string text);
        private void SetText(string text)
        {
            Debug.Print(System.Reflection.MethodBase.GetCurrentMethod().ReflectedType.Name + "." + System.Reflection.MethodBase.GetCurrentMethod().Name); //---------
            // InvokeRequired required compares the thread ID of the
            // calling thread to the thread ID of the creating thread.
            // If these threads are different, it returns true.
            if (this.lblSTrigger.InvokeRequired)
            {
                SetTextCallback d = new SetTextCallback(SetText);
                this.Invoke(d, new object[] { text });
            }
            else
            {
                this.lblSTrigger.Text = text;
            }
        }                  */
        private void PresentationStart()
        {
            Debug.Print(System.Reflection.MethodBase.GetCurrentMethod().ReflectedType.Name + "." + System.Reflection.MethodBase.GetCurrentMethod().Name); //---------
            try
            {
                objApp = new PowerPoint.Application();
                objApp.Visible = Microsoft.Office.Core.MsoTriState.msoTrue;
                objPresSet = objApp.Presentations;
                objPres = objPresSet.Open(stSettings.strPPath, Microsoft.Office.Core.MsoTriState.msoFalse, // MsoTriState ReadOnly,
                Microsoft.Office.Core.MsoTriState.msoFalse, Microsoft.Office.Core.MsoTriState.msoTrue);    //MsoTriState Untitled,MsoTriState WithWindow
            }
            catch (Exception e)
            {
                this.Activate();
                MessageBox.Show(e.Message);
                tsmPresentationStop_Click(null, null);
                return;
            }

            //objSlides = objPres.Slides;

            //Run the Slide show
            objSSS = objPres.SlideShowSettings;
            objSSS.ShowType = Microsoft.Office.Interop.PowerPoint.PpSlideShowType.ppShowTypeSpeaker;
            objSSS.LoopUntilStopped = Microsoft.Office.Core.MsoTriState.msoTrue;

            if (stSettings.bytRunMacro == 0 || stSettings.strPPMacroName=="")
            {
                objSSS.Run();
            }
            else
            {
                object[] oRunArgs = new Object[] { "'" + objPres.Name + "'!" + stSettings.strPPMacroName, stSettings.strMParam1, stSettings.strMParam2, stSettings.strMParam3 };
                try
                {
                    objApp.GetType().InvokeMember("Run", System.Reflection.BindingFlags.Default | System.Reflection.BindingFlags.InvokeMethod, null, objApp, oRunArgs);
                    originalPWidth = objPres.SlideShowWindow.Width;
                }
                catch (System.Runtime.InteropServices.COMException e)
                {
                    MessageBox.Show(e.Message + "\n" + "\n" + e.Source + "\n" + e.StackTrace);
                    return;
                }
                catch (Exception e)
                {
                    this.Activate();
                    MessageBox.Show(e.Message + "\n" + "\n" + e.InnerException.Source + "\nn" + e.InnerException.Message);
                    return;
                }
            }

            //handle
            //IntPtr hwnd = new IntPtr(objPres.SlideShowWindow.HWND);
            //WindowWrapper handleWrapper = new WindowWrapper(hwnd);
            //SetParent(handleWrapper.Handle, this.Handle);
            //this.Visible = true;
            if (stSettings.bytFullScreenPres==0)
            {
                SlideShowSize();
                //WinApi.HideTray();
                Taskbar.Hide();
            }
        }