Esempio n. 1
0
        //Open document on new object
        public DocBuffer()
        {
            //Clear out unused buffer bookmarks
            Word.Bookmarks bookmarks = Globals.ThisAddIn.Application.ActiveDocument.Bookmarks;
            bookmarks.ShowHidden = true;

            foreach (Word.Bookmark mark in bookmarks)
            {
                if (mark.Name.Contains("_buf"))
                {
                    mark.Delete();
                }
            }

            //Remove trail of undo's for clearing out the bookmarks
            Globals.ThisAddIn.Application.ActiveDocument.UndoClear();

            //Set up template
            template    = Globals.ThisAddIn.Application.NormalTemplate;
            templateObj = template;

            //Open Blank document, then attach styles *and update
            HiddenDoc = Globals.ThisAddIn.Application.Documents.Add(ref missing, ref missing, ref missing, ref FalseObj);
            HiddenDoc.set_AttachedTemplate(ref templateObj);
            HiddenDoc.UpdateStyles();

            //Tell hidden document it has been saved to remove rare prompt to save document
            HiddenDoc.Saved = true;

            //Make primary document active
            Globals.ThisAddIn.Application.ActiveDocument.Activate();
        }
Esempio n. 2
0
        private void PopulateDocumentPath()
        {
            string docPath = this.Application.ActiveDocument.Path;

            if (string.IsNullOrEmpty(docPath))
            {
                Word.Template template = (Word.Template) this.Application.ActiveDocument.get_AttachedTemplate();
                docPath = template.Path;
            }

            documentEditProperties[this.Application.ActiveDocument.Name].DocumentPath = docPath;
            //do not prepopulate file name - let user do this
            //documentEditProperties[this.Application.ActiveDocument.Name].PDFFullName = Path.Combine(
            //    docPath,string.Concat(Path.GetFileNameWithoutExtension(this.Application.ActiveDocument.Name), ".pdf"));
        }
Esempio n. 3
0
        private void LoadDefaultBuildingBlock()
        {
            object Categorie         = "Built-In";
            object BuildingBlockName = "Double Table ";
            object True = true;

            //The path of the Building Block.dotx may depends on your opreating system,
            //We could use HKLM hive to locate Word.
            object WordPath;

            WordPath = Registry.GetValue(@"HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Office\12.0\Word\InstallRoot", "Path", "");
            Globals.ThisDocument.AttachedTemplate = WordPath + @"Document Parts\1033\Building Blocks.dotx";
            Word.Template template = (Word.Template)Globals.ThisDocument.AttachedTemplate;
            //For example we'll insert the "Double Table" into our document, we can located the specific Building Block via WdBuildingBlockTypes,Categorie and BuildingBlockName.
            template.BuildingBlockTypes.Item(Microsoft.Office.Interop.Word.WdBuildingBlockTypes.wdTypeTables).Categories.Item(ref Categorie).BuildingBlocks.Item(ref BuildingBlockName).Insert(Globals.ThisDocument.Application.Selection.Range, ref True);
        }
