Ejemplo n.º 1
0
        public void InsertBT()
        {
            object missing = System.Reflection.Missing.Value;

            Word._Application app  = new Word.Application();
            object            path = RandomPath;

            //object fileName = @"\tmp\show.doc";
            // Word.Document doc = app.Documents.Open(ref fileName, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);

            //doc.Activate();
            Word.Document doc = app.Documents.Add(ref missing, ref missing, ref missing, ref missing);
            doc.Activate();
            Word.Range range = doc.Paragraphs.Last.Range;                                       //定位到段尾
            range.Text = "脚手架专项施工方案";
            range.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter; //文字居中
            //object styleName = "标题 2";
            // range.set_Style(ref styleName);
            range.Font.Size = 20;
            range.Font.Name = "黑体";
            range.Font.Bold = 1;

            doc.SaveAs(ref path, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);
            app.Quit(ref missing, ref missing, ref missing);
            InsertFile(path.ToString());

            //range.InsertParagraphAfter();
        }
Ejemplo n.º 2
0
        // Open a new document
        public void Open( )
        {
            object missing = System.Reflection.Missing.Value;

            oDoc = oWordApplic.Documents.Add(ref missing, ref missing, ref missing, ref missing);

            oDoc.Activate();
        }
        // Open a file (the file must exists) and activate it
        public void Open(string strFileName)
        {
            object fileName  = strFileName;
            object readOnly  = false;
            object isVisible = true;
            object missing   = System.Reflection.Missing.Value;

            oDoc = oWordApplic.Documents.Open(ref fileName, ref missing, ref readOnly,
                                              ref missing, ref missing, ref missing, ref missing, ref missing, ref missing,
                                              ref missing, ref missing, ref isVisible, ref missing, ref missing, ref missing, ref missing);

            oDoc.Activate();
        }
Ejemplo n.º 4
0
        /*
         * Activates a word document.
         */
        public void ActivateDocument(Document slDoc, ProjectServerConnection psc)
        {
            Word.Document wdDoc = null;
            try
            {
                Document slDocDeep = psc.DocumentManager.getDocumentDeep(slDoc.id);

                // Todo: I think part of the problem here is opening one document in word,
                // overwriting that document (FHANDLE, ETC)  and reloading as a new doc
                // with the existing one still open.
                //
                // This should be testable by uploading multiple documents to RoundTable
                // and opening each one.
                FileStream   fs = new FileStream(slDocDeep.filename, FileMode.Create);
                BinaryWriter bw = new BinaryWriter(fs);
                bw.Write(slDocDeep.document, 0, slDocDeep.document.Length);
                bw.Flush();
                bw.Close();
                fs.Close();                 // Already done by bw?

                // Activate the RoundTable Document.
                object missing  = Type.Missing;
                object filepath = Path.GetFullPath(slDocDeep.filename);
                wdDoc = wordApp.Documents.Open(ref filepath,
                                               ref missing, ref missing, ref missing, ref missing,
                                               ref missing, ref missing, ref missing, ref missing,
                                               ref missing, ref missing, ref missing, ref missing,
                                               ref missing, ref missing, ref missing);

                wdDoc.Activate();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                try
                {
                    Marshal.ReleaseComObject(wdDoc);
                }
                catch (Exception ex)
                {
                }
            }
        }
Ejemplo n.º 5
0
        public void InsertFile(string path)
        {
            Word.ApplicationClass applicationClass = new Word.ApplicationClass();
            object fileName = showPath;

            Word.Document doct = applicationClass.Documents.Open(ref fileName, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);
            doct.Activate();
            object confirmConversion = false;
            object link       = false;
            object attachment = false;

            applicationClass.Selection.InsertFile(path, ref missing, ref confirmConversion, ref link, ref attachment);
            object pBreak = (int)Word.WdBreakType.wdPageBreak;

            applicationClass.Selection.InsertBreak(ref pBreak);
            doct.SaveAs(ref fileName, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);
            ((Word._Document)doct).Close(ref missing, ref missing, ref missing);
        }
