Beispiel #1
0
        public void RunExample()
        {
            // start excel and turn Application msg boxes
            Excel.Application excelApplication = COMObject.Create <Excel.Application>();
            excelApplication.DisplayAlerts = false;

            // create a utils instance, not need for but helpful to keep the lines of code low
            CommonUtils utils = new CommonUtils(excelApplication);

            // add a new workbook
            Excel.Workbook  workBook  = excelApplication.Workbooks.Add();
            Excel.Worksheet workSheet = (Excel.Worksheet)workBook.Worksheets[1];

            // the given thread culture in all NetOffice calls are stored in NetOffice.Settings.
            // you can change the culture of course. Default is en-us.
            CultureInfo cultureInfo = NetOffice.Settings.Default.ThreadCulture;
            string      Pattern1    = string.Format("0{0}00", cultureInfo.NumberFormat.CurrencyDecimalSeparator);
            string      Pattern2    = string.Format("#{1}##0{0}00", cultureInfo.NumberFormat.CurrencyDecimalSeparator, cultureInfo.NumberFormat.CurrencyGroupSeparator);

            workSheet.Range("A1").Value = "Type";
            workSheet.Range("B1").Value = "Value";
            workSheet.Range("C1").Value = "Formatted " + Pattern1;
            workSheet.Range("D1").Value = "Formatted " + Pattern2;

            int integerValue = 532234;

            workSheet.Range("A3").Value        = "Integer";
            workSheet.Range("B3").Value        = integerValue;
            workSheet.Range("C3").Value        = integerValue;
            workSheet.Range("C3").NumberFormat = Pattern1;
            workSheet.Range("D3").Value        = integerValue;
            workSheet.Range("D3").NumberFormat = Pattern2;

            double doubleValue = 23172.64;

            workSheet.Range("A4").Value        = "double";
            workSheet.Range("B4").Value        = doubleValue;
            workSheet.Range("C4").Value        = doubleValue;
            workSheet.Range("C4").NumberFormat = Pattern1;
            workSheet.Range("D4").Value        = doubleValue;
            workSheet.Range("D4").NumberFormat = Pattern2;

            float floatValue = 84345.9132f;

            workSheet.Range("A5").Value        = "float";
            workSheet.Range("B5").Value        = floatValue;
            workSheet.Range("C5").Value        = floatValue;
            workSheet.Range("C5").NumberFormat = Pattern1;
            workSheet.Range("D5").Value        = floatValue;
            workSheet.Range("D5").NumberFormat = Pattern2;

            Decimal decimalValue = 7251231.313367m;

            workSheet.Range("A6").Value        = "Decimal";
            workSheet.Range("B6").Value        = decimalValue;
            workSheet.Range("C6").Value        = decimalValue;
            workSheet.Range("C6").NumberFormat = Pattern1;
            workSheet.Range("D6").Value        = decimalValue;
            workSheet.Range("D6").NumberFormat = Pattern2;

            workSheet.Range("A9").Value  = "DateTime";
            workSheet.Range("B10").Value = NetOffice.Settings.Default.ThreadCulture.DateTimeFormat.FullDateTimePattern;
            workSheet.Range("C10").Value = NetOffice.Settings.Default.ThreadCulture.DateTimeFormat.LongDatePattern;
            workSheet.Range("D10").Value = NetOffice.Settings.Default.ThreadCulture.DateTimeFormat.ShortDatePattern;
            workSheet.Range("E10").Value = NetOffice.Settings.Default.ThreadCulture.DateTimeFormat.LongTimePattern;
            workSheet.Range("F10").Value = NetOffice.Settings.Default.ThreadCulture.DateTimeFormat.ShortTimePattern;

            // DateTime
            DateTime dateTimeValue = DateTime.Now;

            workSheet.Range("B11").Value        = dateTimeValue;
            workSheet.Range("B11").NumberFormat = NetOffice.Settings.Default.ThreadCulture.DateTimeFormat.FullDateTimePattern;

            workSheet.Range("C11").Value        = dateTimeValue;
            workSheet.Range("C11").NumberFormat = NetOffice.Settings.Default.ThreadCulture.DateTimeFormat.LongDatePattern;

            workSheet.Range("D11").Value        = dateTimeValue;
            workSheet.Range("D11").NumberFormat = NetOffice.Settings.Default.ThreadCulture.DateTimeFormat.ShortDatePattern;

            workSheet.Range("E11").Value        = dateTimeValue;
            workSheet.Range("E11").NumberFormat = NetOffice.Settings.Default.ThreadCulture.DateTimeFormat.LongTimePattern;

            workSheet.Range("F11").Value        = dateTimeValue;
            workSheet.Range("F11").NumberFormat = NetOffice.Settings.Default.ThreadCulture.DateTimeFormat.ShortTimePattern;

            // string
            workSheet.Range("A14").Value        = "String";
            workSheet.Range("B14").Value        = "This is a sample String";
            workSheet.Range("B14").NumberFormat = "@";

            // number as string
            workSheet.Range("B15").Value        = "513";
            workSheet.Range("B15").NumberFormat = "@";

            // set colums
            workSheet.Columns[1].AutoFit();
            workSheet.Columns[2].AutoFit();
            workSheet.Columns[3].AutoFit();
            workSheet.Columns[4].AutoFit();

            // save the book
            string workbookFile = utils.File.Combine(HostApplication.RootDirectory, "Example03", DocumentFormat.Normal);

            workBook.SaveAs(workbookFile);

            // close excel and dispose reference
            excelApplication.Quit();
            excelApplication.Dispose();

            // show end dialog
            HostApplication.ShowFinishDialog(null, workbookFile);
        }
Beispiel #2
0
        private void buttonStartExample_Click(object sender, EventArgs e)
        {
            // start excel and turn off msg boxes
            Excel.Application excelApplication = COMObject.Create <Excel.Application>();
            excelApplication.DisplayAlerts = false;
            excelApplication.Visible       = true;

            excelApplication.Settings.ExceptionMessageBehavior    = NetOffice.ExceptionMessageHandling.DiagnosticsAndInnerMessage;
            excelApplication.Settings.ExceptionDiagnosticsMessage = "Failed to proceed {CallInstance}={CallType}=>{Name}.{NlApplicationVersions}{NewLine}";

            // add a new workbook
            Excel.Workbook  workBook  = excelApplication.Workbooks.Add();
            Excel.Worksheet workSheet = (Excel.Worksheet)workBook.Worksheets[1];

            // show selected window and display user clicks ok or cancel
            bool        returnValue       = false;
            RadioButton radioSelectButton = GetSelectedRadioButton();

            switch (radioSelectButton.Text)
            {
            case "xlDialogAddinManager":

                returnValue = excelApplication.Dialogs[XlBuiltInDialog.xlDialogAddinManager].Show();
                break;

            case "xlDialogFont":

                returnValue = excelApplication.Dialogs[XlBuiltInDialog.xlDialogFont].Show();
                break;

            case "xlDialogEditColor":

                returnValue = excelApplication.Dialogs[XlBuiltInDialog.xlDialogEditColor].Show();
                break;

            case "xlDialogGallery3dBar":

                returnValue = excelApplication.Dialogs[XlBuiltInDialog.xlDialogGallery3dBar].Show();
                break;

            case "xlDialogSearch":

                returnValue = excelApplication.Dialogs[XlBuiltInDialog.xlDialogSearch].Show();
                break;

            case "xlDialogPrinterSetup":

                returnValue = excelApplication.Dialogs[XlBuiltInDialog.xlDialogPrinterSetup].Show();
                break;

            case "xlDialogFormatNumber":

                returnValue = excelApplication.Dialogs[XlBuiltInDialog.xlDialogFormatNumber].Show();
                break;

            case "xlDialogApplyStyle":

                returnValue = excelApplication.Dialogs[XlBuiltInDialog.xlDialogApplyStyle].Show();
                break;

            default:
                throw (new ArgumentOutOfRangeException("Unkown dialog selected."));
            }

            string message = string.Format("The dialog returns {0}.", returnValue);

            MessageBox.Show(this, message, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Information);

            // close excel and dispose reference
            excelApplication.Quit();
            excelApplication.Dispose();
        }