Esempio n. 4
0
        void ProcessDocument(Word.Document Doc)
        {
            settings = Properties.Settings.Default;
            string templateName = settings.TemplateName;

            wdDoc = Doc;
            Word.Template template = (Word.Template)Doc.get_AttachedTemplate();
            templatePath = Path.GetDirectoryName(template.FullName);
            if (template.Name == templateName)
            {
                if (!connectedTaskPane)
                {
                    ((Word.DocumentEvents_Event)Doc).Close += new Microsoft.Office.Interop.Word.DocumentEvents_CloseEventHandler(ThisAddIn_DocumentClose);
                    ctlAnn           = new AnnouncementsControl();
                    ctpAnnouncements = CustomTaskPanes.Add(ctlAnn, "Announcements");
                    int wid = Properties.Settings.Default.CTPWidth;
                    if (wid != 0)
                    {
                        ctpAnnouncements.Width = wid;
                    }
                    ctlAnn.CreateAnnouncements        += new AnnouncementsAddIn.AnnouncementsControl.CreateAnnouncementsHandler(ctlAnn_CreateAnnouncements);
                    ctlAnn.SaveAnnouncements          += new EventHandler(ctlAnn_SaveAnnouncements);
                    ctlAnn.AddEditReminder            += new AnnouncementsControl.AnnouncementsReminderHandler(ctlAnn_AddEditReminder);
                    ctlAnn.PrintAnnouncements         += new EventHandler(ctlAnn_PrintAnnouncements);
                    ctlAnn.DateSelected               += new AnnouncementsControl.DateSelectedHandler(ctlAnn_DateSelected);
                    Doc.ActiveWindow.View.Zoom.PageFit = Microsoft.Office.Interop.Word.WdPageFit.wdPageFitBestFit;
                    ConnectToExcel();
                    connectedTaskPane = true;
                }
                ctpAnnouncements.Visible = true;
            }
            else if (connectedTaskPane)
            {
                ctpAnnouncements.Visible = false;
            }
        }
        private void findTemplate()
        {
            foreach (Word.Template t in Globals.ThisAddIn.Application.Templates)
            {
                //C:\Users\jharrop\Documents\Visual Studio 2010\Projects\OpenDope_Author_NoXML\trunk\OpenDope_AnswerFormat\OpenDope_AnswerFormat\bin\Debug\FabDocx.dotx
                //C:\Users\jharrop\AppData\Roaming\Microsoft\Templates\Normal.dotm

                if (t.FullName.Contains(templatePath))
                {
                    FabDotxAsTemplate = t;
                    log.Debug("Found template " + t.FullName);
                    break;
                }
            }
        }