Ejemplo n.º 6
0
        //// ----------------------------------------------------
        ////         Find and replace words in MS Word
        //// ----------------------------------------------------
        //public static void FindAndReplaceFIRST(
        //                           object vkFind,
        //                           object vkReplace,
        //                           object vkNum,
        //            Word.Application vkWordApp,
        //                    Word.Document vkMyDoc
        //    )
        //{
        //    object vkReadOnly = false;
        //    object vkVisible = true;
        //    object vkFalse = false;
        //    object missing = false;
        //    object vkTrue = true;
        //    object vkDynamic = 2;
        //    object replaceAll = Word.WdReplace.wdReplaceAll;

        //    if (vkMyDoc == null)
        //    {
        //        return;
        //    }
        //    if (vkWordApp == null)
        //    {
        //        return;
        //    }
        //    if (vkReplace.ToString().Trim().Length == 0)
        //    {
        //        return;
        //    }
        //    if (vkFind.ToString().Trim().Length == 0)
        //    {
        //        return;
        //    }


        //    // Replace Word Document body
        //    //

        //    // 05/09/2010 - Testando a passagem de paramentros com nome do parametro... nao remover codigo abaixo.

        //    // Working... Forward = false;

        //    //vkWordApp.Selection.Find.Execute(
        //    //    ref vkFind, ref vkFalse, ref vkFalse,
        //    //    ref vkFalse, ref vkFalse, ref vkFalse,
        //    //    ref vkTrue, ref vkNum, ref vkFalse,
        //    //    ref vkReplace, ref vkDynamic, ref vkFalse,
        //    //    ref vkFalse, ref vkFalse, ref vkFalse );

        //    //vkWordApp.Selection.Find.Execute(
        //    //          FindText: vkFind,
        //    //          ReplaceWith: vkReplace,
        //    //          MatchCase: vkFalse,
        //    //          MatchWholeWord: vkTrue,
        //    //          MatchAllWordForms: vkTrue,
        //    //          Replace: vkDynamic);

        //    vkWordApp.Selection.Find.Execute(
        //        MatchCase: vkFalse,
        //        MatchWholeWord: vkTrue,
        //        MatchWildcards: vkFalse,
        //        MatchSoundsLike: vkFalse,
        //        MatchAllWordForms: vkFalse,
        //        Forward: vkFalse,
        //        Wrap: vkNum,
        //        Format: vkFalse,
        //        FindText: vkFind,
        //        ReplaceWith: vkReplace,
        //        Replace: vkDynamic,
        //        MatchKashida: vkFalse,
        //        MatchDiacritics: vkFalse,
        //        MatchAlefHamza: vkFalse,
        //        MatchControl: vkFalse );

        //    // Replace in the primary header/ footer
        //    //
        //    //vkMyDoc.Sections.Item(1).Headers.Item(Word.WdHeaderFooterIndex.wdHeaderFooterPrimary).Range.Find.Execute(
        //    //    ref vkFind, ref vkFalse, ref vkFalse,
        //    //    ref vkFalse, ref vkFalse, ref vkFalse,
        //    //    ref vkTrue, ref vkNum, ref vkFalse,
        //    //    ref vkReplace, ref vkDynamic, ref vkFalse,
        //    //    ref vkFalse, ref vkFalse, ref vkFalse);

        //    vkMyDoc.Sections.Item( 1 ).Headers.Item( Word.WdHeaderFooterIndex.wdHeaderFooterPrimary ).Range.Find.Execute(
        //        FindText: vkFind,
        //        MatchCase: vkFalse,
        //        MatchWholeWord: vkFalse,
        //        MatchWildcards: vkFalse,
        //        MatchSoundsLike: vkFalse,
        //        MatchAllWordForms: vkFalse,
        //        Forward: vkFalse,
        //        Wrap: vkNum,
        //        Format: vkFalse,
        //        ReplaceWith: vkReplace,
        //        Replace: vkDynamic,
        //        MatchKashida: vkFalse,
        //        MatchDiacritics: vkFalse,
        //        MatchAlefHamza: vkFalse,
        //        MatchControl: vkFalse );

        //    vkMyDoc.Sections.Item( 1 ).Headers.Item( Word.WdHeaderFooterIndex.wdHeaderFooterFirstPage).Range.Find.Execute(
        //        FindText: vkFind,
        //        MatchCase: vkFalse,
        //        MatchWholeWord: vkFalse,
        //        MatchWildcards: vkFalse,
        //        MatchSoundsLike: vkFalse,
        //        MatchAllWordForms: vkFalse,
        //        Forward: vkFalse,
        //        Wrap: vkNum,
        //        Format: vkFalse,
        //        ReplaceWith: vkReplace,
        //        Replace: vkDynamic,
        //        MatchKashida: vkFalse,
        //        MatchDiacritics: vkFalse,
        //        MatchAlefHamza: vkFalse,
        //        MatchControl: vkFalse );

        //    vkMyDoc.Sections.Item( 1 ).Headers.Item( Word.WdHeaderFooterIndex.wdHeaderFooterEvenPages).Range.Find.Execute(
        //        FindText: vkFind,
        //        MatchCase: vkFalse,
        //        MatchWholeWord: vkFalse,
        //        MatchWildcards: vkFalse,
        //        MatchSoundsLike: vkFalse,
        //        MatchAllWordForms: vkFalse,
        //        Forward: vkFalse,
        //        Wrap: vkNum,
        //        Format: vkFalse,
        //        ReplaceWith: vkReplace,
        //        Replace: vkDynamic,
        //        MatchKashida: vkFalse,
        //        MatchDiacritics: vkFalse,
        //        MatchAlefHamza: vkFalse,
        //        MatchControl: vkFalse );


        //    // Replace in the first page footer
        //    //
        //    //vkMyDoc.Sections.Item( 1 ).Footers.Item( Word.WdHeaderFooterIndex.wdHeaderFooterPrimary).Range.Find.Execute(
        //    //    ref vkFind, ref vkFalse, ref vkFalse,
        //    //    ref vkFalse, ref vkFalse, ref vkFalse,
        //    //    ref vkTrue, ref vkNum, ref vkFalse,
        //    //    ref vkReplace, ref vkDynamic, ref vkFalse,
        //    //    ref vkFalse, ref vkFalse, ref vkFalse );


        //    vkMyDoc.Sections.Item( 1 ).Footers.Item( Word.WdHeaderFooterIndex.wdHeaderFooterPrimary).Range.Find.Execute(
        //        FindText: vkFind,
        //        MatchCase: vkFalse,
        //        MatchWholeWord: vkFalse,
        //        MatchWildcards: vkFalse,
        //        MatchSoundsLike: vkFalse,
        //        MatchAllWordForms: vkFalse,
        //        Forward: vkFalse,
        //        Wrap: vkNum,
        //        Format: vkFalse,
        //        ReplaceWith: vkReplace,
        //        Replace: vkDynamic,
        //        MatchKashida: vkFalse,
        //        MatchDiacritics: vkFalse,
        //        MatchAlefHamza: vkFalse,
        //        MatchControl: vkFalse );


        //    vkMyDoc.Sections.Item( 1 ).Footers.Item( Word.WdHeaderFooterIndex.wdHeaderFooterFirstPage ).Range.Find.Execute(
        //        FindText: vkFind,
        //        MatchCase: vkFalse,
        //        MatchWholeWord: vkFalse,
        //        MatchWildcards: vkFalse,
        //        MatchSoundsLike: vkFalse,
        //        MatchAllWordForms: vkFalse,
        //        Forward: vkFalse,
        //        Wrap: vkNum,
        //        Format: vkFalse,
        //        ReplaceWith: vkReplace,
        //        Replace: vkDynamic,
        //        MatchKashida: vkFalse,
        //        MatchDiacritics: vkFalse,
        //        MatchAlefHamza: vkFalse,
        //        MatchControl: vkFalse );

        //    vkMyDoc.Sections.Item( 1 ).Footers.Item( Word.WdHeaderFooterIndex.wdHeaderFooterEvenPages ).Range.Find.Execute(
        //        FindText: vkFind,
        //        MatchCase: vkFalse,
        //        MatchWholeWord: vkFalse,
        //        MatchWildcards: vkFalse,
        //        MatchSoundsLike: vkFalse,
        //        MatchAllWordForms: vkFalse,
        //        Forward: vkFalse,
        //        Wrap: vkNum,
        //        Format: vkFalse,
        //        ReplaceWith: vkReplace,
        //        Replace: vkDynamic,
        //        MatchKashida: vkFalse,
        //        MatchDiacritics: vkFalse,
        //        MatchAlefHamza: vkFalse,
        //        MatchControl: vkFalse );

        //}



        /// <summary>
        /// Replace inside the range
        /// </summary>
        /// <param name="range"></param>
        /// <param name="wordDocument"></param>
        /// <param name="wordApp"></param>
        /// <param name="findText"></param>
        /// <param name="replaceText"></param>
        private static void ReplaceRange(Word.Range range, Word.Document wordDocument, Word.Application wordApp, object findText, object replaceText)
        {
            object missing = System.Reflection.Missing.Value;

            wordDocument.Activate();

            //object item = Word.WdGoToItem.wdGoToPage;
            //object whichItem = Word.WdGoToDirection.wdGoToFirst;
            //wordDocument.GoTo(ref item, ref whichItem, ref missing, ref missing);

            //object forward = true;
            //object replaceAll = Word.WdReplace.wdReplaceAll;
            //object matchAllWord = true;

            //range.Find.Execute(ref findText, ref missing, ref matchAllWord,
            //    ref missing, ref missing, ref missing, ref forward,
            //    ref missing, ref missing, ref replaceText, ref replaceAll,
            //    ref missing, ref missing, ref missing, ref missing);

            range.Find.ClearFormatting();
            range.Find.Replacement.ClearFormatting();

            object vkTrue  = true;
            object vkFalse = false;

            range.Find.Execute(
                FindText: findText,
                MatchCase: vkFalse,
                MatchWholeWord: vkTrue,
                MatchWildcards: vkFalse,
                MatchSoundsLike: vkFalse,
                MatchAllWordForms: vkFalse,
                Forward: vkFalse,
                Wrap: 1,
                Format: vkFalse,
                ReplaceWith: replaceText,
                Replace: 2,
                MatchKashida: vkFalse,
                MatchDiacritics: vkFalse,
                MatchAlefHamza: vkFalse,
                MatchControl: vkFalse);
        }
