Пример #1
0
        private static void InvokeHelper(Word.Dialog dialog, string member, string value)
        {
            System.Type dialogType = typeof(Word.Dialog);

            // Set the appropriate property of the dialog box.
            dialogType.InvokeMember(member,
                                    System.Reflection.BindingFlags.SetProperty |
                                    System.Reflection.BindingFlags.Public |
                                    System.Reflection.BindingFlags.Instance,
                                    null, dialog, new object[] { value },
                                    System.Globalization.CultureInfo.InvariantCulture);
        }
Пример #2
0
        //</Snippet89>


        //---------------------------------------------------------------------

        private void PageSetupDialogHidden()
        {
            Word.Dialog dialog = Application.Dialogs[Word.WdWordDialog.wdDialogFilePageSetup];

            InvokeHelper(dialog, "PageWidth", "3.3\"");
            InvokeHelper(dialog, "PageHeight", "6\"");
            InvokeHelper(dialog, "TopMargin", "0.71\"");
            InvokeHelper(dialog, "BottomMargin", "0.81\"");
            InvokeHelper(dialog, "LeftMargin", "0.66\"");
            InvokeHelper(dialog, "RightMargin", "0.66\"");
            InvokeHelper(dialog, "HeaderDistance", "0.28\"");
            InvokeHelper(dialog, "Orientation", "0");
            InvokeHelper(dialog, "DifferentFirstPage", "0");
            InvokeHelper(dialog, "FirstPage", "0");
            InvokeHelper(dialog, "OtherPages", "0");

            // Apply these settings only to the current selection with this line of code:
            InvokeHelper(dialog, "ApplyPropsTo", "3");

            // Apply the settings.
            dialog.Execute();
        }
Пример #3
0
        private string CheckText(string text)
        {
            _WordDoc.SelectAllEditableRanges(ref _Missing);
            _WordApp.Selection.Text = text;


            Word.Dialog spellDialog = _WordApp.Dialogs[Word.WdWordDialog.wdDialogToolsSpellingAndGrammar];

            // Keep the dialog in front of the current process (ArcMap)
            var hwndSpellDialog = FindWindow("OpusApp", null);

            SetParent(hwndSpellDialog, System.Diagnostics.Process.GetCurrentProcess().MainWindowHandle);


            int dialogReturn = spellDialog.Show(ref _Missing);

            _CancelSpellChecking = (dialogReturn == 0 || dialogReturn == -2);


            _WordDoc.SelectAllEditableRanges(ref _Missing);
            return(_WordApp.Selection.Text);
        }
Пример #4
0
        //---------------------------------------------------------------------
        private void Test13()
        {
            //<Snippet100>
            Word.Dialog dlg = Application.Dialogs[Word.WdWordDialog.wdDialogFileOpen];
            dlg.Show();
            //</Snippet100>


            //<Snippet99>
            MessageBox.Show("Characters: " + this.Characters.Count.ToString());
            //</Snippet99>


            //<Snippet98>
            Word.Range rng = this.Content;
            rng.Select();
            //</Snippet98>


            //<Snippet97>
            Word.Cell cell = this.Tables[1].Cell(1, 1);

            cell.Range.Text = "Name";
            cell.Range.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphRight;
            //</Snippet97>


            //<Snippet96>
            this.Tables[1].Columns.Add(this.Tables[1].Columns[1]);
            this.Tables[1].Columns.DistributeWidth();
            //</Snippet96>


            //<Snippet95>
            this.Tables[1].Rows.Add(this.Tables[1].Rows[1]);
            //</Snippet95>


            //<Snippet88>
            this.Tables[1].Range.Font.Size = 8;
            this.Tables[1].set_Style("Table Grid 8");
            //</Snippet88>


            //<Snippet87>
            Word.Table newTable = this.Tables[1];
            //</Snippet87>


            //<Snippet86>
            object start = 0;
            object end   = 0;

            Word.Range tableLocation = this.Range(ref start, ref end);
            this.Tables.Add(tableLocation, 3, 4);
            //</Snippet86>


            //<Snippet77>
            Application.Selection.Find.ClearFormatting();
            Application.Selection.Find.Execute("find me", Forward: true, Wrap: Word.WdFindWrap.wdFindContinue);
            //</Snippet77>
        }
Пример #5
0
        //---------------------------------------------------------------------
        private void Test14()
        {
            //<Snippet114>
            foreach (Word.Section wordSection in this.Sections)
            {
                Word.Range footerRange = wordSection.Footers[Word.WdHeaderFooterIndex.wdHeaderFooterPrimary].Range;
                footerRange.Font.ColorIndex = Word.WdColorIndex.wdDarkRed;
                footerRange.Font.Size       = 20;
                footerRange.Text            = "Confidential";
            }
            //</Snippet114>


            //<Snippet113>
            string result = "Spelled incorrectly.";

            object startLocation = this.Content.Start;
            object endLocation   = this.Content.End;
            bool   spellCheck    = this.Application.CheckSpelling(
                this.Range(ref startLocation, ref endLocation).Text);

            if (spellCheck == true)
            {
                result = "Spelled correctly.";
            }

            MessageBox.Show(result);
            //</Snippet113>


            //<Snippet111>
            object noReset          = false;
            object password         = System.String.Empty;
            object useIRM           = false;
            object enforceStyleLock = false;

            this.Protect(Word.WdProtectionType.wdAllowOnlyReading,
                         ref noReset, ref password, ref useIRM, ref enforceStyleLock);
            //</Snippet111>

            //<Snippet112>
            this.bookmark1.Range.Editors.Add(Word.WdEditorType.wdEditorEveryone);
            //</Snippet112>


            //<Snippet109>
            float leftPosition = (float)this.Application.Selection.Information[
                Word.WdInformation.wdHorizontalPositionRelativeToPage];

            float topPosition = (float)this.Application.Selection.Information[
                Word.WdInformation.wdVerticalPositionRelativeToPage];

            //</Snippet109>


            //<Snippet110>
            this.Shapes.AddTextEffect(Office.MsoPresetTextEffect.msoTextEffect29, "SampleText",
                                      "Arial Black", 24, Office.MsoTriState.msoFalse, Office.MsoTriState.msoFalse,
                                      leftPosition, topPosition);
            //</Snippet110>


            //<Snippet108>
            this.Application.Selection.InlineShapes.AddPicture(@"C:\SamplePicture.jpg");
            //</Snippet108>


            //<Snippet107>
            HideText(bookmark1.Range);

            object oTrue    = true;
            object oFalse   = false;
            object range    = Word.WdPrintOutRange.wdPrintAllDocument;
            object items    = Word.WdPrintOutItem.wdPrintDocumentContent;
            object copies   = "1";
            object pages    = "";
            object pageType = Word.WdPrintOutPages.wdPrintAllPages;

            this.PrintOut(
                ref oTrue, ref oFalse, ref range, ref missing, ref missing, ref missing,
                ref items, ref copies, ref pages, ref pageType, ref oFalse, ref oTrue,
                ref missing, ref oFalse, ref missing, ref missing, ref missing, ref missing);

            UnhideText(bookmark1.Range);
            //</Snippet107>



            Word.Dialog dialog     = Application.Dialogs[Word.WdWordDialog.wdDialogFileOpen];
            System.Type dialogType = typeof(Word.Dialog);

            // Set the Name property of the dialog box.
            dialogType.InvokeMember("Name",
                                    System.Reflection.BindingFlags.SetProperty |
                                    System.Reflection.BindingFlags.Public |
                                    System.Reflection.BindingFlags.Instance,
                                    null, dialog, new object[] { "Testing" },
                                    System.Globalization.CultureInfo.InvariantCulture);

            // Display the dialog box.
            dialog.Show(ref missing);

            // Show the Name property.
            MessageBox.Show(dialogType.InvokeMember("Name",
                                                    System.Reflection.BindingFlags.GetProperty |
                                                    System.Reflection.BindingFlags.Public |
                                                    System.Reflection.BindingFlags.Instance,
                                                    null, dialog, null,
                                                    System.Globalization.CultureInfo.InvariantCulture).ToString());
        }
Пример #6
0
 public void InsertImage()
 {
     Word.Dialog dlg = Globals.ThisAddIn.Application.Dialogs[Word.WdWordDialog.wdDialogInsertPicture];
     dlg.Show();
 }