Esempio n. 6
0
        /// <summary>
        /// Convert a Word file to PDF
        /// </summary>
        /// <param name="inputFile">Full path of the input Word file</param>
        /// <param name="outputFile">Full path of the output PDF</param>
        /// <returns></returns>
        public static new int Convert(String inputFile, String outputFile, Hashtable options)
        {
            Boolean running = (Boolean)options["noquit"];

            Microsoft.Office.Interop.Word.Application word = null;
            object oMissing = System.Reflection.Missing.Value;

            Microsoft.Office.Interop.Word.Template tmpl;
            String           temporaryStorageDir = null;
            float            wordVersion         = 0;
            List <AppOption> wordOptionList      = new List <AppOption>();

            try
            {
                String  filename           = (String)inputFile;
                Boolean hasSignatures      = WordConverter.hasDigitalSignatures(filename);
                Boolean visible            = !(Boolean)options["hidden"];
                Boolean openAndRepair      = !(Boolean)options["word_no_repair"];
                Boolean nowrite            = (Boolean)options["readonly"];
                Boolean includeProps       = !(Boolean)options["excludeprops"];
                Boolean includeTags        = !(Boolean)options["excludetags"];
                Boolean bitmapMissingFonts = !(Boolean)options["word_ref_fonts"];
                Boolean autosave           = options.ContainsKey("IsTempWord") && (Boolean)options["IsTempWord"];
                bool    pdfa          = (Boolean)options["pdfa"] ? true : false;
                String  writePassword = "";
                String  readPassword  = "";
                int     maxPages      = 0;

                WdExportOptimizeFor     quality    = WdExportOptimizeFor.wdExportOptimizeForPrint;
                WdExportItem            showMarkup = WdExportItem.wdExportDocumentContent;
                WdExportCreateBookmarks bookmarks  = (Boolean)options["bookmarks"] ?
                                                     WdExportCreateBookmarks.wdExportCreateHeadingBookmarks :
                                                     WdExportCreateBookmarks.wdExportCreateNoBookmarks;
                Microsoft.Office.Interop.Word.Options   wdOptions      = null;
                Microsoft.Office.Interop.Word.Documents documents      = null;
                Microsoft.Office.Interop.Word.Template  normalTemplate = null;

                tmpl = null;
                try
                {
                    word = (Microsoft.Office.Interop.Word.Application)Marshal.GetActiveObject("Word.Application");
                }
                catch (System.Exception)
                {
                    int tries = 10;
                    word    = new Microsoft.Office.Interop.Word.Application();
                    running = false;
                    while (tries > 0)
                    {
                        try
                        {
                            // Try to set a property on the object
                            word.ScreenUpdating = false;
                        }
                        catch (COMException)
                        {
                            // Decrement the number of tries and have a bit of a snooze
                            tries--;
                            Thread.Sleep(500);
                            continue;
                        }
                        // Looks ok, so bail out of the loop
                        break;
                    }
                    if (tries == 0)
                    {
                        Converter.releaseCOMObject(word);
                        return((int)ExitCode.ApplicationError);
                    }
                }

                wdOptions          = word.Options;
                word.DisplayAlerts = WdAlertLevel.wdAlertsNone;
                // Issue #48 - we should allow control over whether the history is lost
                if (!(Boolean)options["word_keep_history"])
                {
                    word.DisplayRecentFiles = false;
                }
                word.DisplayDocumentInformationPanel = false;
                word.FeatureInstall = Microsoft.Office.Core.MsoFeatureInstall.msoFeatureInstallNone;
                wordVersion         = (float)System.Convert.ToDecimal(word.Version, new CultureInfo("en-US"));

                // Set the Word options in a way that allows us to reset the options when we finish
                try
                {
                    wordOptionList.Add(new AppOption("AlertIfNotDefault", false, ref wdOptions));
                    wordOptionList.Add(new AppOption("AllowReadingMode", false, ref wdOptions));
                    wordOptionList.Add(new AppOption("PrecisePositioning", true, ref wdOptions));
                    wordOptionList.Add(new AppOption("UpdateFieldsAtPrint", false, ref wdOptions));
                    wordOptionList.Add(new AppOption("UpdateLinksAtPrint", false, ref wdOptions));
                    wordOptionList.Add(new AppOption("UpdateLinksAtOpen", false, ref wdOptions));
                    wordOptionList.Add(new AppOption("UpdateFieldsWithTrackedChangesAtPrint", false, ref wdOptions));
                    wordOptionList.Add(new AppOption("WarnBeforeSavingPrintingSendingMarkup", false, ref wdOptions));
                    wordOptionList.Add(new AppOption("BackgroundSave", true, ref wdOptions));
                    wordOptionList.Add(new AppOption("SavePropertiesPrompt", false, ref wdOptions));
                    wordOptionList.Add(new AppOption("DoNotPromptForConvert", true, ref wdOptions));
                    wordOptionList.Add(new AppOption("PromptUpdateStyle", false, ref wdOptions));
                    wordOptionList.Add(new AppOption("ConfirmConversions", false, ref wdOptions));
                    wordOptionList.Add(new AppOption("CheckGrammarAsYouType", false, ref wdOptions));
                    wordOptionList.Add(new AppOption("CheckGrammarWithSpelling", false, ref wdOptions));
                    wordOptionList.Add(new AppOption("CheckSpellingAsYouType", false, ref wdOptions));
                    wordOptionList.Add(new AppOption("DisplaySmartTagButtons", false, ref wdOptions));
                    wordOptionList.Add(new AppOption("EnableLivePreview", false, ref wdOptions));
                    wordOptionList.Add(new AppOption("ShowReadabilityStatistics", false, ref wdOptions));
                    wordOptionList.Add(new AppOption("SuggestSpellingCorrections", false, ref wdOptions));
                    wordOptionList.Add(new AppOption("AllowDragAndDrop", false, ref wdOptions));
                    wordOptionList.Add(new AppOption("EnableMisusedWordsDictionary", false, ref wdOptions));
                    wordOptionList.Add(new AppOption("ShowFormatError", false, ref wdOptions));
                    wordOptionList.Add(new AppOption("StoreRSIDOnSave", false, ref wdOptions));
                    wordOptionList.Add(new AppOption("SaveNormalPrompt", false, ref wdOptions));
                    wordOptionList.Add(new AppOption("AllowFastSave", false, ref wdOptions));
                    wordOptionList.Add(new AppOption("BackgroundOpen", false, ref wdOptions));
                    wordOptionList.Add(new AppOption("ShowMarkupOpenSave", false, ref wdOptions));
                    wordOptionList.Add(new AppOption("SaveInterval", 0, ref wdOptions));
                }
                catch (SystemException)
                {
                }

                // Set up the PDF output quality
                if ((Boolean)options["print"])
                {
                    quality = WdExportOptimizeFor.wdExportOptimizeForPrint;
                }
                if ((Boolean)options["screen"])
                {
                    quality = WdExportOptimizeFor.wdExportOptimizeForOnScreen;
                }

                if ((Boolean)options["markup"])
                {
                    showMarkup = WdExportItem.wdExportDocumentWithMarkup;
                }

                if (!String.IsNullOrEmpty((String)options["password"]))
                {
                    readPassword = (String)options["password"];
                }

                if (!String.IsNullOrEmpty((String)options["writepassword"]))
                {
                    writePassword = (String)options["writepassword"];
                }

                // Large Word files may simply not print reliably - if the word_max_pages
                // configuration option is set, then we must close up and forget about
                // converting the file.
                maxPages = (int)options[@"word_max_pages"];

                documents      = word.Documents;
                normalTemplate = word.NormalTemplate;

                // Check for password protection and no password
                if (Converter.IsPasswordProtected(inputFile) && String.IsNullOrEmpty(readPassword))
                {
                    normalTemplate.Saved = true;
                    Console.WriteLine("Unable to open password protected file");
                    return((int)ExitCode.PasswordFailure);
                }

                // Having signatures means we should open the document very carefully
                if (hasSignatures)
                {
                    nowrite       = true;
                    autosave      = false;
                    openAndRepair = false;
                }

                Microsoft.Office.Interop.Word.Document doc = null;
                try
                {
                    if ((bool)options["merge"] && !String.IsNullOrEmpty((string)options["template"]) &&
                        File.Exists((string)options["template"]) &&
                        System.Text.RegularExpressions.Regex.IsMatch((string)options["template"], @"^.*\.dot[mx]?$", System.Text.RegularExpressions.RegexOptions.IgnoreCase))
                    {
                        // Create a new document based on a template
                        doc = documents.Add((string)options["template"]);
                        Object rStart = 0;
                        Object rEnd   = 0;
                        Range  range  = doc.Range(rStart, rEnd);
                        range.InsertFile(inputFile);
                        Converter.releaseCOMObject(range);
                        // Make sure we save the file with the original filename so
                        // filename fields update correctly
                        temporaryStorageDir = Path.GetTempFileName();
                        File.Delete(temporaryStorageDir);
                        Directory.CreateDirectory(temporaryStorageDir);
                        doc.SaveAs(Path.Combine(temporaryStorageDir, Path.GetFileName(inputFile)));
                    }
                    else
                    {
                        // Open the source document
                        doc = documents.OpenNoRepairDialog(FileName: filename, ReadOnly: nowrite, PasswordDocument: readPassword, WritePasswordDocument: writePassword, Visible: visible, OpenAndRepair: openAndRepair);
                    }
                }
                catch (System.Runtime.InteropServices.COMException)
                {
                    Console.WriteLine("Unable to open file");
                    return((int)ExitCode.FileOpenFailure);
                }

                // Check if there are signatures in the document which changes how we do things
                if (hasSignatures)
                {
                    // Add in a delay to allow signatures to load
                    Thread.Sleep(500);
                }
                else
                {
                    Microsoft.Office.Interop.Word.Window docWin     = null;
                    Microsoft.Office.Interop.Word.View   docWinView = null;

                    doc.Activate();
                    // Check if there are too many pages
                    if (maxPages > 0)
                    {
                        var pageCount = doc.ComputeStatistics(WdStatistic.wdStatisticPages, false);
                        doc.Saved = true;
                        if (pageCount > maxPages)
                        {
                            throw new Exception(String.Format("Too many pages to process ({0}). More than {1}", pageCount, maxPages));
                        }
                    }

                    // Prevent "property not available" errors, see http://blogs.msmvps.com/wordmeister/2013/02/22/word2013bug-not-available-for-reading/
                    docWin     = doc.ActiveWindow;
                    docWinView = docWin.View;
                    if (wordVersion >= 15)
                    {
                        docWinView.ReadingLayout = false;
                    }

                    // Sometimes the print view will not be available (e.g. for a blog post)
                    // Try and switch view
                    try
                    {
                        docWinView.Type = WdViewType.wdPrintPreview;
                    }
                    catch (Exception) { }

                    // Hide comments
                    try
                    {
                        docWinView.RevisionsView            = WdRevisionsView.wdRevisionsViewFinal;
                        docWinView.ShowRevisionsAndComments = false;
                    }
                    catch (SystemException) { }

                    // Try to avoid Word thinking any changes are happening to the document
                    doc.SpellingChecked = true;
                    doc.GrammarChecked  = true;

                    // Changing these properties may be disallowed if the document is protected
                    // and is not signed
                    if (doc.ProtectionType == WdProtectionType.wdNoProtection && !hasSignatures)
                    {
                        if (autosave)
                        {
                            doc.Save(); doc.Saved = true;
                        }
                        doc.TrackMoves      = false;
                        doc.TrackRevisions  = false;
                        doc.TrackFormatting = false;
                    }

                    normalTemplate.Saved = true;

                    // Hide the document window if need be
                    if ((Boolean)options["hidden"])
                    {
                        var activeWin = word.ActiveWindow;
                        activeWin.Visible     = false;
                        activeWin.WindowState = WdWindowState.wdWindowStateMinimize;
                        Converter.releaseCOMObject(activeWin);
                    }

                    // Check if we have a template file to apply to this document
                    // The template must be a file and must end in .dot, .dotx or .dotm
                    if (!String.IsNullOrEmpty((String)options["template"]) && !(bool)options["merge"])
                    {
                        string template = (string)options["template"];
                        if (File.Exists(template) && System.Text.RegularExpressions.Regex.IsMatch(template, @"^.*\.dot[mx]?$"))
                        {
                            doc.set_AttachedTemplate(template);
                            doc.UpdateStyles();
                            tmpl = doc.get_AttachedTemplate();
                        }
                        else
                        {
                            Console.WriteLine("Invalid template '{0}'", template);
                        }
                    }

                    // See if we have to update fields
                    if (!(Boolean)options["word_no_field_update"])
                    {
                        updateDocumentFields(doc, word, inputFile, options);
                    }

                    var pageSetup = doc.PageSetup;
                    if ((float)options["word_header_dist"] >= 0)
                    {
                        pageSetup.HeaderDistance = (float)options["word_header_dist"];
                    }
                    if ((float)options["word_footer_dist"] >= 0)
                    {
                        pageSetup.FooterDistance = (float)options["word_footer_dist"];
                    }
                    Converter.releaseCOMObject(pageSetup);
                    try
                    {
                        // Make sure we are not in a header footer view
                        docWinView.SeekView = WdSeekView.wdSeekPrimaryHeader;
                        docWinView.SeekView = WdSeekView.wdSeekPrimaryFooter;
                        docWinView.SeekView = WdSeekView.wdSeekMainDocument;
                    }
                    catch (Exception)
                    {
                        // We might fail when switching views
                    }

                    normalTemplate.Saved = true;
                    if (autosave)
                    {
                        doc.Save();
                    }
                    doc.Saved = true;
                    Converter.releaseCOMObject(docWinView);
                    Converter.releaseCOMObject(docWin);
                }

                doc.ExportAsFixedFormat(outputFile, WdExportFormat.wdExportFormatPDF, false,
                                        quality, WdExportRange.wdExportAllDocument,
                                        1, 1, showMarkup, includeProps, true, bookmarks, includeTags, bitmapMissingFonts, pdfa);

                if (tmpl != null)
                {
                    tmpl.Saved = true;
                }

                object saveChanges = autosave? WdSaveOptions.wdSaveChanges : WdSaveOptions.wdDoNotSaveChanges;
                if (nowrite)
                {
                    doc.Saved = true;
                }
                normalTemplate.Saved = true;
                ((_Document)doc).Close(ref saveChanges, ref oMissing, ref oMissing);

                // Reset options
                foreach (AppOption opt in wordOptionList)
                {
                    opt.resetValue(ref wdOptions);
                }

                Converter.releaseCOMObject(wdOptions);
                Converter.releaseCOMObject(documents);
                Converter.releaseCOMObject(doc);
                Converter.releaseCOMObject(tmpl);
                Converter.releaseCOMObject(normalTemplate);

                return((int)ExitCode.Success);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                return((int)ExitCode.UnknownError);
            }
            finally
            {
                if (temporaryStorageDir != null && Directory.Exists(temporaryStorageDir))
                {
                    try
                    {
                        if (File.Exists(Path.Combine(temporaryStorageDir, Path.GetFileName(inputFile))))
                        {
                            File.Delete(Path.Combine(temporaryStorageDir, Path.GetFileName(inputFile)));
                        }
                        Directory.Delete(temporaryStorageDir);
                    }
                    catch (Exception) { }
                }
                if (word != null && !running)
                {
                    closeWordApplication(word);
                }
                Converter.releaseCOMObject(word);
            }
        }
        //</Snippet1>

        private void ThisDocument_Startup(object sender, System.EventArgs e)
        {
            try
            {
                //<Snippet2>
                comboBoxContentControl1.PlaceholderText = "Choose a title, or enter your own";
                comboBoxContentControl1.DropDownListEntries.Add("Engineer", "Engineer", 0);
                comboBoxContentControl1.DropDownListEntries.Add("Designer", "Designer", 1);
                comboBoxContentControl1.DropDownListEntries.Add("Manager", "Manager", 2);

                dropDownListContentControl1.PlaceholderText =
                    "Choose a rating (1 lowest, 3 highest)";
                dropDownListContentControl1.DropDownListEntries.Add("1", "1", 0);
                dropDownListContentControl1.DropDownListEntries.Add("2", "2", 1);
                dropDownListContentControl1.DropDownListEntries.Add("3", "3", 2);
                //</Snippet2>

                //<Snippet3>
                this.Tables[1].Range.Select();
                groupControl1 = this.Controls.AddGroupContentControl("groupControl1");
                //</Snippet3>

                //<Snippet4>
                Word.Template template1 = this.AttachedTemplate as Word.Template;

                if (template1 != null)
                {
                    object description = null;
                    template1.BuildingBlockEntries.Add("Employee Table",
                                                       Word.WdBuildingBlockTypes.wdTypeCustom1, "Employee and Customer Information",
                                                       this.Tables[1].Range, ref description, Word.WdDocPartInsertOptions.wdInsertContent);
                    template1.BuildingBlockEntries.Add("Customer Table",
                                                       Word.WdBuildingBlockTypes.wdTypeCustom1, "Employee and Customer Information",
                                                       this.Tables[2].Range, ref description, Word.WdDocPartInsertOptions.wdInsertContent);
                }
                //</Snippet4>

                //<Snippet5>
                if (!this.FormsDesign)
                {
                    this.ToggleFormsDesign();
                }
                this.Tables[2].Delete();
                this.Tables[1].Delete();
                this.ToggleFormsDesign();
                //</Snippet5>

                //<Snippet6>
                buildingBlockControl1 = this.Controls.AddBuildingBlockGalleryContentControl(
                    this.Paragraphs[1].Range, "buildingBlockControl1");
                buildingBlockControl1.BuildingBlockCategory = "Employee and Customer Information";
                buildingBlockControl1.BuildingBlockType     = Word.WdBuildingBlockTypes.wdTypeCustom1;
                buildingBlockControl1.PlaceholderText       = "Choose your first building block";

                buildingBlockControl2 = this.Controls.AddBuildingBlockGalleryContentControl(
                    this.Paragraphs[2].Range, "buildingBlockControl2");
                buildingBlockControl2.BuildingBlockCategory = "Employee and Customer Information";
                buildingBlockControl2.BuildingBlockType     = Word.WdBuildingBlockTypes.wdTypeCustom1;
                buildingBlockControl2.PlaceholderText       = "Choose your second building block";
                //</Snippet6>
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }