private void ThisAddIn_Startup(object sender, System.EventArgs e) { try { if (this.Application.Caption.IndexOf("LGSL+") > -1) { var width = 0; //USERCONTROL DE GAUCHE AVEC TABLE LAYOUT PANNEL myUserControl1 = new InterfaceVert(); //set Width du au UserControl Docking width = myUserControl1.Width; myCustomTaskPane = this.CustomTaskPanes.Add(myUserControl1, "XLApp"); //set Width du au UserControl Docking myCustomTaskPane.Width = width; myCustomTaskPane.DockPosition = Office.MsoCTPDockPosition.msoCTPDockPositionLeft; myCustomTaskPane.DockPositionRestrict = Office.MsoCTPDockPositionRestrict.msoCTPDockPositionRestrictNoChange; myCustomTaskPane.VisibleChanged += new EventHandler(myCustomTaskPane_VisibleChanged); //////myCustomTaskPane.Visible = true; Globals.Ribbons.ManageTaskPaneRibbon.toggleButton1.Checked = myCustomTaskPane.Visible; if (this.Application.Caption.IndexOf("LGSL+") > -1) { this.Application.Run("ShowOrHideUserControlCheckBoxInCSharp"); //met myUserControl1 visible } //FIN USERCONTROL DE GAUCHE AVEC TABLE LAYOUT PANNEL //TEST NEW USERCONTROL InterfaceData : myUserControlInterfaceData = new VerifProjet(); width = myUserControlInterfaceData.Width; myCustomTaskPaneInterfaceData = this.CustomTaskPanes.Add(myUserControlInterfaceData, "Data applicatif"); //set Width du au UserControl Docking myCustomTaskPaneInterfaceData.Width = width; myCustomTaskPaneInterfaceData.DockPosition = Office.MsoCTPDockPosition.msoCTPDockPositionLeft; myCustomTaskPaneInterfaceData.DockPositionRestrict = Office.MsoCTPDockPositionRestrict.msoCTPDockPositionRestrictNoChange; myCustomTaskPaneInterfaceData.VisibleChanged += new EventHandler(myCustomTaskPaneInterfaceData_VisibleChanged); //myCustomTaskPaneVerificationProjetTemp.VisibleChanged += // new EventHandler(myCustomTaskPaneVerificationProjetTemp_VisibleChanged); //FIN TEST //TEST InteractSousTraitant : myUserControlInterfaceSousTrait = new InteractSousTrait(); width = myUserControlInterfaceSousTrait.Width; myCustomTaskPaneInterfaceSousTrait = this.CustomTaskPanes.Add(myUserControlInterfaceSousTrait, " "); //set Width du au UserControl Docking myCustomTaskPaneInterfaceSousTrait.Width = width; myCustomTaskPaneInterfaceSousTrait.DockPosition = Office.MsoCTPDockPosition.msoCTPDockPositionLeft; myCustomTaskPaneInterfaceSousTrait.DockPositionRestrict = Office.MsoCTPDockPositionRestrict.msoCTPDockPositionRestrictNoChange; myCustomTaskPaneInterfaceSousTrait.VisibleChanged += new EventHandler(myCustomTaskPaneInterfaceSousTrait_VisibleChanged); //FIN TEST //TEST NEW USERCONTROL VERIFPROJET : myUserControlVerificationProjet = new VerifProjet(); width = myUserControlVerificationProjet.Width; myCustomTaskPaneVerificationProjet = this.CustomTaskPanes.Add(myUserControlVerificationProjet, "Vérification rapide"); //set Width du au UserControl Docking myCustomTaskPaneVerificationProjet.Width = width; myCustomTaskPaneVerificationProjet.DockPosition = Office.MsoCTPDockPosition.msoCTPDockPositionRight; myCustomTaskPaneVerificationProjet.DockPositionRestrict = Office.MsoCTPDockPositionRestrict.msoCTPDockPositionRestrictNoChange; myCustomTaskPaneVerificationProjet.VisibleChanged += new EventHandler(myCustomTaskPaneVerificationProjet_VisibleChanged); //FIN TEST //// TEST USERCONTROL WPF myUserControlWPF = new UserControl2(); // UserContron2.cs width = myUserControlWPF.Width; int height = myUserControlWPF.Height; myCustomTaskPaneWPFEstImp = this.CustomTaskPanes.Add(myUserControlWPF, "XLApp - Estimation imposition"); myCustomTaskPaneWPFEstImp.DockPosition = Office.MsoCTPDockPosition.msoCTPDockPositionFloating; //myUserControlWPF.SizeChanged -= UserControl2_SizeChanged; myCustomTaskPaneWPFEstImp.Height = height + 45; myCustomTaskPaneWPFEstImp.Width = width + 15; myCustomTaskPaneWPFEstImp.DockPositionRestrict = Office.MsoCTPDockPositionRestrict.msoCTPDockPositionRestrictNoChange; myCustomTaskPaneWPFEstImp.Control.SizeChanged += new EventHandler(Control_SizeChanged); //myCustomTaskPaneWPF.Control.MinimumSize = new System.Drawing.Size(100, 100); //myCustomTaskPaneWPF.Control.MaximumSize = new System.Drawing.Size(myCustomTaskPaneWPF.Width, myCustomTaskPaneWPF.Height); //SetCustomPanePositionWhenFloating(myCustomTaskPaneWPF, (int)this.Application.Left, (int)this.Application.Top); //myCustomTaskPaneWPF.DockPositionRestrict = Office.MsoCTPDockPositionRestrict.msoCTPDockPositionRestrictNoChange; //myCustomTaskPaneWPF.VisibleChanged += // new EventHandler(myCustomTaskPaneWPF_VisibleChanged); //FIN TEST //pour image custom: defineCustomCommandBarImages(); } else { AddInUtilities.UnConnectAddin(); //AddInUtilities.InitiateFirstLaunch(); } } catch { } }
//public bool saisieTabs_setVisible(object sender, EventArgs e) //{ // return false; //} public void toggleButton1_Click(object sender, RibbonControlEventArgs e) { bool isMyApp; if (AddInUtilities.GetIsAddIn(out isMyApp)) //UNCHECKButton { //ShowXLBackStageView: //Globals.Ribbons.CustomRibbon.Tabs[Your tab id].RibbonUI.ActivateTab(""); //Globals.Ribbons.ManageTaskPaneRibbon.RibbonUI.ActivateTab("FileTab"); //Excel 2010 or higher: Build in way to activate tab if (Globals.Ribbons.ManageTaskPaneRibbon.RibbonUI != null) { toggleButton1.Checked = false; System.Windows.Forms.SendKeys.Send("%{f}%"); //va aller dans le backstage view de Excel return; //Globals.Ribbons.ManageTaskPaneRibbon.RibbonUI.ActivateTab("TabHome"); } } if (!isMyApp) { //Globals.Ribbons.ManageTaskPaneRibbon.tab2.Visible = false; // messagebox pour avertir l'utilisateur ou fermer la visibilité... string userProfile = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile); if (!System.IO.File.Exists(userProfile + "\\Desktop\\" + "XLApp" + ".lnk")) { // code si aucun raccourci sur le bureau : string msgErr = ""; // vérifier si c'est au moins excel 2013 et 64 bit (version 15) int noVers = int.Parse(Globals.ThisAddIn.Application.Version.ToString().Split('.')[0]); //bool Is64bit = Environment.GetEnvironmentVariable("ProgramW6432").Length > 0 if (noVers < 15) { msgErr = "Pour finaliser l'installation, la version d'Excel 2013 ou plus récente est requise, option 64 bit."; } //vérifier si l'accès à la sécurité est activé avant de poursuivre, sinon, informez l'utilisateur comment le faire. try { var VDP = Globals.ThisAddIn.Application.ActiveWorkbook.VBProject; if (VDP != null) { VDP = null; } } catch { if (msgErr != "") { msgErr += "\n\nEnsuite, vous devez configurer une option de sécurité dans Excel en suivant cette procédure :\n\nFichier > Options > Paramètres du Centre de gestion de la confidentialité > Paramètres des macros > Cocher \"Accès approuv au modèle d'objet du projet VBA\""; } else { msgErr += "Pour finaliser l'installation, configurer une option de sécurité en suivant cette procédure :\n\nFichier > Options > Paramètres du Centre de gestion de la confidentialité > Paramètres des macros > Cocher \"Accès approuv au modèle d'objet du projet VBA\""; } } if (msgErr != "") { System.Windows.MessageBox.Show(msgErr, "XLApp"); toggleButton1.Checked = false; return; } AddInUtilities.InitiateFirstLaunch(); // if (AddInUtilities.InitiateFirstLaunch()) { // updateDeskTopShortCutDescription("XLApp"); // System.Windows.MessageBox.Show("Pour utiliser l'application, veuillez lancer le raccourci par votre bureau ou par le menu démarrer.", "XLApp"); // } //Globals.Ribbons.ManageTaskPaneRibbon.tab2.Visible = false; //AddInUtilities.UnConnectAddin(); } else { DialogResult result1 = MessageBox.Show("Lancer l'application?", "XLApp", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1); if (result1 == DialogResult.Yes) { Globals.Ribbons.ManageTaskPaneRibbon.tab2.Visible = false; AddInUtilities.UnConnectAddin(); AddInUtilities.LaunchApp(); } else { Globals.Ribbons.ManageTaskPaneRibbon.tab2.Visible = false; AddInUtilities.UnConnectAddin(); } } return; } Globals.ThisAddIn.TaskPaneInterfaceVert.Visible = ((RibbonToggleButton)sender).Checked; //Globals.ThisAddIn.Application.Run("resizeWindow"); //if (!Globals.Application.ActiveWorkbook.IsAddin) // Globals.ThisAddIn.TaskPane.Visible = ((RibbonToggleButton)sender).Checked; //else // Globals.Application.Run("SheetList_RDB"); }
internal static void InitiateFirstLaunch() { // Validation1 : vérifier si les raccourcis sont sur le bureau : string userProfile = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile); if (File.Exists(userProfile + "\\Desktop\\" + "XLApp" + ".lnk")) { return; } DialogResult result1 = MessageBox.Show("Démarrer le lancement d'initialisation de l'application?", "XLApp", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1); if (result1 == DialogResult.Yes) { Globals.ThisAddIn.Application.Cursor = Microsoft.Office.Interop.Excel.XlMousePointer.xlWait; // Avant le démarrement du lancement d'initialisation de l'application, // on pourrait activer la clé de registre qui coche et permet donc l'accès au trust model VBA // https://blogs.msdn.microsoft.com/cristib/2012/02/29/vba-how-to-programmatically-enable-access-to-the-vba-object-model-using-macros/ // Cela peut se faire en ajoutant les composantes VB au document excel actif et en runnant la macro qui quittera la scéance // active d'Excel, ou en codant directement ds c#, mais la scéance doit être fermer. //en attendant, la demande a été fait manuellement à l'utilisateur dans la méthod qui appelle le InitiateFirstLaunch() List <int> oldExcelIDs = new List <int>(); Process[] excelProcesses = Process.GetProcessesByName("Excel"); foreach (Process pro in excelProcesses) { oldExcelIDs.Add(pro.Id); } Excel.Application xlApp; Excel.Workbook xlWorkBook; object misValue = System.Reflection.Missing.Value; xlApp = new Excel.Application(); // http://stackoverflow.com/questions/1600502/programmatically-configuring-ms-words-trust-center-settings-using-c-sharp // a very simple way of opening an .xls file containing macros, without messing with the registry or Excel's trust settings. : xlApp.FileValidation = Microsoft.Office.Core.MsoFileValidationMode.msoFileValidationSkip; // fin a very simple way :) xlApp.AutomationSecurity = Microsoft.Office.Core.MsoAutomationSecurity.msoAutomationSecurityLow; //26 mai 2017 https://msdn.microsoft.com/en-us/library/office/ff194819.aspx //Get the assembly information System.Reflection.Assembly assemblyInfo = System.Reflection.Assembly.GetExecutingAssembly(); //Location is where the assembly is run from string assemblyLocation = assemblyInfo.Location; //CodeBase is the location of the ClickOnce deployment files Uri uriCodeBase = new Uri(assemblyInfo.CodeBase); string ClickOnceLocation = Path.GetDirectoryName(uriCodeBase.LocalPath.ToString()); //xlApp.Caption = ""; // readonly=true xlWorkBook = xlApp.Workbooks.Open(ClickOnceLocation + "\\XLApp.dll", 0, true, 5, "False", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "", false, false, 0, false, 1, 0); xlApp.AutomationSecurity = Microsoft.Office.Core.MsoAutomationSecurity.msoAutomationSecurityLow; //26 mai 2017 https://msdn.microsoft.com/en-us/library/office/ff194819.aspx xlApp.Caption = ""; //xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1); //xlApp.Visible = true //xlWorkBook = null; //xlApp = null; // updateDeskTopShortCutDescription("XLApp"); System.Windows.MessageBox.Show("Pour utiliser l'application, veuillez lancer le raccourci par votre bureau ou par le menu démarrer.", "XLApp"); Globals.Ribbons.ManageTaskPaneRibbon.tab2.Visible = false; AddInUtilities.UnConnectAddin(); Globals.ThisAddIn.Application.Cursor = Microsoft.Office.Interop.Excel.XlMousePointer.xlDefault; xlWorkBook = null; xlApp = null; // https://stackoverflow.com/questions/17777545/closing-excel-application-process-in-c-sharp-after-data-access //--------Take the list of excel processes again and compare the IDs, if the Id is not in the old list is the one we just created, let's kill it!------ excelProcesses = Process.GetProcessesByName("Excel"); foreach (Process proc in excelProcesses) { if (!oldExcelIDs.Contains(proc.Id)) { try { proc.Kill(); } catch { } } } return; } //si l'utilisateur ne veut pas démarrer l'app initialisation: Globals.Ribbons.ManageTaskPaneRibbon.tab2.Visible = false; AddInUtilities.UnConnectAddin(); return; }