Пример #1
0
        private void AddSmartTagToDocument(Word.Document document)
        {
            Microsoft.Office.Tools.Word.SmartTag smartTagDemo =

                Globals.Factory.CreateSmartTag(
                    "www.microsoft.com/Demo#DemoSmartTag",
                    "Demonstration Smart Tag");

            // Specify the terms to recognize.
            smartTagDemo.Terms.Add("term");
            smartTagDemo.Terms.Add("recognize");

            displayAddress = Globals.Factory.CreateAction("To be replaced");

            // Add the action to the smart tag.
            smartTagDemo.Actions = new Microsoft.Office.Tools.Word.Action[] {
                displayAddress
            };

            Microsoft.Office.Tools.Word.Document vstoDocument =
                Globals.Factory.GetVstoObject(document);
            // Add the smart tag to the document
            vstoDocument.VstoSmartTags.Add(smartTagDemo);

            displayAddress.BeforeCaptionShow += new
                                                Microsoft.Office.Tools.Word.BeforeCaptionShowEventHandler(
                displayAddress_BeforeCaptionShow);

            displayAddress.Click += new
                                    Microsoft.Office.Tools.Word.ActionClickEventHandler(
                displayAddress_Click);
        }
Пример #2
0
 private void CreateDocument2()
 {
     document2 = this.Application.Documents.Add(ref missing,
                                                ref missing, ref missing, ref missing);
     vstoDocument             = Globals.Factory.GetVstoObject(document2);
     vstoDocument.BeforeSave += new SaveEventHandler(vstoDocument_BeforeSave);
 }
Пример #3
0
        //</Snippet9>

        //<Snippet10>
        void Application_DocumentBeforeClose(Word.Document Doc, ref bool Cancel)
        {
            bool isExtended = Globals.Factory.HasVstoObject(Doc);

            if (isExtended)
            {
                Microsoft.Office.Tools.Word.Document vstoDocument = Globals.Factory.GetVstoObject(Doc);


                System.Collections.ArrayList controlsToRemove =
                    new System.Collections.ArrayList();

                // Get all of the Windows Forms controls.
                foreach (object control in vstoDocument.Controls)
                {
                    if (control is System.Windows.Forms.Control)
                    {
                        controlsToRemove.Add(control);
                    }
                }

                // Remove all of the Windows Forms controls from the document.
                foreach (object control in controlsToRemove)
                {
                    vstoDocument.Controls.Remove(control);
                }
            }
        }
Пример #4
0
        public void IUO(Office.IRibbonControl control)
        {
            Microsoft.Office.Interop.Word.Document nativeDocument = Globals.ThisAddIn.Application.ActiveDocument;
            Microsoft.Office.Tools.Word.Document   vstoDocument   = Globals.Factory.GetVstoObject(nativeDocument);
            Office.DocumentProperties properties = (Office.DocumentProperties)vstoDocument.CustomDocumentProperties;

            if (ReadDocumentProperty("Classification") != null)
            {
                properties["Classification"].Delete();
            }

            properties.Add("Classification", false,
                           Microsoft.Office.Core.MsoDocProperties.msoPropertyTypeString,
                           "Internal");

            foreach (Word.Section wordSection in Globals.ThisAddIn.Application.ActiveDocument.Sections)
            {
                Word.Range footerRange = wordSection.Footers[Word.WdHeaderFooterIndex.wdHeaderFooterPrimary].Range;
                footerRange.Font.ColorIndex           = Word.WdColorIndex.wdDarkBlue;
                footerRange.Font.Size                 = 15;
                footerRange.Text                      = "Classification: Internal Use Only";
                footerRange.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter;

                Word.Range headerRange = wordSection.Headers[Word.WdHeaderFooterIndex.wdHeaderFooterPrimary].Range;
                headerRange.Font.ColorIndex           = Word.WdColorIndex.wdDarkBlue;
                headerRange.Font.Size                 = 15;
                headerRange.Text                      = "Classification: Internal Use Only";
                headerRange.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter;
            }
        }
