C#和Word-替换为格式

| 我正在尝试以最简单的形式在预先存在的Word文档中填写一些信息。我的代码做得很好... 它不执行的工作是修复所需的格式,替换后的文本必须变为黑色和粗体,表格上的totals字段需要正确对齐。 如果有人可以指出如何在我的代码中执行此操作,将不胜感激。 这是我当前的代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Word = Microsoft.Office.Interop.Word;

namespace WordDocTesting
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void FindAndReplace(Word.Application WordApp,
                                    object findText,
                                    object replaceWithText)
        {
            object missing = Type.Missing;

            object matchCase = true;
            object matchWholeWord = true;
            object matchWildCards = false;
            object matchSoundLike = false;
            object nmatchAllWordForms = false;
            object forward = true;
            object format = false;
            object matchKashida = false;
            object matchDiacritics = false;
            object matchAlefHamza = false;
            object matchControl = false;
            object read_only = false;
            object visible = true;
            object replace = 2;
            object wrap = 1;

            if (replaceWithText.ToString().Length < 250) // Normal execution
            {
                WordApp.Selection.Find.Execute(ref findText,
                                               ref matchCase,
                                               ref matchWholeWord,
                                               ref matchWildCards,
                                               ref matchSoundLike,
                                               ref nmatchAllWordForms,
                                               ref forward,
                                               ref wrap,
                                               ref format,
                                               ref replaceWithText,
                                               ref replace,
                                               ref matchKashida,
                                               ref matchDiacritics,
                                               ref matchAlefHamza,
                                               ref matchControl);
            }
            else
            {
                WordApp.Application.Selection.Find.Execute(ref findText, 
                                                           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);

                WordApp.Application.Selection.Text.

                WordApp.Application.Selection.Text = (string)replaceWithText; 
            }

        }

        private void button1_Click(object sender, EventArgs e)
        {
            Word.Application word = new Word.Application();
            Word.Document document = new Word.Document();

            // Define an object to pass to the word API for missing parameters
            object missing = Type.Missing;

            try
            {
                // Everything that word accepts must be an object
                object fileName = @\"R:\\Project\\Proposals\\TestMe.docx\";

                object readOnly = true;
                document = word.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 missing, 
                                               ref missing, ref missing, ref missing,
                                               ref missing);

                document.Activate();

                String propsalTitle = \"Lorem ipsum dolor sit amet\";
                this.FindAndReplace(word, \"Enter Proposal Title Here\", propsalTitle);

                string defaultTechDesc = \"ENTER RELEVANT TECHNICAL INFORMATION ABOUT PROJECT HERE.  USE PICTURES WHEN AVAILABLE AND APPLICABLE.\";
                String techDesc = \"Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer tempor rutrum libero sed dapibus. Praesent vehicula mollis ultricies. Maecenas vulputate enim vitae nisi gravida euismod. Morbi aliquam lacus enim. Donec suscipit mi at sem mollis id gravida sapien tempor. Donec in sem at quam dignissim placerat et a urna. Maecenas nec tellus vel ipsum volutpat aliquet nec ut tortor. Nulla facilisi. Donec vel quam lectus, ac mattis sapien. Nullam a justo nisl, sit amet congue velit. Ut imperdiet, velit id luctus vulputate, augue libero consectetur eros, quis gravida quam orci ac lacus. Curabitur mollis, mi sit amet interdum feugiat, risus lacus dignissim metus, ut luctus nulla orci a ante. Nunc et turpis vel ipsum faucibus rhoncus eu eu velit. Sed interdum, magna sit amet porta euismod, orci felis tincidunt mauris, et iaculis ligula arcu molestie justo. Duis sollicitudin purus ut purus hendrerit adipiscing. Donec aliquam ultricies eros. \" + \"\\n\\n\" + defaultTechDesc;
                this.FindAndReplace(word, defaultTechDesc, techDesc);

                String deliverables = \"Lorem ipsum dolor sit amet, consectetur adipiscing elit.\";
                this.FindAndReplace(word, \"ENTER ALL ITEMS THAT WILL BE DELIVERD OVER PERIOD OF PERFORMANCE FOR THIS PROJECT.\", deliverables);

                String Contact = \"Jenny\";
                this.FindAndReplace(word, \"ENTER POINT OF CONTACT HERE\", admContact);

                String PocTitle = \"ipsum\";
                this.FindAndReplace(word, \"ENTER POC TITLE HERE\", admPocTitle);

                String PocTeam = \"dolor\";
                this.FindAndReplace(word, \"ENTER POC TEAM NAME HERE\", admPocTeam);

                String PocPhone = \"867-5309\";
                this.FindAndReplace(word, \"ENTER POC PHONE NUMBER HERE\", admPocPhone);

                String PocEmail = \"Jenny.G.Time@Sit.amet\";
                this.FindAndReplace(word, \"ENTER POC EMAIL HERE\", admPocEmail);

                // Tables
                Word.Table table = document.Tables[1];

                double totalCost = 0;
                Word.Row row;
                for (int i = 1; i <= 10; i++)
                {
                    row = table.Rows.Add(ref missing);
                    row.Cells[1].Range.Text = \"Task New \" + i;
                    row.Cells[2].Range.Text = String.Format(\"{0:C}\", i);
                    totalCost = totalCost + i;
                }

                row = table.Rows.Add(ref missing);
                row.Cells[1].Range.Text = \"Total\";
                row.Cells[2].Range.Text = String.Format(\"{0:C}\", totalCost);

                object newFileName = @\"R:\\Project\\Proposals\\TestMe2.docx\";

                document.SaveAs(ref newFileName,
                                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);
            }

            catch (Exception ex)
            {
                // Message here
            }
            finally
            {
                document.Close(ref missing, ref missing, ref missing);
                word.Application.Quit(ref missing, ref missing, ref missing);
            }
        }
    }
}
    
已邀请:
得到它了。 粗体文本,删除斜体,更改颜色,文本对齐...全部包含在下面的代码中。 再次感谢所有帮助。 :)
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Word = Microsoft.Office.Interop.Word;
using Color = Microsoft.Office.Interop.Word.WdColor;

namespace WordDocTesting
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void FindAndReplace(Word.Application WordApp,
                                    object findText,
                                    object replaceWithText,
                                    bool boldIt)
        {
            object missing = Type.Missing;

            object matchCase = true;
            object matchWholeWord = true;
            object matchWildCards = false;
            object matchSoundLike = false;
            object nmatchAllWordForms = false;
            object forward = true;
            object format = false;
            object matchKashida = false;
            object matchDiacritics = false;
            object matchAlefHamza = false;
            object matchControl = false;
            object read_only = false;
            object visible = true;
            object replace = 2;
            object wrap = 1;


            WordApp.Selection.Find.Execute(ref findText,
                                           ref matchCase,
                                           ref matchWholeWord,
                                           ref matchWildCards,
                                           ref matchSoundLike,
                                           ref nmatchAllWordForms,
                                           ref forward,
                                           ref wrap,
                                           ref format,
                                           /*ref replaceWithText,*/ ref missing,
                                           /*ref replace,*/         ref missing,
                                           ref matchKashida,
                                           ref matchDiacritics,
                                           ref matchAlefHamza,
                                           ref matchControl);


            if (boldIt)
            {
                WordApp.Application.Selection.Font.Bold = 1;
            }

            WordApp.Application.Selection.Font.Italic = 0;
            WordApp.Application.Selection.Font.Color = Color.wdColorBlack;

            WordApp.Application.Selection.Text = (string)replaceWithText; 
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Word.Application word = new Word.Application();
            Word.Document document = new Word.Document();

            // Define an object to pass to the word API for missing parameters
            object missing = Type.Missing;

            try
            {
                // Everything that word accepts must be an object
                object fileName = @\"R:\\Project\\Proposals\\TestMe.docx\";

                object readOnly = true;
                document = word.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 missing, 
                                               ref missing, ref missing, ref missing,
                                               ref missing);

                document.Activate();

                String propsalTitle = \"Lorem ipsum dolor sit amet\";
                this.FindAndReplace(word, \"Enter Proposal Title Here\", propsalTitle, true);

                string defaultTechDesc = \"ENTER RELEVANT TECHNICAL INFORMATION ABOUT PROJECT HERE.  USE PICTURES WHEN AVAILABLE AND APPLICABLE.\";
                String techDesc = \"Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer tempor rutrum libero sed dapibus. Praesent vehicula mollis ultricies. Maecenas vulputate enim vitae nisi gravida euismod. Morbi aliquam lacus enim. Donec suscipit mi at sem mollis id gravida sapien tempor. Donec in sem at quam dignissim placerat et a urna. Maecenas nec tellus vel ipsum volutpat aliquet nec ut tortor. Nulla facilisi. Donec vel quam lectus, ac mattis sapien. Nullam a justo nisl, sit amet congue velit. Ut imperdiet, velit id luctus vulputate, augue libero consectetur eros, quis gravida quam orci ac lacus. Curabitur mollis, mi sit amet interdum feugiat, risus lacus dignissim metus, ut luctus nulla orci a ante. Nunc et turpis vel ipsum faucibus rhoncus eu eu velit. Sed interdum, magna sit amet porta euismod, orci felis tincidunt mauris, et iaculis ligula arcu molestie justo. Duis sollicitudin purus ut purus hendrerit adipiscing. Donec aliquam ultricies eros. \";
                this.FindAndReplace(word, defaultTechDesc, techDesc, false);

                String deliverables = \"Lorem ipsum dolor sit amet, consectetur adipiscing elit.\";
                this.FindAndReplace(word, \"ENTER ALL ITEMS THAT WILL BE DELIVERD OVER PERIOD OF PERFORMANCE FOR THIS PROJECT.\", deliverables, false);

                String Contact = \"Jenny\";
                this.FindAndReplace(word, \"ENTER POINT OF CONTACT HERE\", Contact, false);

                String PocTitle = \"ipsum\";
                this.FindAndReplace(word, \"ENTER POC TITLE HERE\", PocTitle, false);

                String PocTeam = \"dolor\";
                this.FindAndReplace(word, \"ENTER POC TEAM NAME HERE\", PocTeam, false);

                String PocPhone = \"867-5309\";
                this.FindAndReplace(word, \"ENTER POC PHONE NUMBER HERE\", PocPhone, false);

                String PocEmail = \"Jenny.G.Time@Sit.amet\";
                this.FindAndReplace(word, \"ENTER POC EMAIL HERE\", PocEmail, false);

                // Tables
                Word.Table table = document.Tables[1];

                int x = 0;
                double totalCost = 0;
                Word.Row row;
                for (int i = 1; i <= 10; i++)
                {
                    x = i + 1;
                    row = table.Rows.Add(ref missing);
                    row.Cells[1].Range.Text = \"Task New \" + i;
                    row.Cells[2].Range.Text = String.Format(\"{0:C}\", i);
                    table.Cell(x, 1).Range.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphLeft;
                    table.Cell(x, 1).Range.Font.Bold = 0;
                    table.Cell(x, 2).Range.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphRight;
                    table.Cell(x, 2).Range.Font.Bold = 0;
                    totalCost = totalCost + i;
                }

                x = x + 1;
                row = table.Rows.Add(ref missing);
                row.Cells[1].Range.Text = \"Total\";
                row.Cells[2].Range.Text = String.Format(\"{0:C}\", totalCost);
                table.Cell(x, 1).Range.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphRight;
                table.Cell(x, 1).Range.Font.Bold = 1;
                table.Cell(x, 2).Range.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphRight;
                table.Cell(x, 2).Range.Font.Bold = 1;

                object newFileName = @\"R:\\Project\\Proposals\\TestMe2.docx\";

                document.SaveAs(ref newFileName,
                                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);
            }

            catch (Exception ex)
            {
                // Message here
            }
            finally
            {
                document.Close(ref missing, 
                               ref missing, 
                               ref missing);

                word.Application.Quit(ref missing, 
                                      ref missing, 
                                      ref missing);
            }
        }
    }
}
    
我不是Interop或C#上使用Microsoft Word的专家,但是您可能对此很感兴趣: 如何在MS Word表单元格中使文本的一部分变为粗体? 希望能帮助到你 。     

要回复问题请先登录注册