Beispiel #3
0
        public TestResult DoTest()
        {
            Excel.Application application = null;
            DateTime          startTime   = DateTime.Now;

            try
            {
                // start excel and turn off msg boxes
                application = COMObject.Create <Excel.Application>(COMObjectCreateOptions.CreateNewCore);
                application.DisplayAlerts = false;
                application.Visible       = true;

                application.NewWorkbookEvent         += new Excel.Application_NewWorkbookEventHandler(ExcelApplication_NewWorkbook);
                application.WorkbookBeforeCloseEvent += new Excel.Application_WorkbookBeforeCloseEventHandler(ExcelApplication_WorkbookBeforeClose);
                application.WorkbookActivateEvent    += new Excel.Application_WorkbookActivateEventHandler(ExcelApplication_WorkbookActivate);
                application.WorkbookDeactivateEvent  += new Excel.Application_WorkbookDeactivateEventHandler(ExcelApplication_WorkbookDeactivate);
                application.SheetActivateEvent       += new Excel.Application_SheetActivateEventHandler(ExcelApplication_SheetActivateEvent);
                application.SheetDeactivateEvent     += new Excel.Application_SheetDeactivateEventHandler(ExcelApplication_SheetDeactivateEvent);

                // add a new workbook add a sheet and close
                Excel.Workbook workBook = application.Workbooks.Add();
                workBook.Worksheets.Add();
                workBook.Close();

                if (_newWorkbookEvent && _workbookBeforeCloseEvent && _sheetActivateEvent && _sheetDeactivateEvent && _workbookActivateEvent && _workbookDeactivateEvent)
                {
                    return(new TestResult(true, DateTime.Now.Subtract(startTime), "", null, ""));
                }
                else
                {
                    string errorMessage = "";
                    if (!_newWorkbookEvent)
                    {
                        errorMessage += "NewWorkbookEvent failed ";
                    }
                    if (!_workbookBeforeCloseEvent)
                    {
                        errorMessage += "WorkbookBeforeCloseEvent failed ";
                    }
                    if (!_sheetActivateEvent)
                    {
                        errorMessage += "WorkbookActivateEvent failed ";
                    }
                    if (!_sheetDeactivateEvent)
                    {
                        errorMessage += "WorkbookDeactivateEvent failed ";
                    }
                    if (!_workbookActivateEvent)
                    {
                        errorMessage += "SheetActivateEvent failed ";
                    }
                    if (!_workbookDeactivateEvent)
                    {
                        errorMessage += "SheetDeactivateEvent failed ";
                    }

                    return(new TestResult(true, DateTime.Now.Subtract(startTime), errorMessage, null, ""));
                }
            }
            catch (Exception exception)
            {
                return(new TestResult(false, DateTime.Now.Subtract(startTime), exception.Message, exception, ""));
            }
            finally
            {
                if (null != application)
                {
                    application.Quit();
                    application.Dispose();
                }
            }
        }
Beispiel #4
0
        public TestResult DoTest()
        {
            Access.Application application = null;
            DateTime           startTime   = DateTime.Now;

            try
            {
                application = COMObject.Create <Access.Application>(COMObjectCreateOptions.CreateNewCore);

                // create database name
                string fileExtension = GetDefaultExtension(application);
                string documentFile  = string.Format("{0}\\Test3{1}", Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), fileExtension);

                // delete old database if exists
                if (System.IO.File.Exists(documentFile))
                {
                    System.IO.File.Delete(documentFile);
                }

                // create database
                DAO.Database newDatabase = application.DBEngine.Workspaces[0].CreateDatabase(documentFile, LanguageConstants.dbLangGeneral);
                application.DBEngine.Workspaces[0].Close();

                // setup database connection                        'Provider=Microsoft.Jet.OLEDB.4.0;Data Source= < access2007
                OleDbConnection oleConnection = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Persist Security Info=False;Data Source=" + documentFile);
                oleConnection.Open();

                // create table
                OleDbCommand oleCreateCommand = new OleDbCommand("CREATE TABLE NetOfficeTable(Column1 Text, Column2 Text)", oleConnection);
                oleCreateCommand.ExecuteReader().Close();

                // write some data with plain sql & close
                for (int i = 0; i < 20000; i++)
                {
                    string       insertCommand    = string.Format("INSERT INTO NetOfficeTable(Column1, Column2) VALUES(\"{0}\", \"{1}\")", i, DateTime.Now.ToShortTimeString());
                    OleDbCommand oleInsertCommand = new OleDbCommand(insertCommand, oleConnection);
                    oleInsertCommand.ExecuteReader().Close();
                }
                oleConnection.Close();

                // now we do CompactDatabase
                string newDocumentFile = string.Format("{0}\\CompactDatabase{1}", Environment.CurrentDirectory, fileExtension);
                if (File.Exists(newDocumentFile))
                {
                    File.Delete(newDocumentFile);
                }

                application.DBEngine.CompactDatabase(documentFile, newDocumentFile);


                return(new TestResult(true, DateTime.Now.Subtract(startTime), "", null, ""));
            }
            catch (Exception exception)
            {
                return(new TestResult(false, DateTime.Now.Subtract(startTime), exception.Message, exception, ""));
            }
            finally
            {
                if (null != application)
                {
                    // close access and dispose reference
                    application.Quit(AcQuitOption.acQuitSaveNone);
                    application.Dispose();
                }
            }
        }