Ejemplo n.º 7
0
        // Open a file (the file must exists) and activate it
        public void Open(string strFileName)
        {
//			oWordApplic = new Word.ApplicationClass();
            object fileName    = strFileName;
            object readOnly    = false;
            object isVisible   = true;
            object missing     = System.Reflection.Missing.Value;
            string strFileType = strFileName.Substring(strFileName.LastIndexOf(".") + 1);

            if (strFileType == "doc")
            {
                object templatePath = @strFileName;
                oDoc = oWordApplic.Documents.Add(ref templatePath, ref missing, ref missing, ref missing);
            }
            else
            {
                oDoc = oWordApplic.Documents.Open(ref fileName, ref missing, ref readOnly,
                                                  ref missing, ref missing, ref missing, ref missing, ref missing, ref missing,
                                                  ref missing, ref missing, ref isVisible, ref missing, ref missing, ref missing, ref missing);
            }
            oDoc.Activate();
        }
Ejemplo n.º 8
0
        public void InsertTitle(string title, string level)
        {
            object missing = System.Reflection.Missing.Value;

            Word._Application app  = new Word.Application();
            object            path = RandomPath;

            Word.Document doc = app.Documents.Add(ref missing, ref missing, ref missing, ref missing);
            doc.Activate();
            Word.Range range = doc.Paragraphs.Last.Range;//定位到段尾
            //range.InsertParagraphAfter();



            range.Text        = level + "." + title;
            range.Font.Italic = 1;
            range.Font.Color  = Word.WdColor.wdColorBlue;

            //object styleName = "标题 3";
            //range.set_Style(ref styleName);
            doc.SaveAs(ref path, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);
            app.Quit(ref missing, ref missing, ref missing);
            InsertFile(path.ToString());
        }