Пример #5
0
        void Application_WindowBeforeDoubleClick(Selection sel, ref bool Cancel)
        {
            int n = sel.ContentControls.Count;

            Microsoft.Office.Interop.Word.ContentControl cp = sel.Range.ParentContentControl;

            String mytitle = null;
            String sid     = null;

            if (cp != null)
            {
                mytitle = cp.Title;
                sid     = cp.ID;


                if (mytitle == "violet")
                {
                    Debug.WriteLine("control selected " + sid);
                    string xid = cp.Tag;

                    Microsoft.Office.Tools.Word.Document vstoDocument = Globals.Factory.GetVstoObject(this.Application.ActiveDocument);
                    CustomXMLPart xmlpart = vstoDocument.CustomXMLParts.SelectByID(xid);
                    string        xml     = xmlpart.XML;


                    Globals.Ribbons.Ribbon1.initPath(xml);

                    Globals.Ribbons.Ribbon1.RibbonUI.ActivateTabMso("TabAddIns");
                }
            }



            //throw new NotImplementedException();
        }
Пример #6
0
        void ThisDocument_BeforeDoubleClick(object sender, Microsoft.Office.Tools.Word.ClickEventArgs e)
        {
            Microsoft.Office.Tools.Word.Document vstoDoc = Globals.Factory.GetVstoObject(this.Application.ActiveDocument);

            int numShapes = Application.Selection.InlineShapes.Count;

            if (numShapes > 0)
            {
                InlineShape s = Application.Selection.InlineShapes[1];
                sendViewpointMessage(s.AlternativeText);
            }
        }
Пример #7
0
 private void CreateDocumentHostItem()
 {
     //<Snippet8>
     if (Globals.ThisAddIn.Application.Documents.Count > 0)
     {
         Microsoft.Office.Interop.Word.Document nativeDocument =
             Globals.ThisAddIn.Application.ActiveDocument;
         Microsoft.Office.Tools.Word.Document vstoDocument =
             Globals.Factory.GetVstoObject(nativeDocument);
     }
     //</Snippet8>
 }
Пример #8
0
        private void ProtectFirstParagraph()
        {
            Microsoft.Office.Tools.Word.Document vstoDocument =
                Globals.Factory.GetVstoObject(this.Application.ActiveDocument);


            vstoDocument.Paragraphs[1].Range.InsertParagraphBefore();

            Word.Range range1 = vstoDocument.Paragraphs[1].Range;
            range1.Text = "You cannot edit or change the formatting of text " +
                          "in this sentence, because this sentence is in a GroupContentControl.";
            range1.Select();

            groupControl1 = vstoDocument.Controls.AddGroupContentControl("groupControl1");
        }
Пример #9
0
        public string ReadDocumentProperty(string propertyName)
        {
            Microsoft.Office.Interop.Word.Document nativeDocument = Globals.ThisAddIn.Application.ActiveDocument;
            Microsoft.Office.Tools.Word.Document   vstoDocument   = Globals.Factory.GetVstoObject(nativeDocument);
            Office.DocumentProperties properties = (Office.DocumentProperties)vstoDocument.CustomDocumentProperties;

            foreach (Office.DocumentProperty prop in properties)
            {
                if (prop.Name == propertyName)
                {
                    return(prop.Value.ToString());
                }
            }
            return(null);
        }
Пример #10
0
        private void Application_DocumentBeforeSave(Word.Document Doc,
                                                    ref bool SaveAsUI, ref bool Cancel)
        {
            bool isExtended = Globals.Factory.HasVstoObject(Doc);


            if (isExtended)
            {
                Microsoft.Office.Tools.Word.Document vstoDocument = Globals.Factory.GetVstoObject(Doc);

                if (vstoDocument.Controls.Contains(button))
                {
                    vstoDocument.Controls.Remove(button);
                    Globals.Ribbons.MyRibbon.addButtonCheckBox.Checked = false;
                }
            }
        }