Beispiel #5
0
        public TestResult DoTest()
        {
            Word.Application application = null;
            DateTime         startTime   = DateTime.Now;

            try
            {
                Bitmap iconBitmap = new Bitmap(System.Reflection.Assembly.GetAssembly(this.GetType()).GetManifestResourceStream("WordTestsCSharp.Test07.bmp"));
                application = COMObject.Create <Word.Application>(COMObjectCreateOptions.CreateNewCore);
                application.Factory.ObjectActivator.CreateDynamicInstance += Factory_CreateCOMDynamic;

                application.DisplayAlerts = WdAlertLevel.wdAlertsNone;
                application.Documents.Add();

                Office.CommandBar       commandBar;
                Office.CommandBarButton commandBarBtn;

                Word.Template normalDotTemplate = GetNormalDotTemplate(application);
                application.CustomizationContext = normalDotTemplate;

                // add a commandbar popup
                Office.CommandBarPopup commandBarPopup = (Office.CommandBarPopup)application.CommandBars["Menu Bar"].Controls.Add(MsoControlType.msoControlPopup, MsoBarPosition.msoBarTop, System.Type.Missing, 1, true);
                commandBarPopup.Caption = "commandBarPopup";

                #region CommandBarButton

                // add a button to the popup
                commandBarBtn         = (Office.CommandBarButton)commandBarPopup.Controls.Add(MsoControlType.msoControlButton, System.Type.Missing, System.Type.Missing, System.Type.Missing, true);
                commandBarBtn.Style   = MsoButtonStyle.msoButtonIconAndCaption;
                commandBarBtn.Caption = "commandBarButton";
                Clipboard.SetDataObject(iconBitmap);
                commandBarBtn.PasteFace();
                commandBarBtn.ClickEvent += new Office.CommandBarButton_ClickEventHandler(commandBarBtn_Click);

                #endregion

                #region Create a new toolbar

                // add a new toolbar
                commandBar         = application.CommandBars.Add("MyCommandBar", MsoBarPosition.msoBarTop, false, true);
                commandBar.Visible = true;

                // add a button to the toolbar
                commandBarBtn             = (Office.CommandBarButton)commandBar.Controls.Add(MsoControlType.msoControlButton, System.Type.Missing, System.Type.Missing, System.Type.Missing, true);
                commandBarBtn.Style       = MsoButtonStyle.msoButtonIconAndCaption;
                commandBarBtn.Caption     = "commandBarButton";
                commandBarBtn.FaceId      = 3;
                commandBarBtn.ClickEvent += new Office.CommandBarButton_ClickEventHandler(commandBarBtn_Click);

                // add a dropdown box to the toolbar
                commandBarPopup         = (Office.CommandBarPopup)commandBar.Controls.Add(MsoControlType.msoControlPopup, System.Type.Missing, System.Type.Missing, System.Type.Missing, true);
                commandBarPopup.Caption = "commandBarPopup";

                // add a button to the popup, we use an own icon for the button
                commandBarBtn         = (Office.CommandBarButton)commandBarPopup.Controls.Add(MsoControlType.msoControlButton, System.Type.Missing, System.Type.Missing, System.Type.Missing, true);
                commandBarBtn.Style   = MsoButtonStyle.msoButtonIconAndCaption;
                commandBarBtn.Caption = "commandBarButton";
                Clipboard.SetDataObject(iconBitmap);
                commandBarBtn.PasteFace();
                commandBarBtn.ClickEvent += new Office.CommandBarButton_ClickEventHandler(commandBarBtn_Click);

                #endregion

                #region Create a new ContextMenu

                // add a commandbar popup
                commandBarPopup         = (Office.CommandBarPopup)application.CommandBars["Text"].Controls.Add(MsoControlType.msoControlPopup, MsoBarPosition.msoBarTop, false, 1, true);
                commandBarPopup.Caption = "commandBarPopup";

                // add a button to the popup
                commandBarBtn             = (Office.CommandBarButton)commandBarPopup.Controls.Add(MsoControlType.msoControlButton, System.Type.Missing, System.Type.Missing, System.Type.Missing, true);
                commandBarBtn.Style       = MsoButtonStyle.msoButtonIconAndCaption;
                commandBarBtn.Caption     = "commandBarButton";
                commandBarBtn.FaceId      = 9;
                commandBarBtn.ClickEvent += new Office.CommandBarButton_ClickEventHandler(commandBarBtn_Click);

                #endregion

                normalDotTemplate.Saved = true;

                return(new TestResult(true, DateTime.Now.Subtract(startTime), "", null, ""));
            }
            catch (Exception exception)
            {
                return(new TestResult(false, DateTime.Now.Subtract(startTime), exception.Message, exception, ""));
            }
            finally
            {
                if (null != application)
                {
                    application.Quit(WdSaveOptions.wdDoNotSaveChanges);
                    application.Dispose();
                }
            }
        }
Beispiel #6
0
        public TestResult DoTest()
        {
            Excel.Application application = null;
            DateTime          startTime   = DateTime.Now;

            try
            {
                Bitmap iconBitmap = new Bitmap(System.Reflection.Assembly.GetAssembly(this.GetType()).GetManifestResourceStream("ExcelTestsCSharp.Test08.bmp"));
                application = COMObject.Create <Excel.Application>(COMObjectCreateOptions.CreateNewCore);
                application.DisplayAlerts = false;
                application.Workbooks.Add();
                Excel.Worksheet sheet = application.Workbooks[1].Sheets[1] as Excel.Worksheet;

                // add a commandbar popup
                Office.CommandBarPopup commandBarPopup = (Office.CommandBarPopup)application.CommandBars["Worksheet Menu Bar"].Controls.Add(MsoControlType.msoControlPopup, System.Type.Missing, System.Type.Missing, System.Type.Missing, true);
                commandBarPopup.Caption = "commandBarPopup";

                #region CommandBarButton

                // add a button to the popup
                Office.CommandBarButton commandBarBtn = (Office.CommandBarButton)commandBarPopup.Controls.Add(MsoControlType.msoControlButton, System.Type.Missing, System.Type.Missing, System.Type.Missing, true);
                commandBarBtn.Style   = MsoButtonStyle.msoButtonIconAndCaption;
                commandBarBtn.Caption = "commandBarButton";
                Clipboard.SetDataObject(iconBitmap);
                commandBarBtn.PasteFace();
                commandBarBtn.ClickEvent += new Office.CommandBarButton_ClickEventHandler(commandBarBtn_Click);

                #endregion

                #region Create a new toolbar

                // add a new toolbar
                Office.CommandBar commandBar = application.CommandBars.Add("MyCommandBar", MsoBarPosition.msoBarTop, false, true);
                commandBar.Visible = true;

                // add a button to the toolbar
                commandBarBtn             = (Office.CommandBarButton)commandBar.Controls.Add(MsoControlType.msoControlButton, System.Type.Missing, System.Type.Missing, System.Type.Missing, true);
                commandBarBtn.Style       = MsoButtonStyle.msoButtonIconAndCaption;
                commandBarBtn.Caption     = "commandBarButton";
                commandBarBtn.FaceId      = 3;
                commandBarBtn.ClickEvent += new Office.CommandBarButton_ClickEventHandler(commandBarBtn_Click);

                // add a dropdown box to the toolbar
                commandBarPopup         = (Office.CommandBarPopup)commandBar.Controls.Add(MsoControlType.msoControlPopup, System.Type.Missing, System.Type.Missing, System.Type.Missing, true);
                commandBarPopup.Caption = "commandBarPopup";

                // add a button to the popup, we use an own icon for the button
                commandBarBtn         = (Office.CommandBarButton)commandBarPopup.Controls.Add(MsoControlType.msoControlButton, System.Type.Missing, System.Type.Missing, System.Type.Missing, true);
                commandBarBtn.Style   = MsoButtonStyle.msoButtonIconAndCaption;
                commandBarBtn.Caption = "commandBarButton";
                Clipboard.SetDataObject(iconBitmap);
                commandBarBtn.PasteFace();
                commandBarBtn.ClickEvent += new Office.CommandBarButton_ClickEventHandler(commandBarBtn_Click);

                #endregion

                #region Create a new ContextMenu

                // add a commandbar popup
                commandBarPopup         = (Office.CommandBarPopup)application.CommandBars["Cell"].Controls.Add(MsoControlType.msoControlPopup, System.Type.Missing, System.Type.Missing, System.Type.Missing, true);
                commandBarPopup.Caption = "commandBarPopup";

                // add a button to the popup
                commandBarBtn             = (Office.CommandBarButton)commandBarPopup.Controls.Add(MsoControlType.msoControlButton);
                commandBarBtn.Style       = MsoButtonStyle.msoButtonIconAndCaption;
                commandBarBtn.Caption     = "commandBarButton";
                commandBarBtn.FaceId      = 9;
                commandBarBtn.ClickEvent += new Office.CommandBarButton_ClickEventHandler(commandBarBtn_Click);

                #endregion

                return(new TestResult(true, DateTime.Now.Subtract(startTime), "", null, ""));
            }
            catch (Exception exception)
            {
                return(new TestResult(false, DateTime.Now.Subtract(startTime), exception.Message, exception, ""));
            }
            finally
            {
                if (null != application)
                {
                    application.Quit();
                    application.Dispose();
                }
            }
        }