Ejemplo n.º 9
0
        private void btn_start_Click(object sender, EventArgs e)
        {
            //check if template exists
            if (ofd_template.FileName.Length == 0)
            {
                MessageBox.Show("first choose a doc word");
                return;
            }

            // check empty table
            if (dgv_data.Rows.Count == 0)
            {
                MessageBox.Show("table is empty"); return;
            }

            //check row contents
            int base_vle = -1;

            for (int i = 0; i < dgv_data.Rows.Count - 1; i++)
            {
                if (base_vle == -1)
                {
                    base_vle = Convert.ToInt32(dgv_data.Rows[i].Cells[2].Value);
                }
                else
                {
                    int this_vle = Convert.ToInt32(dgv_data.Rows[i].Cells[2].Value);;
                    if (this_vle != base_vle)
                    {
                        MessageBox.Show("content rows are not same!");
                        return;
                    }
                }
            }

            Directory.CreateDirectory("export/");
            if (Directory.GetFiles("export/").Length > 0)
            {
                MessageBox.Show("first empty export folder"); return;
            }

            progressBar1.Maximum = base_vle;
            progressBar1.Value   = 0;
            for (int i = 0; i < base_vle; i++)
            {
                progressBar1.Value = i + 1;

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

                Word.Document aDoc = null;


                wordApp.Visible = false;


                File.Copy(ofd_template.FileName, "export/" + i + ".docx");

                aDoc = wordApp.Documents.Open(System.Windows.Forms.Application.StartupPath + "/export/" + i + ".docx");


                aDoc.Activate();


                //  Call FindAndReplace()function for each change
                for (int ii = 0; ii < dgv_data.RowCount - 1; ii++)
                {
                    string   this_tag = dgv_data.Rows[ii].Cells[0].Value.ToString();
                    string[] contents = dic_data[this_tag];

                    this.FindAndReplace(wordApp, this_tag, contents[i]);
                }


                aDoc.Save();
                aDoc.Close();
            }
            MessageBox.Show("done!");
        }
Ejemplo n.º 10
0
        // Open a new document
        public void Open( )
        {
            object missing = System.Reflection.Missing.Value;
            oDoc = oWordApplic.Documents.Add(ref missing, ref missing,ref missing, ref missing);

            oDoc = oWordApplic.Documents.Add(ref missing, ref missing, ref missing, ref missing);

            oDoc.Activate();
        }
Ejemplo n.º 11
0
        // Open a file (the file must exists) and activate it
        public void Open( string strFileName)
        {
            object fileName = strFileName;
            object readOnly = false;
            object isVisible = true;
            object missing = System.Reflection.Missing.Value;

            oDoc = oWordApplic.Documents.Open(ref fileName, ref missing,ref readOnly,
                ref missing, ref missing, ref missing, ref missing, ref missing, ref missing,
                ref missing, ref missing, ref isVisible,ref missing,ref missing,ref missing);

            oDoc.Activate();
        }