Exemplo n.º 1
0
        // ---------------------------------------------
        //             Open Document
        // ---------------------------------------------
        public static void OpenDocument(object fromFileName, object vkReadOnly)
        {
            if (!File.Exists((string)fromFileName))
            {
                MessageBox.Show("File not found. " + fromFileName);
                return;
            }


            Word.Application vkWordApp =
                new Word.Application();

            // object vkReadOnly = false;
            object vkVisible = true;
            object vkFalse   = false;
            object vkTrue    = true;
            object vkDynamic = 2;


            object vkMissing = System.Reflection.Missing.Value;

            // Let's make the word application visible
            vkWordApp.Visible = true;
            vkWordApp.Activate();

            // Let's open the document
            Word.Document vkMyDoc = vkWordApp.Documents.Open(
                ref fromFileName, ref vkMissing, ref vkReadOnly,
                ref vkMissing, ref vkMissing, ref vkMissing,
                ref vkMissing, ref vkMissing, ref vkMissing,
                ref vkMissing, ref vkMissing, ref vkVisible);

            return;
        }
Exemplo n.º 2
0
        // ---------------------------------------------
        //             Copy Documents
        // ---------------------------------------------
        public static object CopyDocument(
            object fromFileName,
            object destinationFileName,
            List <WordDocumentTasks.TagStructure> tag
            )
        {
            Word.Application vkWordApp =
                new Word.Application();

            object saveFile = destinationFileName;

            object vkReadOnly = false;
            object vkVisible  = true;
            object vkFalse    = false;
            object vkTrue     = true;
            object vkDynamic  = 2;

            object vkMissing = System.Reflection.Missing.Value;

            // Let's make the word application visible
            vkWordApp.Visible = true;
            vkWordApp.Activate();

            // Let's copy the document
            File.Copy(fromFileName.ToString(), destinationFileName.ToString(), true);

            // Let's open the DESTINATION document
            Word.Document vkMyDoc = vkWordApp.Documents.Open(
                ref destinationFileName, ref vkMissing, ref vkReadOnly,
                ref vkMissing, ref vkMissing, ref vkMissing,
                ref vkMissing, ref vkMissing, ref vkMissing,
                ref vkMissing, ref vkMissing, ref vkVisible);

            foreach (var t in tag)
            {
                FindAndReplace(t.Tag, t.TagValue, 1, vkWordApp, vkMyDoc);
            }

            vkMyDoc.Save();

            // close the new document
            vkMyDoc.Close(ref vkFalse, ref vkMissing, ref vkMissing);

            // close word application
            vkWordApp.Quit(ref vkFalse, ref vkMissing, ref vkMissing);

            return(saveFile);
        }
Exemplo n.º 3
0
        /*
         *
         * http://www.c-sharpcorner.com/UploadFile/amrish_deep/WordAutomation05102007223934PM/WordAutomation.aspx
         * 9.1 Embedding Pictures in Document Header:
         *
         * //EMBEDDING LOGOS IN THE DOCUMENT
         * //SETTING FOCUES ON THE PAGE HEADER TO EMBED THE WATERMARK
         * oWord.ActiveWindow.ActivePane.View.SeekView = Word.WdSeekView.wdSeekCurrentPageHeader;
         *
         * //THE LOGO IS ASSIGNED TO A SHAPE OBJECT SO THAT WE CAN USE ALL THE
         * //SHAPE FORMATTING OPTIONS PRESENT FOR THE SHAPE OBJECT
         * Word.Shape logoCustom = null;
         *
         * //THE PATH OF THE LOGO FILE TO BE EMBEDDED IN THE HEADER
         * String logoPath = "C:\\Document and Settings\\MyLogo.jpg";
         * logoCustom = oWord.Selection.HeaderFooter.Shapes.AddPicture(logoPath,
         * ref oFalse, ref oTrue, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);
         *
         * logoCustom.Select(ref oMissing);
         * logoCustom.Name = "CustomLogo";
         * logoCustom.Left = (float)Word.WdShapePosition.wdShapeLeft;
         *
         * //SETTING FOCUES BACK TO DOCUMENT
         * oWord.ActiveWindow.ActivePane.View.SeekView = Word.WdSeekView.wdSeekMainDocument;
         *
         */


        public static void CompareDocuments(object fromFileName, string toFileName)
        {
            if (!File.Exists((string)fromFileName))
            {
                MessageBox.Show("File not found. " + fromFileName);
                return;
            }


            Word.Application vkWordApp =
                new Word.Application();

            object vkReadOnly = false;
            object vkVisible  = true;
            object vkFalse    = false;
            object vkTrue     = true;
            object vkDynamic  = 2;


            object vkMissing = System.Reflection.Missing.Value;

            // Let's make the word application visible
            vkWordApp.Visible = true;
            vkWordApp.Activate();

            // Let's open the document
            Word.Document vkMyDoc = vkWordApp.Documents.Open(
                ref fromFileName, ref vkMissing, ref vkReadOnly,
                ref vkMissing, ref vkMissing, ref vkMissing,
                ref vkMissing, ref vkMissing, ref vkMissing,
                ref vkMissing, ref vkMissing, ref vkVisible);

            vkMyDoc.Compare(toFileName);

            vkMyDoc.Close();
            System.Runtime.InteropServices.Marshal.ReleaseComObject(vkMyDoc);

            vkWordApp.Quit();
            System.Runtime.InteropServices.Marshal.ReleaseComObject(vkWordApp);

            return;
        }