Beispiel #7
0
        public TestResult DoTest()
        {
            PowerPoint.Application application = null;
            DateTime startTime = DateTime.Now;

            try
            {
                Bitmap iconBitmap = new Bitmap(System.Reflection.Assembly.GetAssembly(this.GetType()).GetManifestResourceStream("PowerPointTestsCSharp.Test06.bmp"));
                application = COMObject.Create <PowerPoint.Application>(COMObjectCreateOptions.CreateNewCore);

                Office.CommandBar       commandBar;
                Office.CommandBarButton commandBarBtn;

                // add a new presentation with one new slide
                PowerPoint.Presentation presentation = application.Presentations.Add(MsoTriState.msoTrue);
                PowerPoint.Slide        slide        = presentation.Slides.Add(1, PpSlideLayout.ppLayoutBlank);

                // add a commandbar popup
                Office.CommandBarPopup commandBarPopup = (Office.CommandBarPopup)application.CommandBars["Menu Bar"].Controls.Add(MsoControlType.msoControlPopup, System.Type.Missing, System.Type.Missing, System.Type.Missing, true);
                commandBarPopup.Caption = "commandBarPopup";

                #region CommandBarButton

                // add a button to the popup
                commandBarBtn         = (Office.CommandBarButton)commandBarPopup.Controls.Add(MsoControlType.msoControlButton, System.Type.Missing, System.Type.Missing, System.Type.Missing, true);
                commandBarBtn.Style   = MsoButtonStyle.msoButtonIconAndCaption;
                commandBarBtn.Caption = "commandBarButton";
                Clipboard.SetDataObject(iconBitmap);
                commandBarBtn.PasteFace();
                commandBarBtn.ClickEvent += new Office.CommandBarButton_ClickEventHandler(commandBarBtn_Click);

                #endregion

                #region Create a new toolbar

                // add a new toolbar
                commandBar         = application.CommandBars.Add("MyCommandBar", MsoBarPosition.msoBarTop, false, true);
                commandBar.Visible = true;

                // add a button to the toolbar
                commandBarBtn             = (Office.CommandBarButton)commandBar.Controls.Add(MsoControlType.msoControlButton, System.Type.Missing, System.Type.Missing, System.Type.Missing, true);
                commandBarBtn.Style       = MsoButtonStyle.msoButtonIconAndCaption;
                commandBarBtn.Caption     = "commandBarButton";
                commandBarBtn.FaceId      = 3;
                commandBarBtn.ClickEvent += new Office.CommandBarButton_ClickEventHandler(commandBarBtn_Click);

                // add a dropdown box to the toolbar
                commandBarPopup         = (Office.CommandBarPopup)commandBar.Controls.Add(MsoControlType.msoControlPopup, System.Type.Missing, System.Type.Missing, System.Type.Missing, true);
                commandBarPopup.Caption = "commandBarPopup";

                // add a button to the popup, we use an own icon for the button
                commandBarBtn         = (Office.CommandBarButton)commandBarPopup.Controls.Add(MsoControlType.msoControlButton, System.Type.Missing, System.Type.Missing, System.Type.Missing, true);
                commandBarBtn.Style   = MsoButtonStyle.msoButtonIconAndCaption;
                commandBarBtn.Caption = "commandBarButton";
                Clipboard.SetDataObject(iconBitmap);
                commandBarBtn.PasteFace();
                commandBarBtn.ClickEvent += new Office.CommandBarButton_ClickEventHandler(commandBarBtn_Click);

                #endregion

                #region Create a new ContextMenu

                // add a commandbar popup
                commandBarPopup = (Office.CommandBarPopup)application.CommandBars["Frames"].Controls.Add(
                    MsoControlType.msoControlPopup, Missing.Value, Missing.Value, Missing.Value, true);
                commandBarPopup.Caption = "commandBarPopup";

                // add a button to the popup
                commandBarBtn             = (Office.CommandBarButton)commandBarPopup.Controls.Add(MsoControlType.msoControlButton, Missing.Value, Missing.Value, Missing.Value, true);
                commandBarBtn.Style       = MsoButtonStyle.msoButtonIconAndCaption;
                commandBarBtn.Caption     = "commandBarButton";
                commandBarBtn.FaceId      = 9;
                commandBarBtn.ClickEvent += new Office.CommandBarButton_ClickEventHandler(commandBarBtn_Click);

                #endregion

                return(new TestResult(true, DateTime.Now.Subtract(startTime), "", null, ""));
            }
            catch (Exception exception)
            {
                return(new TestResult(false, DateTime.Now.Subtract(startTime), exception.Message, exception, ""));
            }
            finally
            {
                if (null != application)
                {
                    application.Quit();
                    application.Dispose();
                }
            }
        }
Beispiel #8
0
 private void TestAddin_OnConnection(object Application, NetOffice.Tools.ext_ConnectMode ConnectMode, object AddInInst, ref Array custom)
 {
     Office.COMAddIn addin = COMObject.Create <Office.COMAddIn>(AddInInst);
     addin.Object = this;
     addin.Dispose();
 }
Beispiel #9
0
        public void RunExample()
        {
            // create simple a csv-file as datasource
            string fileName = string.Format("{0}\\DataSource.csv", HostApplication.RootDirectory);

            // if file exists then delete
            if (File.Exists(fileName))
            {
                File.Delete(fileName);
            }

            File.AppendAllText(fileName, string.Format("{0},{1}{2}", "ProjectName", "ProjectLink", Environment.NewLine));
            File.AppendAllText(fileName, string.Format("{0},{1}{2}", "NetOffice", "https://github.com/NetOfficeFw/NetOffice", Environment.NewLine));

            // start word and turn off msg boxes
            Word.Application wordApplication = COMObject.Create <Word.Application>();
            wordApplication.DisplayAlerts = WdAlertLevel.wdAlertsNone;

            // create a utils instance, not need for but helpful to keep the lines of code low
            CommonUtils utils = new CommonUtils(wordApplication);

            // add a new document
            Word.Document newDocument = wordApplication.Documents.Add();

            // define the document as mailmerge
            newDocument.MailMerge.MainDocumentType = WdMailMergeMainDocType.wdFormLetters;

            // open the datasource
            newDocument.MailMerge.OpenDataSource(fileName);

            // insert some text and the mailmergefields defined in the datasource
            wordApplication.Selection.TypeText("This test is brought to you by ");
            newDocument.MailMerge.Fields.Add(wordApplication.Selection.Range, "ProjectName");

            wordApplication.Selection.TypeText(" for more information and examples visit ");
            newDocument.MailMerge.Fields.Add(wordApplication.Selection.Range, "ProjectLink ");

            wordApplication.Selection.TypeText(" or click ");

            object adress      = newDocument.MailMerge.DataSource.DataFields[2].Value;
            object screenTip   = "click me if you want!";
            object displayText = "here";

            newDocument.Hyperlinks.Add(wordApplication.Selection.Range, adress, Missing.Value, screenTip, displayText, Missing.Value);

            // show the contents of the fields
            int wdToggle = 9999998;

            newDocument.MailMerge.ViewMailMergeFieldCodes = wdToggle;

            //do not show the fieldcodes
            wordApplication.ActiveWindow.View.ShowFieldCodes = false;

            // save the document
            string documentFile = utils.File.Combine(HostApplication.RootDirectory, "Example04", DocumentFormat.Normal);

            newDocument.SaveAs(documentFile);

            // close word and dispose reference
            wordApplication.Quit();
            wordApplication.Dispose();

            // show end dialog
            HostApplication.ShowFinishDialog(null, documentFile);
        }
Beispiel #10
0
        public void RunExample()
        {
            // start word and turn off msg boxes
            Word.Application wordApplication = COMObject.Create <Word.Application>();
            wordApplication.DisplayAlerts = WdAlertLevel.wdAlertsNone;

            // create a utils instance, not need for but helpful to keep the lines of code low
            CommonUtils utils = new CommonUtils(wordApplication);

            // add a new document
            Word.Document newDocument = wordApplication.Documents.Add();

            // create a new listtemplate
            Word.ListTemplate template = newDocument.ListTemplates.Add(true, "NetOfficeListTemplate");

            //get the predefined listlevels (9)
            Word.ListLevels levels = template.ListLevels;

            // customize the first level of the list
            levels[1].NumberFormat = "%1.";

            // tab is used to change the level
            levels[1].TrailingCharacter = WdTrailingCharacter.wdTrailingTab;
            levels[1].NumberStyle       = WdListNumberStyle.wdListNumberStyleArabic;
            levels[1].NumberPosition    = 0;
            levels[1].Alignment         = WdListLevelAlignment.wdListLevelAlignLeft;
            levels[1].TextPosition      = wordApplication.CentimetersToPoints(0.63F);
            levels[1].TabPosition       = wordApplication.CentimetersToPoints(0.63F);
            levels[1].ResetOnHigher     = 0;
            levels[1].StartAt           = 1;
            levels[1].LinkedStyle       = "";
            levels[1].Font.Bold         = 1;

            // customize the second level of the list
            levels[2].NumberFormat = "%1.%2.";

            // tab is used to change the level
            levels[2].TrailingCharacter = WdTrailingCharacter.wdTrailingTab;
            levels[2].NumberStyle       = WdListNumberStyle.wdListNumberStyleArabic;

            // we want the numbers to appear under the first letter of the higher level
            levels[2].NumberPosition = wordApplication.CentimetersToPoints(0.63F);
            levels[2].Alignment      = WdListLevelAlignment.wdListLevelAlignLeft;

            // and the text should indent a tab more on the right
            levels[2].TextPosition  = wordApplication.CentimetersToPoints(1.4F);
            levels[2].TabPosition   = wordApplication.CentimetersToPoints(1.4F);
            levels[2].ResetOnHigher = 0;
            levels[2].StartAt       = 1;
            levels[2].LinkedStyle   = "";
            levels[2].Font.Italic   = 1;

            // apply the defined listtemplate to the selection
            wordApplication.Selection.Range.ListFormat.ApplyListTemplate(template, false,
                                                                         WdListApplyTo.wdListApplyToWholeList, WdDefaultListBehavior.wdWord9ListBehavior);

            //create a list
            wordApplication.Selection.TypeText("Welcoming");
            wordApplication.Selection.TypeParagraph();

            wordApplication.Selection.TypeText("Introduction");
            wordApplication.Selection.TypeParagraph();

            wordApplication.Selection.TypeText("Presentation");
            wordApplication.Selection.TypeParagraph();

            // execute the indent so the second level gets activated
            wordApplication.Selection.Range.ListFormat.ListIndent();

            wordApplication.Selection.TypeText("Top 1");
            wordApplication.Selection.TypeParagraph();

            wordApplication.Selection.TypeText("Top 2");
            wordApplication.Selection.TypeParagraph();

            wordApplication.Selection.TypeText("Top 3");
            wordApplication.Selection.TypeParagraph();

            // execute the outdent so the first level gets reactivated
            wordApplication.Selection.Range.ListFormat.ListOutdent();
            wordApplication.Selection.TypeText("Questions & Answers");

            // save the document
            string documentFile = utils.File.Combine(HostApplication.RootDirectory, "Example03", DocumentFormat.Normal);

            newDocument.SaveAs(documentFile);

            // close word and dispose reference
            wordApplication.Quit();
            wordApplication.Dispose();

            // show end dialog
            HostApplication.ShowFinishDialog(null, documentFile);
        }
Beispiel #11
0
        private void buttonStartExample_Click(object sender, EventArgs e)
        {
            // start word and turn off msg boxes
            _wordApplication = COMObject.Create <Word.Application>();
            _wordApplication.DisplayAlerts = WdAlertLevel.wdAlertsNone;

            Office.CommandBar       commandBar    = null;
            Office.CommandBarButton commandBarBtn = null;

            // add a new document
            _wordApplication.Documents.Add();

            Word.Template normalDotTemplate = GetNormalDotTemplate();
            _wordApplication.CustomizationContext = normalDotTemplate;

            // add a commandbar popup
            Office.CommandBarPopup commandBarPopup = (Office.CommandBarPopup)_wordApplication.CommandBars["Menu Bar"].Controls.Add(MsoControlType.msoControlPopup, MsoBarPosition.msoBarTop, System.Type.Missing, 1, true);
            commandBarPopup.Caption = "commandBarPopup";

            #region few words, how to access the picture

            /*
             * you can see we use an own icon via .PasteFace()
             * is not possible from outside process boundaries to use the PictureProperty directly
             * the reason for is IPictureDisp: http://support.microsoft.com/kb/286460/de
             * its not important is early or late binding or managed or unmanaged, the behaviour is always the same
             * For example, a COMAddin running as InProcServer and can access the Picture Property
             */
            #endregion

            #region CommandBarButton

            // add a button to the popup
            commandBarBtn         = (Office.CommandBarButton)commandBarPopup.Controls.Add(MsoControlType.msoControlButton, System.Type.Missing, System.Type.Missing, System.Type.Missing, true);
            commandBarBtn.Style   = MsoButtonStyle.msoButtonIconAndCaption;
            commandBarBtn.Caption = "commandBarButton";
            Clipboard.SetDataObject(HostApplication.DisplayIcon.ToBitmap());
            commandBarBtn.PasteFace();
            commandBarBtn.ClickEvent += new Office.CommandBarButton_ClickEventHandler(commandBarBtn_Click);

            #endregion

            #region Create a new toolbar

            // add a new toolbar
            commandBar         = _wordApplication.CommandBars.Add("MyCommandBar", MsoBarPosition.msoBarTop, false, true);
            commandBar.Visible = true;

            // add a button to the toolbar
            commandBarBtn             = (Office.CommandBarButton)commandBar.Controls.Add(MsoControlType.msoControlButton, System.Type.Missing, System.Type.Missing, System.Type.Missing, true);
            commandBarBtn.Style       = MsoButtonStyle.msoButtonIconAndCaption;
            commandBarBtn.Caption     = "commandBarButton";
            commandBarBtn.FaceId      = 3;
            commandBarBtn.ClickEvent += new Office.CommandBarButton_ClickEventHandler(commandBarBtn_Click);

            // add a dropdown box to the toolbar
            commandBarPopup         = (Office.CommandBarPopup)commandBar.Controls.Add(MsoControlType.msoControlPopup, System.Type.Missing, System.Type.Missing, System.Type.Missing, true);
            commandBarPopup.Caption = "commandBarPopup";

            // add a button to the popup, we use an own icon for the button
            commandBarBtn         = (Office.CommandBarButton)commandBarPopup.Controls.Add(MsoControlType.msoControlButton, System.Type.Missing, System.Type.Missing, System.Type.Missing, true);
            commandBarBtn.Style   = MsoButtonStyle.msoButtonIconAndCaption;
            commandBarBtn.Caption = "commandBarButton";
            Clipboard.SetDataObject(HostApplication.DisplayIcon.ToBitmap());
            commandBarBtn.PasteFace();
            commandBarBtn.ClickEvent += new Office.CommandBarButton_ClickEventHandler(commandBarBtn_Click);

            #endregion

            #region Create a new ContextMenu

            // add a commandbar popup
            commandBarPopup         = (Office.CommandBarPopup)_wordApplication.CommandBars["Text"].Controls.Add(MsoControlType.msoControlPopup, MsoBarPosition.msoBarTop, false, 1, true);
            commandBarPopup.Caption = "commandBarPopup";

            // add a button to the popup
            commandBarBtn             = (Office.CommandBarButton)commandBarPopup.Controls.Add(MsoControlType.msoControlButton, System.Type.Missing, System.Type.Missing, System.Type.Missing, true);
            commandBarBtn.Style       = MsoButtonStyle.msoButtonIconAndCaption;
            commandBarBtn.Caption     = "commandBarButton";
            commandBarBtn.FaceId      = 9;
            commandBarBtn.ClickEvent += new Office.CommandBarButton_ClickEventHandler(commandBarBtn_Click);

            #endregion

            normalDotTemplate.Saved = true;

            // make visible & set buttons
            _wordApplication.Visible   = true;
            buttonStartExample.Enabled = false;
            buttonQuitExample.Enabled  = true;
        }
