protected void GenerarArchivo(oPlantilla _Template, string _DestinationPath, string _Tipo) { string _Path = Configuration.Path; // el nombre de una Key debe incluir un root valido. const string userRoot = "HKEY_CURRENT_USER"; const string subkey = "Software\\Microsoft\\Office\\Excel\\Addins\\SAT.Dictamenes.SIPRED.Client"; const string keyName = userRoot + "\\" + subkey; object addInName = "SAT.Dictamenes.SIPRED.Client"; File.Copy($"{_Path}\\templates\\{_Template.Nombre}", _DestinationPath); Registry.SetValue(keyName, "LoadBehavior", 0); Globals.ThisAddIn.Application.COMAddIns.Item(ref addInName).Connect = false; Globals.ThisAddIn.Application.Visible = true; try { Globals.ThisAddIn.Application.Workbooks.Open(_DestinationPath); } catch (Exception ex) { MessageBox.Show($"Error al abrir el archivo [{_DestinationPath}]: {ex.Message.ToString()}", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } //Libro Actual de Excel. Excel.Worksheet xlSht; Excel.Workbook wb = Globals.ThisAddIn.Application.ActiveWorkbook; int count = wb.Worksheets.Count; xlSht = (Excel.Worksheet)wb.Worksheets.Add(Type.Missing, wb.Worksheets[count], Type.Missing, Type.Missing); xlSht.Name = "SIPRED"; wb.Save(); this.TopMost = false; this.Enabled = false; this.Hide(); FormulasComprobaciones _Formulas = new FormulasComprobaciones(); _Formulas._Form = this; _Formulas._Template = _Template; _Formulas._Tipo = _Tipo; _Formulas._formulas = true; _Formulas._Open = false; _Formulas.ShowDialog(); }
private void btnTransferir_Click(object sender, RibbonControlEventArgs e) { if (_Connection) { string _Path = ""; const string userRoot = "HKEY_CURRENT_USER"; const string subkey = "Software\\Microsoft\\Office\\Excel\\Addins\\SAT.Dictamenes.SIPRED.Client"; const string keyName = userRoot + "\\" + subkey; object addInName = "SAT.Dictamenes.SIPRED.Client"; Registry.SetValue(keyName, "LoadBehavior", 0); Globals.ThisAddIn.Application.COMAddIns.Item(ref addInName).Connect = false; Globals.ThisAddIn.Application.Visible = true; Excel.Workbook wb = Globals.ThisAddIn.Application.ActiveWorkbook; if (wb == null) { MessageBox.Show("Archivo no válido, favor de generar el archivo mediante el AddIn D.SAT", "Información Incorrecta", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } try { _Path = Globals.ThisAddIn.Application.ActiveWorkbook.FullName; } catch (Exception ex) { } //wb.Close(); FileInfo _Excel = new FileInfo(_Path == null || _Path == "" ? "C:\\ArchivoNoValido.xlsx" : _Path); if (_Excel.Extension != ".xlsm") { MessageBox.Show("Archivo no válido, favor de generar el archivo mediante el AddIn D.SAT", "Información Incorrecta", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } //try //{ // Globals.ThisAddIn.Application.Workbooks.Open(_Path); //} //catch (Exception ex) //{ } //Libro Actual de Excel. Excel.Worksheet xlSht; wb = Globals.ThisAddIn.Application.ActiveWorkbook; int count = wb.Worksheets.Count; bool _existeS = false; for (int _wCount = 1; _wCount <= count; _wCount++) { string _sName = wb.Worksheets[_wCount].Name; if (_sName == "SIPRED") { _existeS = true; } } if (!_existeS) { xlSht = (Excel.Worksheet)wb.Worksheets.Add(Type.Missing, wb.Worksheets[count], Type.Missing, Type.Missing); xlSht.Name = "SIPRED"; } wb.Save(); //btnOpen_Click(sender, e); btnSave.Visible = false; FormulasComprobaciones form = new FormulasComprobaciones(); form._formulas = false; form._Open = false; form.TopMost = false; form.ShowDialog(); } else { MessageBox.Show(_Message, _Title, MessageBoxButtons.OK, MessageBoxIcon.Error); } }