Пример #7
0
        /// <summary>
        /// 批量打印
        /// </summary>
        public void printFiles()
        {
            var wordFiles = new List <object>();
            var docs      = new List <Word._Document>();

            foreach (string s in listWord)
            {
                if (s != "")
                {
                    wordFiles.Add(s);
                }
                //wordFiles.Add(@"E:\testPrint2.doc");
                //wordFiles.Add(@"E:\testPrint3.doc");
            }
            object oMissing = System.Reflection.Missing.Value;

            Word._Application app = new Word.Application();
            try
            {
                //Document doc = app.Documents.Add(ref wordFile, ref oMissing, ref oMissing, ref oMissing);
                Word.Dialog dialog = null;
                foreach (var wordFile in wordFiles)
                {
                    object         file = wordFile;
                    Word._Document doc  = app.Documents.Open(ref file, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
                                                             ref oMissing, ref oMissing, ref oMissing,
                                                             ref oMissing, ref oMissing, ref oMissing, ref oMissing,
                                                             ref oMissing, ref oMissing, ref oMissing, ref oMissing);
                    docs.Add(doc);
                    //doc.Activate();
                    if (dialog == null)
                    {
                        dialog = app.Dialogs[Word.WdWordDialog.wdDialogFilePrint];
                        //第一次弹出打印设置窗体单击确定默认就会打印了。
                        //所以这里就判断flag为0,就单击的是取消,就退出;否则就默认打印了
                        int flag = dialog.Show(ref oMissing);
                        if (flag == 0)
                        {
                            break;
                        }
                        //if ( flag == 1)
                        //{
                        //    dialog.Execute();
                        //    doc.PrintOut(ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
                        //                 ref oMissing, ref oMissing, ref oMissing, ref oMissing,
                        //                 ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
                        //                 ref oMissing, ref oMissing);
                        //}
                    }
                    else
                    {
                        dialog.Execute();
                        //doc.PrintOut(ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
                        //             ref oMissing, ref oMissing, ref oMissing, ref oMissing,
                        //             ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
                        //             ref oMissing, ref oMissing);
                    }
                    System.Threading.Thread.Sleep(1000 * listWord.Count);
                }
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
            finally
            {
                object saveChange = Microsoft.Office.Interop.Word.WdSaveOptions.wdDoNotSaveChanges;
                foreach (var doc in docs)
                {
                    if (doc != null)
                    {
                        doc.Close(ref saveChange, ref oMissing, ref oMissing);
                    }
                }
                app.Quit(ref oMissing, ref oMissing, ref oMissing);
            }
        }
Пример #8
0
        private void findTemplates(string searchPath, string newTemplatePath, string searchString, bool replaceTemplates, bool hideWord)
        {
            string templatelogfile       = "found_templates.txt";
            string pwprotectedlogfile    = "password_protected_documents.txt";
            string errordocumentslogfile = "error_documents.txt";
            string headertemplatelog     = "Dokumentename;Alter Template Pfad";
            string headererrordocuments  = "Dokumente die Fehler verursachten";
            string headerpwprotected     = "Dokumente die Passwortgeschützt sind";
            string logrootdirectory      = "C:\\_HUWIIT\\";
            string logfoldername         = "logs";
            string logLine;

            string[] filepaths;

            if (!Directory.Exists(logrootdirectory + logfoldername))
            {
                Directory.CreateDirectory(logrootdirectory + logfoldername);
            }
            string[] extensions = { ".doc", ".docx" };
            filepaths = Directory.GetFiles(searchPath, "*.*", SearchOption.AllDirectories).Where(f => extensions.Contains(System.IO.Path.GetExtension(f).ToLower())).ToArray();
            this.Dispatcher.Invoke(() => StatusBar.Maximum = filepaths.Length);
            var wordApp = new Word.Application();

            wordApp.Visible = hideWord;

            int iteration = 0;

            foreach (string path in filepaths)
            {
                iteration++;
                Dispatcher.Invoke(() => StatusBar.Value = iteration);
                var checkprotection = MsOfficeHelper.IsProtected(path);
                if (checkprotection == true)
                {
                    logLine = path;
                    WriteLog(logLine, pwprotectedlogfile, headerpwprotected);
                }

                else if (checkprotection == false)
                {
                    string fileName = System.IO.Path.GetFileName(path);
                    if (fileName.StartsWith("~"))
                    {
                    }

                    else
                    {
                        try
                        {
                            var         document        = wordApp.Documents.Open(path, AddToRecentFiles: false);
                            Word.Dialog dlg             = wordApp.Dialogs[Word.WdWordDialog.wdDialogDocumentStatistics];
                            var         oldTemplatePath = VB.Interaction.CallByName(dlg, "Template", VB.CallType.Get, null).ToString();
                            //MessageBox.Show(searchString);
                            if (CheckTemplatePaths(oldTemplatePath, searchString) && replaceTemplates == true)
                            {
                                document.set_AttachedTemplate(newTemplatePath);
                                document.Save();
                                logLine = String.Format("{0};{1};{2}", path, oldTemplatePath, newTemplatePath);
                                WriteLog(logLine, templatelogfile, headertemplatelog);
                                Dispatcher.Invoke(() => { documents.Add(new DocumentList()
                                    {
                                        DocumentPath = path, NewTemplate = newTemplatePath, OldTemplate = oldTemplatePath
                                    }); });
                            }

                            else
                            {
                                logLine = String.Format("{0};{1}", path, oldTemplatePath);
                                WriteLog(logLine, templatelogfile, headertemplatelog);
                                Dispatcher.Invoke(() => { documents.Add(new DocumentList()
                                    {
                                        DocumentPath = path, NewTemplate = "nicht ersetzt", OldTemplate = oldTemplatePath
                                    }); });
                            }
                            document.Close();
                        }

                        catch
                        {
                            logLine = String.Format("{0}", path);
                            WriteLog(logLine, errordocumentslogfile, headererrordocuments);
                        }
                    }
                }
            }

            wordApp.Quit();
            Dispatcher.Invoke(() => { searchFolderForDocuments.IsEnabled = true; exportDataGridToExcel.IsEnabled = true; });
        }
Пример #9
0
        //---------------------------------------------------------------------
        private void Test14()
        {
            //<Snippet114>
            foreach (Word.Section wordSection in this.Application.ActiveDocument.Sections)
            {
                Word.Range footerRange = wordSection.Footers[Word.WdHeaderFooterIndex.wdHeaderFooterPrimary].Range;
                footerRange.Font.ColorIndex = Word.WdColorIndex.wdDarkRed;
                footerRange.Font.Size       = 20;
                footerRange.Text            = "Confidential";
            }
            //</Snippet114>


            //<Snippet113>
            Word.Document document1  = this.Application.ActiveDocument;
            string        result     = "Spelled incorrectly.";
            bool          spellCheck = this.Application.CheckSpelling(
                document1.Range(document1.Content.Start, document1.Content.End).Text);

            if (spellCheck == true)
            {
                result = "Spelled correctly.";
            }

            MessageBox.Show(result);
            //</Snippet113>


            //<Snippet111>
            this.Application.ActiveDocument.Protect(
                Word.WdProtectionType.wdAllowOnlyReading,
                false, System.String.Empty, false, false);
            //</Snippet111>

            // The following lines are left out of the snippet on purpose.
            Word.Bookmark bookmark1 = this.Application.ActiveDocument.Bookmarks.Add(
                "bookmark1", document1.Paragraphs[1].Range);
            //<Snippet112>
            bookmark1.Range.Editors.Add(Word.WdEditorType.wdEditorEveryone);
            //</Snippet112>


            //<Snippet109>
            float leftPosition = (float)this.Application.Selection.Information[
                Word.WdInformation.wdHorizontalPositionRelativeToPage];

            float topPosition = (float)this.Application.Selection.Information[
                Word.WdInformation.wdVerticalPositionRelativeToPage];

            //</Snippet109>


            //<Snippet110>
            this.Application.ActiveDocument.Shapes.AddTextEffect(
                Office.MsoPresetTextEffect.msoTextEffect29, "SampleText",
                "Arial Black", 24, Office.MsoTriState.msoFalse,
                Office.MsoTriState.msoFalse, leftPosition, topPosition);
            //</Snippet110>


            //<Snippet108>
            this.Application.Selection.InlineShapes.AddPicture(@"C:\SamplePicture.jpg");
            //</Snippet108>

            //<Snippet107>
            HideText(bookmark1.Range);

            this.Application.ActiveDocument.PrintOut(true, false, Word.WdPrintOutRange.wdPrintAllDocument,
                                                     Item: Word.WdPrintOutItem.wdPrintDocumentContent, Copies: "1", Pages: "",
                                                     PageType: Word.WdPrintOutPages.wdPrintAllPages, PrintToFile: false, Collate: true,
                                                     ManualDuplexPrint: false);

            UnhideText(bookmark1.Range);
            //</Snippet107>


            //<Snippet102>
            Word.Dialog dlg     = Application.Dialogs[Word.WdWordDialog.wdDialogFileOpen];
            System.Type dlgType = typeof(Word.Dialog);

            // Set the Name property of the dialog box.
            dlgType.InvokeMember("Name",
                                 System.Reflection.BindingFlags.SetProperty |
                                 System.Reflection.BindingFlags.Public |
                                 System.Reflection.BindingFlags.Instance,
                                 null, dlg, new object[] { "Testing" },
                                 System.Globalization.CultureInfo.InvariantCulture);
            //</Snippet102>


            //<Snippet103>
            // Display the dialog box.
            dlg.Show(ref missing);

            // Show the Name property.
            MessageBox.Show(dlgType.InvokeMember("Name",
                                                 System.Reflection.BindingFlags.GetProperty |
                                                 System.Reflection.BindingFlags.Public |
                                                 System.Reflection.BindingFlags.Instance,
                                                 null, dlg, null,
                                                 System.Globalization.CultureInfo.InvariantCulture).ToString());
            //</Snippet103>
        }