Beispiel #12
0
        public TestResult DoTest()
        {
            NetOffice.Settings.Default.MessageFilter.Enabled = true;
            MSProject.Application application = null;
            DateTime startTime = DateTime.Now;

            try
            {
                application = COMObject.Create <MSProject.Application>(COMObjectCreateOptions.CreateNewCore);

                application.ProjectTaskNewEvent          += new MSProject.Application_ProjectTaskNewEventHandler(ApplicationProjectTaskNewEvent);
                application.ProjectBeforeCloseEvent      += new MSProject.Application_ProjectBeforeCloseEventHandler(ApplicationProjectBeforeCloseEvent);
                application.ProjectBeforeTaskChangeEvent += new MSProject.Application_ProjectBeforeTaskChangeEventHandler(ApplicationProjectBeforeTaskChangeEvent);
                application.ProjectBeforeTaskDeleteEvent += new MSProject.Application_ProjectBeforeTaskDeleteEventHandler(ApplicationProjectBeforeTaskDeleteEvent);

                MSProject.Project newProject = application.Projects.Add();
                MSProject.Task    task1      = newProject.Tasks.Add("Task 1");
                MSProject.Task    task2      = newProject.Tasks.Add("Task 2");

                task2.Delete();

                application.FileCloseAll(PjSaveType.pjDoNotSave);

                if (TaskDeleteEventCalled && TaskChangeEventCalled && BeforeCloseEventCalled && TaskChangeEventCalled)
                {
                    return(new TestResult(true, DateTime.Now.Subtract(startTime), "", null, ""));
                }
                else
                {
                    string errorMessage = "";
                    if (!TaskDeleteEventCalled)
                    {
                        errorMessage += "ProjectBeforeTaskDeleteEvent failed ";
                    }
                    if (!TaskChangeEventCalled)
                    {
                        errorMessage += "ProjectBeforeTaskChangeEvent failed ";
                    }
                    if (!BeforeCloseEventCalled)
                    {
                        errorMessage += "ProjectBeforeCloseEvent failed ";
                    }
                    if (!TaskNewEventCalled)
                    {
                        errorMessage += "ProjectTaskNewEvent failed ";
                    }

                    return(new TestResult(false, DateTime.Now.Subtract(startTime), errorMessage, null, ""));
                }
            }
            catch (Exception exception)
            {
                return(new TestResult(false, DateTime.Now.Subtract(startTime), exception.Message, exception, ""));
            }
            finally
            {
                NetOffice.Settings.Default.MessageFilter.Enabled = false;

                if (null != application)
                {
                    application.Quit(PjSaveType.pjDoNotSave);
                    application.Dispose();
                }
            }
        }
Beispiel #13
0
        public TestResult DoTest()
        {
            Excel.Application application = null;
            DateTime          startTime   = DateTime.Now;

            try
            {
                // start excel and turn off Application msg boxes
                application = COMObject.Create <Excel.Application>(COMObjectCreateOptions.CreateNewCore);
                application.DisplayAlerts = false;

                // add a new workbook
                Excel.Workbook  workBook  = application.Workbooks.Add();
                Excel.Worksheet workSheet = (Excel.Worksheet)workBook.Worksheets[1];

                /* some kind of numerics */

                // the given thread culture in all latebinding calls are stored in NetOffice.Settings.
                // you can change the culture. default is en-us.
                CultureInfo cultureInfo = NetOffice.Settings.Default.ThreadCulture;
                string      Pattern1    = string.Format("0{0}00", cultureInfo.NumberFormat.CurrencyDecimalSeparator);
                string      Pattern2    = string.Format("#{1}##0{0}00", cultureInfo.NumberFormat.CurrencyDecimalSeparator, cultureInfo.NumberFormat.CurrencyGroupSeparator);

                workSheet.Range("A1").Value = "Type";
                workSheet.Range("B1").Value = "Value";
                workSheet.Range("C1").Value = "Formatted " + Pattern1;
                workSheet.Range("D1").Value = "Formatted " + Pattern2;

                int integerValue = 532234;
                workSheet.Range("A3").Value        = "Integer";
                workSheet.Range("B3").Value        = integerValue;
                workSheet.Range("C3").Value        = integerValue;
                workSheet.Range("C3").NumberFormat = Pattern1;
                workSheet.Range("D3").Value        = integerValue;
                workSheet.Range("D3").NumberFormat = Pattern2;

                double doubleValue = 23172.64;
                workSheet.Range("A4").Value            = "double";
                workSheet.Range("B4").Value            = doubleValue;
                workSheet.Range("C4").Value            = doubleValue;
                workSheet.Range("C4").NumberFormat     = Pattern1;
                workSheet.Range("D4").Value            = doubleValue;
                workSheet.get_Range("D4").NumberFormat = Pattern2;

                float floatValue = 84345.9132f;
                workSheet.Range("A5").Value        = "float";
                workSheet.Range("B5").Value        = floatValue;
                workSheet.Range("C5").Value        = floatValue;
                workSheet.Range("C5").NumberFormat = Pattern1;
                workSheet.Range("D5").Value        = floatValue;
                workSheet.Range("D5").NumberFormat = Pattern2;

                Decimal decimalValue = 7251231.313367m;
                workSheet.Range("A6").Value        = "Decimal";
                workSheet.Range("B6").Value        = decimalValue;
                workSheet.Range("C6").Value        = decimalValue;
                workSheet.Range("C6").NumberFormat = Pattern1;
                workSheet.Range("D6").Value        = decimalValue;
                workSheet.Range("D6").NumberFormat = Pattern2;

                workSheet.Range("A9").Value  = "DateTime";
                workSheet.Range("B10").Value = cultureInfo.DateTimeFormat.FullDateTimePattern;
                workSheet.Range("C10").Value = cultureInfo.DateTimeFormat.LongDatePattern;
                workSheet.Range("D10").Value = cultureInfo.DateTimeFormat.ShortDatePattern;
                workSheet.Range("E10").Value = cultureInfo.DateTimeFormat.LongTimePattern;
                workSheet.Range("F10").Value = cultureInfo.DateTimeFormat.ShortTimePattern;

                // DateTime
                DateTime dateTimeValue = DateTime.Now;
                workSheet.Range("B11").Value        = dateTimeValue;
                workSheet.Range("B11").NumberFormat = cultureInfo.DateTimeFormat.FullDateTimePattern;

                workSheet.Range("C11").Value        = dateTimeValue;
                workSheet.Range("C11").NumberFormat = cultureInfo.DateTimeFormat.LongDatePattern;

                workSheet.Range("D11").Value        = dateTimeValue;
                workSheet.Range("D11").NumberFormat = cultureInfo.DateTimeFormat.ShortDatePattern;

                workSheet.Range("E11").Value        = dateTimeValue;
                workSheet.Range("E11").NumberFormat = cultureInfo.DateTimeFormat.LongTimePattern;

                workSheet.Range("F11").Value        = dateTimeValue;
                workSheet.Range("F11").NumberFormat = cultureInfo.DateTimeFormat.ShortTimePattern;

                // string
                workSheet.Range("A14").Value        = "String";
                workSheet.Range("B14").Value        = "This is a sample String";
                workSheet.Range("B14").NumberFormat = "@";

                // number as string
                workSheet.Range("B15").Value        = "513";
                workSheet.Range("B15").NumberFormat = "@";

                // set colums
                workSheet.Columns[1].AutoFit();
                workSheet.Columns[2].AutoFit();
                workSheet.Columns[3].AutoFit();
                workSheet.Columns[4].AutoFit();

                return(new TestResult(true, DateTime.Now.Subtract(startTime), "", null, ""));
            }
            catch (Exception exception)
            {
                return(new TestResult(false, DateTime.Now.Subtract(startTime), exception.Message, exception, ""));
            }
            finally
            {
                if (null != application)
                {
                    application.Quit();
                    application.Dispose();
                }
            }
        }
Beispiel #14
0
        private void buttonStartExample_Click(object sender, EventArgs e)
        {
            // start excel and turn off msg boxes
            _excelApplication = COMObject.Create <Excel.Application>();
            _excelApplication.DisplayAlerts = false;

            Office.CommandBar       commandBar    = null;
            Office.CommandBarButton commandBarBtn = null;

            // add a new workbook
            Excel.Workbook workBook = _excelApplication.Workbooks.Add();

            //var test = _excelApplication.CommandBars["Worksheet Menu Bar"].Controls.Add(
            //        MsoControlType.msoControlPopup, System.Type.Missing, System.Type.Missing, System.Type.Missing, true);

            // add a commandbar popup
            Office.CommandBarPopup commandBarPopup = (Office.CommandBarPopup)
                                                     _excelApplication.CommandBars["Worksheet Menu Bar"].Controls.Add(
                MsoControlType.msoControlPopup, System.Type.Missing, System.Type.Missing, System.Type.Missing, true);
            commandBarPopup.Caption = "commandBarPopup";

            #region few words, how to access the picture

            /*
             * you can see we use an own icon via .PasteFace()
             * is not possible from outside process boundaries to use the PictureProperty directly
             * the reason for is IPictureDisp: http://support.microsoft.com/kb/286460/de
             * its not important is early or late binding or managed or unmanaged, the behaviour is always the same
             * For example, a COMAddin running as InProcServer and can access the Picture Property
             */
            #endregion

            #region CommandBarButton

            // add a button to the popup
            commandBarBtn         = (Office.CommandBarButton)commandBarPopup.Controls.Add(MsoControlType.msoControlButton, System.Type.Missing, System.Type.Missing, System.Type.Missing, true);
            commandBarBtn.Style   = MsoButtonStyle.msoButtonIconAndCaption;
            commandBarBtn.Caption = "commandBarButton";
            Clipboard.SetDataObject(HostApplication.DisplayIcon.ToBitmap());
            commandBarBtn.PasteFace();
            commandBarBtn.ClickEvent += new Office.CommandBarButton_ClickEventHandler(commandBarBtn_Click);

            #endregion

            #region Create a new toolbar

            // add a new toolbar
            commandBar         = _excelApplication.CommandBars.Add("MyCommandBar", MsoBarPosition.msoBarTop, false, true);
            commandBar.Visible = true;

            // add a button to the toolbar
            commandBarBtn             = (Office.CommandBarButton)commandBar.Controls.Add(MsoControlType.msoControlButton, System.Type.Missing, System.Type.Missing, System.Type.Missing, true);
            commandBarBtn.Style       = MsoButtonStyle.msoButtonIconAndCaption;
            commandBarBtn.Caption     = "commandBarButton";
            commandBarBtn.FaceId      = 3;
            commandBarBtn.ClickEvent += new Office.CommandBarButton_ClickEventHandler(commandBarBtn_Click);

            // add a dropdown box to the toolbar
            commandBarPopup         = (Office.CommandBarPopup)commandBar.Controls.Add(MsoControlType.msoControlPopup, System.Type.Missing, System.Type.Missing, System.Type.Missing, true);
            commandBarPopup.Caption = "commandBarPopup";

            // add a button to the popup, we use an own icon for the button
            commandBarBtn         = (Office.CommandBarButton)commandBarPopup.Controls.Add(MsoControlType.msoControlButton, System.Type.Missing, System.Type.Missing, System.Type.Missing, true);
            commandBarBtn.Style   = MsoButtonStyle.msoButtonIconAndCaption;
            commandBarBtn.Caption = "commandBarButton";
            Clipboard.SetDataObject(HostApplication.DisplayIcon.ToBitmap());
            commandBarBtn.PasteFace();
            commandBarBtn.ClickEvent += new Office.CommandBarButton_ClickEventHandler(commandBarBtn_Click);

            #endregion

            #region Create a new ContextMenu

            // add a commandbar popup
            commandBarPopup         = (Office.CommandBarPopup)_excelApplication.CommandBars["Cell"].Controls.Add(MsoControlType.msoControlPopup, System.Type.Missing, System.Type.Missing, System.Type.Missing, true);
            commandBarPopup.Caption = "commandBarPopup";

            // add a button to the popup
            commandBarBtn             = (Office.CommandBarButton)commandBarPopup.Controls.Add(MsoControlType.msoControlButton);
            commandBarBtn.Style       = MsoButtonStyle.msoButtonIconAndCaption;
            commandBarBtn.Caption     = "commandBarButton";
            commandBarBtn.FaceId      = 9;
            commandBarBtn.ClickEvent += new Office.CommandBarButton_ClickEventHandler(commandBarBtn_Click);

            #endregion

            #region Display info

            Excel.Worksheet sheet = (Excel.Worksheet)workBook.Worksheets[1];
            sheet.Cells[2, 2].Value = "this excel instance contains 3 custom menus";
            sheet.Cells[3, 2].Value = "the main menu, the toolbar menu and the cell context menu";
            sheet.Cells[4, 2].Value = "in this case the menus are temporaily created";
            sheet.Cells[5, 2].Value = "they are not persistant and needs no unload event or something like this";
            sheet.Cells[6, 2].Value = "you can also create persistant menus if you want";

            #endregion

            // make visible & set buttons
            _excelApplication.Visible  = true;
            buttonStartExample.Enabled = false;
            buttonQuitExample.Enabled  = true;
        }