Exemplo n.º 4
0
        // ---------------------------------------------
        //             Open Document
        // ---------------------------------------------
        public static ResponseStatus OpenDocument(object fromFileName, object vkReadOnly, bool isFromWeb = false)
        {
            if (!isFromWeb)
            {
                if (!File.Exists((string)fromFileName))
                {
                    ResponseStatus rserror = new ResponseStatus(MessageType.Error);
                    rserror.Message = "File not found. " + fromFileName;
                    MessageBox.Show(rserror.Message);
                    return(rserror);
                }
            }

            Word.Application vkWordApp =
                new Word.Application();

            // object vkReadOnly = false;
            object vkVisible = true;
            object vkFalse   = false;
            object vkTrue    = true;
            object vkDynamic = 2;


            object vkMissing = System.Reflection.Missing.Value;

            // Let's make the word application visible
            vkWordApp.Visible = true;
            vkWordApp.Activate();

            // Let's open the document
            Word.Document vkMyDoc = vkWordApp.Documents.Open(
                ref fromFileName, ref vkMissing, ref vkReadOnly,
                ref vkMissing, ref vkMissing, ref vkMissing,
                ref vkMissing, ref vkMissing, ref vkMissing,
                ref vkMissing, ref vkMissing, ref vkVisible);

            return(new ResponseStatus());
        }
Exemplo n.º 5
0
                /// <summary>
                /// 在WPS2016中打开指定路径的文档
                /// </summary>
                /// <param name="strFilePath">文件路径</param>
                public void OpenWpsFile(string strFilePath)
        {
            try
            {
                Word.Application wordApp = new Word.Application();//应用对象 
                                wordApp.NormalTemplate.Saved = true;
                object        fileName           = strFilePath;
                object        confirmConversions = Type.Missing;
                object        readOnly           = false;
                object        addToRecentFiles   = Type.Missing;
                object        passwordDoc        = Type.Missing;
                object        passwordTemplate   = Type.Missing;
                object        revert             = Type.Missing;
                object        writepwdoc         = Type.Missing;
                object        writepwTemplate    = Type.Missing;
                object        format             = Type.Missing;
                object        encoding           = Type.Missing;
                object        visible            = Type.Missing;
                object        openRepair         = Type.Missing;
                object        docDirection       = Type.Missing;
                object        notEncoding        = Type.Missing;
                object        xmlTransform       = Type.Missing;
                Word.Document doc = wordApp.Documents.Open(
                    ref fileName, ref confirmConversions, ref readOnly, ref addToRecentFiles,
                    ref passwordDoc, ref passwordTemplate, ref revert, ref writepwdoc,
                    ref writepwTemplate, ref format, ref encoding, ref visible, ref openRepair,
                    ref docDirection, ref notEncoding, ref xmlTransform);

                wordApp.Visible = true;
                wordApp.Activate();//激活文档使文档为当前处理  
                           
            }
            catch (Exception ex)
            {
                MessageBox.Show("打开文件时出错:" + ex);
            }
        }