Пример #11
0
        //</Snippet15>

        private void ResizeBookmark()
        {
            //<Snippet16>
            Microsoft.Office.Tools.Word.Document vstoDocument =
                Globals.Factory.GetVstoObject(this.Application.ActiveDocument);


            vstoDocument.ActiveWindow.View.ShowBookmarks = true;
            vstoDocument.Paragraphs[1].Range.InsertParagraphBefore();

            Word.Range firstParagraph = vstoDocument.Paragraphs[1].Range;
            firstParagraph.Text = "123456789abcdefghijklmnopqrstuvwxyz";

            Microsoft.Office.Tools.Word.Bookmark sampleBookmark =
                vstoDocument.Controls.AddBookmark(firstParagraph, "bookmark1");
            sampleBookmark.Start = sampleBookmark.Start + 5;
            sampleBookmark.End   = sampleBookmark.End - 5;
            //</Snippet16>
        }
Пример #12
0
        public void RemoveClassificationButton(Office.IRibbonControl control)
        {
            Microsoft.Office.Interop.Word.Document nativeDocument = Globals.ThisAddIn.Application.ActiveDocument;
            Microsoft.Office.Tools.Word.Document   vstoDocument   = Globals.Factory.GetVstoObject(nativeDocument);
            Office.DocumentProperties properties = (Office.DocumentProperties)vstoDocument.CustomDocumentProperties;

            if (ReadDocumentProperty("Classification") != null)
            {
                properties["Classification"].Delete();
            }

            foreach (Word.Section wordSection in Globals.ThisAddIn.Application.ActiveDocument.Sections)
            {
                Word.Range footerRange = wordSection.Footers[Word.WdHeaderFooterIndex.wdHeaderFooterPrimary].Range;
                footerRange.Delete();
                Word.Range headerRange = wordSection.Headers[Word.WdHeaderFooterIndex.wdHeaderFooterPrimary].Range;
                headerRange.Delete();
            }
        }
        void Application_DocumentBeforeSave(Word.Document Doc, ref bool SaveAsUI, ref bool Cancel)
        {
            Microsoft.Office.Interop.Word.Document   nativeDocument = Globals.ThisAddIn.Application.ActiveDocument;
            Microsoft.Office.Tools.Word.Document     vstoDocument   = Globals.Factory.GetVstoObject(nativeDocument);
            Microsoft.Office.Core.DocumentProperties properties     = (Office.DocumentProperties)vstoDocument.CustomDocumentProperties;
            string messageBoxText = "We noticed you haven't classified your document, would you like to opt out of classifying this document?";
            string caption        = "Classify Your Document";

            object oBasic = Application.WordBasic;

            object fIsAutoSave =
                oBasic.GetType().InvokeMember(
                    "IsAutosaveEvent",
                    BindingFlags.GetProperty,
                    null, oBasic, null);

            MessageBoxButtons button = MessageBoxButtons.YesNo;
            MessageBoxIcon    icon   = MessageBoxIcon.Warning;

            if (ReadDocumentProperty2("Classification") == null && !(int.Parse(fIsAutoSave.ToString()) == 1))
            {
                DialogResult result = MessageBox.Show(messageBoxText, caption, button, icon);
                switch (result)
                {
                case DialogResult.Yes:
                    properties.Add("Classification", false,
                                   Microsoft.Office.Core.MsoDocProperties.msoPropertyTypeString,
                                   "Not Classified");
                    break;

                case DialogResult.No:
                    MessageBox.Show("Please add a classification and try saving the document again", "Classify", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    Cancel = true;
                    break;
                }
            }
        }
Пример #14
0
        private void AddProtectedContentControls()
        {
            Microsoft.Office.Tools.Word.Document vstoDocument =
                Globals.Factory.GetVstoObject(this.Application.ActiveDocument);


            vstoDocument.Paragraphs[1].Range.InsertParagraphBefore();
            Word.Range range1 = vstoDocument.Paragraphs[1].Range;

            deletableControl = vstoDocument.Controls.AddRichTextContentControl(range1,
                                                                               "deletableControl");
            deletableControl.PlaceholderText = "You can delete this control, " +
                                               "but you cannot edit it";
            deletableControl.LockContents = true;

            range1.InsertParagraphAfter();
            Word.Range range2 = vstoDocument.Paragraphs[2].Range;

            editableControl = vstoDocument.Controls.AddRichTextContentControl(range2,
                                                                              "editableControl");
            editableControl.PlaceholderText = "You can edit this control, " +
                                              "but you cannot delete it.";
            editableControl.LockContentControl = true;
        }
Пример #15
0
        internal void AddPictureContentControl(Utility _utility)
        {
            Microsoft.Office.Tools.Word.Document    vstoDocument = Globals.Factory.GetVstoObject(this.Application.ActiveDocument);
            Microsoft.Office.Interop.Word.Selection selection    = this.Application.Selection;
            if (selection != null && selection.Range != null)
            {
                WdSelectionType sel = selection.Type;    // inlineshape



                // if (sel == WdSelectionType.wdSelectionInlineShape)
                {
                    //  InlineShapes shape = vstoDocument.InlineShapes;
                    //shape[0].
                    MemoryStream ms = new MemoryStream(_utility.BitmapBytes);



                    Image _drawnimage = Image.FromStream(ms);

                    //_drawnimage.Save("temp.jpg");
                    // Word.ContentControl contentControl = Globals.ThisAddIn.Application.ActiveDocument.SelectContentControlsByTitle(_utility.TagName)[0];
                    //foreach (Word.ContentControl contentControl in vstoDocument.Content.ContentControls)
                    {
                        //  WdContentControlType type = contentControl.Type;
                        //if (type == WdContentControlType.wdContentControlPicture && contentControl.Tag.Equals(_utility.TagName))
                        {
                            //object missing = Type.Missing;
                            //Microsoft.Office.Interop.Word.ContentControl contentControl = vstoDocument.ContentControls.Add(WdContentControlType.wdContentControlPicture,
                            //                                                          ref missing);
                            //contentControl.Range.InlineShapes.AddPicture("temp.jpg", ref missing, ref missing,
                            //                                                  ref missing);


                            //contentControl.Title = "violet";
                            Microsoft.Office.Interop.Word.ContentControl cp2 = selection.Range.ParentContentControl;

                            if (cp2 != null)//update
                            {
                                CustomXMLPart xmlpart = vstoDocument.CustomXMLParts.SelectByID(cp2.Tag);
                                xmlpart.Delete();
                                cp2.Delete();
                            }


                            CustomXMLPart cp = vstoDocument.CustomXMLParts.Add(_utility.xml);


                            PictureContentControl piccontrol = vstoDocument.Controls.AddPictureContentControl(selection.Range, Guid.NewGuid().ToString());
                            piccontrol.Image = _drawnimage;// ScaleImage(_drawnimage, 200, 150);//Save(new Bitmap(returnImage), 270, 180, 0);
                            piccontrol.Title = "violet";
                            piccontrol.Tag   = cp.Id;
                        }
                    }

                    //      vstoDocument.Save();
                    ms.Flush();
                    ms.Close();
                }
            }
        }
Пример #16
0
        //</Snippet10>

        //<Snippet11>
        private void Application_DocumentOpen_ClearActiveXWrappers(Word.Document Doc)
        {
            Microsoft.Office.Tools.Word.Document vstoDocument = Globals.Factory.GetVstoObject(Doc);
        }
Пример #17
0
        /// <summary>
        /// Create OpenDoPE parts, including optionally, question part.
        /// </summary>
        public void process()
        {
            Microsoft.Office.Interop.Word.Document document = null;
            try
            {
                document = Globals.ThisAddIn.Application.ActiveDocument;
            }
            catch (Exception ex)
            {
                Mbox.ShowSimpleMsgBoxError("No document is open/active. Create or open a docx first.");
                return;
            }

            Model model = Model.ModelFactory(document);

            // Button shouldn't be available if this exists,
            // but ..
            if (model.conditionsPart == null)
            {
                conditions conditions    = new conditions();
                string     conditionsXml = conditions.Serialize();
                model.conditionsPart = addCustomXmlPart(document, conditionsXml);
            }

            if (model.componentsPart == null)
            {
                components components    = new components();
                string     componentsXml = components.Serialize();
                model.componentsPart = addCustomXmlPart(document, componentsXml);
            }

            // Add XPath
            xpaths xpaths = new xpaths();

            // Button shouldn't be available if this exists,
            // but ..
            if (model.xpathsPart != null)
            {
                xpaths.Deserialize(model.xpathsPart.XML, out xpaths);
            }
            int idInt = 1;

            foreach (Word.ContentControl cc in Globals.ThisAddIn.Application.ActiveDocument.ContentControls)
            {
                if (cc.XMLMapping.IsMapped)
                {
                    log.Debug("Adding xpath for " + cc.ID);
                    // then we need to add an XPath
                    string xmXpath = cc.XMLMapping.XPath;

                    xpathsXpath item = new xpathsXpath();
                    // I make no effort here to check whether the xpath
                    // already exists, since the part shouldn't already exist!

                    item.id = "x" + idInt;


                    xpathsXpathDataBinding db = new xpathsXpathDataBinding();
                    db.xpath       = xmXpath;
                    db.storeItemID = cc.XMLMapping.CustomXMLPart.Id;
                    if (!string.IsNullOrWhiteSpace(cc.XMLMapping.PrefixMappings))
                    {
                        db.prefixMappings = cc.XMLMapping.PrefixMappings;
                    }
                    item.dataBinding = db;

                    xpaths.xpath.Add(item);

                    // Write tag
                    TagData td = new TagData(cc.Tag);
                    td.set("od:xpath", item.id);
                    cc.Tag = td.asQueryString();

                    log.Debug(".. added for " + cc.ID);
                    idInt++;
                }
            }
            string xpathsXml = xpaths.Serialize();

            if (model.xpathsPart == null)
            {
                model.xpathsPart = addCustomXmlPart(document, xpathsXml);
            }
            else
            {
                CustomXmlUtilities.replaceXmlDoc(model.xpathsPart, xpathsXml);
            }


            Microsoft.Office.Tools.Word.Document extendedDocument
                = Globals.ThisAddIn.Application.ActiveDocument.GetVstoObject(Globals.Factory);

            //Microsoft.Office.Tools.CustomTaskPane ctp
            //    = Globals.ThisAddIn.createCTP(document, cxp, xpathsPart, conditionsPart, questionsPart, componentsPart);
            //extendedDocument.Tag = ctp;
            //// Want a 2 way association
            //WedTaskPane wedTaskPane = (WedTaskPane)ctp.Control;
            //wedTaskPane.associatedDocument = document;

            //extendedDocument.Shutdown += new EventHandler(
            //    Globals.ThisAddIn.extendedDocument_Shutdown);

            //taskPane.setupCcEvents(document);

            log.Debug("Done. Task pane now also open.");
        }
Пример #18
0
 private void DocumentBeforeDoubleClick()
 {
     Microsoft.Office.Tools.Word.Document vstoDoc = Globals.Factory.GetVstoObject(this.Application.ActiveDocument);
     vstoDoc.BeforeDoubleClick += new Microsoft.Office.Tools.Word.ClickEventHandler(ThisDocument_BeforeDoubleClick);
 }