Beispiel #15
0
        public TestResult DoTest()
        {
            Word.Application application = null;
            DateTime         startTime   = DateTime.Now;

            try
            {
                application = COMObject.Create <Word.Application>(COMObjectCreateOptions.CreateNewCore);
                application.DisplayAlerts = WdAlertLevel.wdAlertsNone;

                // add a new document
                Word.Document newDocument = application.Documents.Add();

                // create a new listtemplate
                Word.ListTemplate template = newDocument.ListTemplates.Add(true, "NetOfficeListTemplate");

                //get the predefined listlevels (9)
                Word.ListLevels levels = template.ListLevels;

                // customize the first level of the list
                levels[1].NumberFormat = "%1.";

                // tab is used to change the level
                levels[1].TrailingCharacter = WdTrailingCharacter.wdTrailingTab;
                levels[1].NumberStyle       = WdListNumberStyle.wdListNumberStyleArabic;
                levels[1].NumberPosition    = 0;
                levels[1].Alignment         = WdListLevelAlignment.wdListLevelAlignLeft;

                levels[1].TextPosition  = application.CentimetersToPoints(0.63F);
                levels[1].TabPosition   = application.CentimetersToPoints(0.63F);
                levels[1].ResetOnHigher = 0;
                levels[1].StartAt       = 1;
                levels[1].LinkedStyle   = "";
                levels[1].Font.Bold     = 1;

                // customize the second level of the list
                levels[2].NumberFormat = "%1.%2.";

                // tab is used to change the level
                levels[2].TrailingCharacter = WdTrailingCharacter.wdTrailingTab;
                levels[2].NumberStyle       = WdListNumberStyle.wdListNumberStyleArabic;

                // we want the numbers to appear under the first letter of the higher level
                levels[2].NumberPosition = application.CentimetersToPoints(0.63F);
                levels[2].Alignment      = WdListLevelAlignment.wdListLevelAlignLeft;

                // and the text should indent a tab more on the right
                levels[2].TextPosition  = application.CentimetersToPoints(1.4F);
                levels[2].TabPosition   = application.CentimetersToPoints(1.4F);
                levels[2].ResetOnHigher = 0;
                levels[2].StartAt       = 1;
                levels[2].LinkedStyle   = "";
                levels[2].Font.Italic   = 1;

                // apply the defined listtemplate to the selection
                application.Selection.Range.ListFormat.ApplyListTemplate(template, false, WdListApplyTo.wdListApplyToWholeList, WdDefaultListBehavior.wdWord9ListBehavior);

                //create a list
                application.Selection.TypeText("Welcoming");
                application.Selection.TypeParagraph();

                application.Selection.TypeText("Introduction");
                application.Selection.TypeParagraph();

                application.Selection.TypeText("Presentation");
                application.Selection.TypeParagraph();

                // execute the indent so the second level gets activated
                application.Selection.Range.ListFormat.ListIndent();

                application.Selection.TypeText("Top 1");
                application.Selection.TypeParagraph();

                application.Selection.TypeText("Top 2");
                application.Selection.TypeParagraph();

                application.Selection.TypeText("Top 3");
                application.Selection.TypeParagraph();

                // execute the outdent so the first level gets reactivated
                application.Selection.Range.ListFormat.ListOutdent();
                application.Selection.TypeText("Questions & Answers");

                newDocument.Close(false);

                return(new TestResult(true, DateTime.Now.Subtract(startTime), "", null, ""));
            }
            catch (Exception exception)
            {
                return(new TestResult(false, DateTime.Now.Subtract(startTime), exception.Message, exception, ""));
            }
            finally
            {
                if (null != application)
                {
                    application.Quit(WdSaveOptions.wdDoNotSaveChanges);
                    application.Dispose();
                }
            }
        }
Beispiel #16
0
        private void buttonStartExample_Click(object sender, EventArgs e)
        {
            // start powerpoint
            _powerApplication = COMObject.Create <PowerPoint.Application>();

            Office.CommandBar       commandBar    = null;
            Office.CommandBarButton commandBarBtn = null;

            // add a new presentation with one new slide
            PowerPoint.Presentation presentation = _powerApplication.Presentations.Add(MsoTriState.msoTrue);
            PowerPoint.Slide        slide        = presentation.Slides.Add(1, PpSlideLayout.ppLayoutBlank);

            // add a commandbar popup
            Office.CommandBarPopup commandBarPopup = (Office.CommandBarPopup)_powerApplication.CommandBars["Menu Bar"].Controls.Add(MsoControlType.msoControlPopup, System.Type.Missing, System.Type.Missing, System.Type.Missing, true);
            commandBarPopup.Caption = "commandBarPopup";

            #region few words, how to access the picture

            /*
             * you can see we use an own icon via .PasteFace()
             * is not possible from outside process boundaries to use the PictureProperty directly
             * the reason for is IPictureDisp: http://support.microsoft.com/kb/286460/de
             * its not important is early or late binding or managed or unmanaged, the behaviour is always the same
             * For example, a COMAddin running as InProcServer and can access the Picture Property
             */
            #endregion

            #region CommandBarButton

            // add a button to the popup
            commandBarBtn         = (Office.CommandBarButton)commandBarPopup.Controls.Add(MsoControlType.msoControlButton, System.Type.Missing, System.Type.Missing, System.Type.Missing, true);
            commandBarBtn.Style   = MsoButtonStyle.msoButtonIconAndCaption;
            commandBarBtn.Caption = "commandBarButton";
            Clipboard.SetDataObject(HostApplication.DisplayIcon.ToBitmap());
            commandBarBtn.PasteFace();
            commandBarBtn.ClickEvent += new Office.CommandBarButton_ClickEventHandler(commandBarBtn_Click);

            #endregion

            #region Create a new toolbar

            // add a new toolbar
            commandBar         = _powerApplication.CommandBars.Add("MyCommandBar", MsoBarPosition.msoBarTop, false, true);
            commandBar.Visible = true;

            // add a button to the toolbar
            commandBarBtn             = (Office.CommandBarButton)commandBar.Controls.Add(MsoControlType.msoControlButton, System.Type.Missing, System.Type.Missing, System.Type.Missing, true);
            commandBarBtn.Style       = MsoButtonStyle.msoButtonIconAndCaption;
            commandBarBtn.Caption     = "commandBarButton";
            commandBarBtn.FaceId      = 3;
            commandBarBtn.ClickEvent += new Office.CommandBarButton_ClickEventHandler(commandBarBtn_Click);

            // add a dropdown box to the toolbar
            commandBarPopup         = (Office.CommandBarPopup)commandBar.Controls.Add(MsoControlType.msoControlPopup, System.Type.Missing, System.Type.Missing, System.Type.Missing, true);
            commandBarPopup.Caption = "commandBarPopup";

            // add a button to the popup, we use an own icon for the button
            commandBarBtn         = (Office.CommandBarButton)commandBarPopup.Controls.Add(MsoControlType.msoControlButton, System.Type.Missing, System.Type.Missing, System.Type.Missing, true);
            commandBarBtn.Style   = MsoButtonStyle.msoButtonIconAndCaption;
            commandBarBtn.Caption = "commandBarButton";
            Clipboard.SetDataObject(HostApplication.DisplayIcon.ToBitmap());
            commandBarBtn.PasteFace();
            commandBarBtn.ClickEvent += new Office.CommandBarButton_ClickEventHandler(commandBarBtn_Click);

            #endregion

            #region Create a new ContextMenu

            // add a commandbar popup
            commandBarPopup = (Office.CommandBarPopup)_powerApplication.CommandBars["Frames"].Controls.Add(
                MsoControlType.msoControlPopup, Missing.Value, Missing.Value, Missing.Value, true);
            commandBarPopup.Caption = "commandBarPopup";

            // add a button to the popup
            commandBarBtn             = (Office.CommandBarButton)commandBarPopup.Controls.Add(MsoControlType.msoControlButton, Missing.Value, Missing.Value, Missing.Value, true);
            commandBarBtn.Style       = MsoButtonStyle.msoButtonIconAndCaption;
            commandBarBtn.Caption     = "commandBarButton";
            commandBarBtn.FaceId      = 9;
            commandBarBtn.ClickEvent += new Office.CommandBarButton_ClickEventHandler(commandBarBtn_Click);

            #endregion

            // make visible & set buttons
            _powerApplication.Visible  = MsoTriState.msoTrue;
            buttonStartExample.Enabled = false;
            buttonQuitExample.Enabled  = true;
        }