Exemplo n.º 6
0
        // ---------------------------------------------
        //             Copy Documents
        // ---------------------------------------------
        public static object CopyDocumentReplaceContents(
            object fromFileName,
            object destinationFileName,
            List <WordDocumentTasks.TagStructure> tag
            )
        {
            Word.Application vkWordApp =
                new Word.Application();

            object saveFile = destinationFileName;

            object vkReadOnly = false;
            object vkVisible  = true;
            object vkFalse    = false;
            object vkTrue     = true;
            object vkDynamic  = 2;


            object vkMissing = System.Reflection.Missing.Value;

            // Let's make the word application visible
            vkWordApp.Visible = true;
            vkWordApp.Activate();

            // Let's open the document
            Word.Document vkMyDoc = vkWordApp.Documents.Open(
                ref fromFileName, ref vkMissing, ref vkReadOnly,
                ref vkMissing, ref vkMissing, ref vkMissing,
                ref vkMissing, ref vkMissing, ref vkMissing,
                ref vkMissing, ref vkMissing, ref vkVisible);

            // Let's create a new document
            Word.Document vkNewDoc = vkWordApp.Documents.Add(
                ref vkMissing, ref vkMissing,
                ref vkMissing, ref vkVisible);

            // Select and Copy from the original document
            vkMyDoc.Select();
            vkWordApp.Selection.Copy();

            // Paste into new document as unformatted text
            vkNewDoc.Select();
            vkWordApp.Selection.PasteSpecial(ref vkMissing, ref vkFalse,
                                             ref vkMissing, ref vkFalse, ref vkDynamic,
                                             ref vkMissing, ref vkMissing);

            // Save the new document
            vkNewDoc.SaveAs(ref saveFile, ref vkMissing,
                            ref vkMissing, ref vkMissing, ref vkMissing,
                            ref vkMissing, ref vkMissing, ref vkMissing,
                            ref vkMissing, ref vkMissing, ref vkMissing);


            // Copy elements
            // FromString => toString
            //
            // underdevelopment
            //
            vkNewDoc.Select();

            foreach (var t in tag)
            {
                FindAndReplace(t.Tag, t.TagValue, 1, vkWordApp, vkMyDoc);
            }

            vkNewDoc.Save();

            // close the new document
            vkNewDoc.Close(ref vkFalse, ref vkMissing, ref vkMissing);

            // close the original document
            vkMyDoc.Close(ref vkFalse, ref vkMissing, ref vkMissing);

            // close word application
            vkWordApp.Quit(ref vkFalse, ref vkMissing, ref vkMissing);

            return(saveFile);
        }
Exemplo n.º 7
0
        private void CheckSpelling()
        {
            TextDocumentHandler handler = new TextDocumentHandler(this.dte);

            if (handler.HasNonEmptySelection)
            {
                try
                {
                    // Launch Word.
                    Word._Application wordApp = new Word.Application();

                    // Add a document.
                    Word._Document wordDoc = wordApp.Documents.Add();

                    // Clear current contents.
                    Word.Range range = wordApp.Selection.Range;
                    range.WholeStory();
                    range.Delete();
                    range = null;

                    // Add the text the user selected.
                    wordApp.Selection.Text = handler.SelectedText;

                    // Show it
                    wordApp.Visible = true;
                    wordApp.Activate();
                    wordDoc.Activate();

                    // Check spelling
                    wordDoc.CheckSpelling();

                    // Get the edited text back
                    wordApp.Selection.WholeStory();
                    string newText = wordApp.Selection.Text;

                    // Word always adds an extra CR, so strip that off.
                    // Also it converts all LFs to CRs, so change
                    // that back.
                    if (!string.IsNullOrEmpty(newText))
                    {
                        if (newText.EndsWith("\r"))
                        {
                            newText = newText.Substring(0, newText.Length - 1);
                        }

                        newText = newText.Replace("\r", "\r\n");
                    }

                    handler.SetSelectedTextIfUnchanged(newText, "Check Spelling");

                    // Tell the doc and Word to go away.
                    object saveChanges = false;
                    wordDoc.Close(ref saveChanges);
                    wordApp.Visible = false;
                    wordApp.Quit();
                }
                catch (COMException ex)
                {
                    // If we get REGDB_E_CLASSNOTREG, then Word probably isn't installed.
                    const uint REGDB_E_CLASSNOTREG = 0x80040154;
                    if (unchecked ((uint)ex.ErrorCode) == REGDB_E_CLASSNOTREG)
                    {
                        this.package.ShowMessageBox(
                            "Microsoft Word is required in order to check spelling, but it isn't available.\r\n\r\nDetails:\r\n" + ex.Message,
                            true);
                    }
                    else
                    {
                        throw;
                    }
                }
            }
        }