如何替换MS-word文本框中的文本? [英] How do I replace text inside MS-word textbox?

查看:111
本文介绍了如何替换MS-word文本框中的文本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用C#编写了一个Windows Forms程序。它包含文本框和数据网格。



我想将数据导出到Word文件中以使用模板。我将Microsoft Word库添加到我的代码中。



我还在Word文件中创建了文本框形状,并在其中添加了文本,可以从我的c#程序中找到



现在一切似乎没问题,但是我被困在C#端,我可以替换普通文本,但不能替换文本框内的文本在word文件中



我尝试过:



这是我的代码:
< pre lang =c#>< pre> private void FindAndReplace(Word.Application wordapp,object ToFindText,object replaceWithText)

{
object matchCase = true;
object matchWholeWord = true;
object matchWildCards = false;
object matchSoundLike = false;
object matchAllforms = false;
object forward = true;
object format = false;
object matchKashida = false;
object matchDiactitics = false;
object matchAlefHamza = false;
object matchControl = false;
object matchread_only = false;
object visible = true;
object replace = 2;
object wrap = 1;

wordapp.Selection.Find.Execute(ref ToFindText,
ref matchCase,ref matchWholeWord,
ref matchWildCards,ref matchSoundLike,
ref matchAllforms,ref forward,
ref wrap,ref format,ref replaceWithText,
ref replace,ref matchKashida,
ref matchDiactitics,ref matchAlefHamza,
ref matchControl);

}


private void CreateWordDocument(对象文件名,对象SaveAs)
{
Word.Application wordApp = new Word.Application( );缺少
对象= Missing.Value;
Word.Document myWordDoc = null;

if(File.Exists((string)filename))

{
object readOnly = false;
object isVisible = false;
wordApp.Visible = false;

myWordDoc = wordApp.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);

myWordDoc.Activate();

//查找并重放
this.FindAndReplace(wordApp,< date1>,textBox2.Text);
this.FindAndReplace(wordApp,< date2>,textBox3.Text);

}

else
{
MessageBox.Show(找不到文件!);
}

// Svae为
myWordDoc.SaveAs2(ref SaveAs,ref missing,ref missing,ref missing,
ref missing,ref missing,ref missing,
ref缺失,ref缺失,ref缺失,
ref缺失,ref缺失,ref缺失,
ref缺失,ref缺失,ref遗失);


myWordDoc.Close();
wordApp.Quit();
MessageBox.Show(File Created!);


}

解决方案

我发现找出代码的最简单方法是使用内置的Macro Recorder,因为它会为您编写代码! ;)



这是一个应该有用的视频:简介Word宏 - 关于如何编辑Word宏的教程 - YouTube [ ^ ]

I wrote a Windows Forms program using C#. It contains textboxes and a data-grid.

I want to export the data into a Word file to use the template. I added the library of Microsoft Word into my code.

Also I created textboxes "shapes" inside the Word file and putted inside them text to be find from my c# program

For now everything seems okay, But I'm stuck at the C# side, I can replace the normal text, but not the text inside textboxes "Inside the word file"

What I have tried:

And here is my code:
<pre lang="c#"><pre>private void FindAndReplace(Word.Application wordapp, object ToFindText, object replaceWithText)

        {
            object matchCase = true;
            object matchWholeWord = true;
            object matchWildCards = false;
            object matchSoundLike = false;
            object matchAllforms = false;
            object forward = true;
            object format = false;
            object matchKashida = false;
            object matchDiactitics = false;
            object matchAlefHamza = false;
            object matchControl = false;
            object matchread_only = false;
            object visible = true;
            object replace = 2;
            object wrap = 1;

            wordapp.Selection.Find.Execute(ref ToFindText,
                ref matchCase, ref matchWholeWord,
                ref matchWildCards, ref matchSoundLike,
                ref matchAllforms, ref forward,
                ref wrap, ref format, ref replaceWithText,
                ref replace, ref matchKashida,
                ref matchDiactitics, ref matchAlefHamza,
                ref matchControl);

        }


        private void CreateWordDocument(object filename, object SaveAs)
        {
            Word.Application wordApp = new Word.Application();
            object missing = Missing.Value;
            Word.Document myWordDoc = null;

            if (File.Exists((string)filename))

            {
                object readOnly = false;
                object isVisible = false;
                wordApp.Visible = false;

                myWordDoc = wordApp.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);

                myWordDoc.Activate();

                //find and replave
                this.FindAndReplace(wordApp, "<date1>", textBox2.Text);
                this.FindAndReplace(wordApp, "<date2>", textBox3.Text);

            }

            else
            {
                MessageBox.Show("File not found!");
            }

            //Svae as
            myWordDoc.SaveAs2(ref SaveAs, 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);


            myWordDoc.Close();
            wordApp.Quit();
            MessageBox.Show("File Created!");


         }

解决方案

The easiest way that I have found to figure out the code is to use the in-built Macro Recorder as it will write the code for you! ;)

Here is a video that should help: Intro to Word Macros - Tutorial on How to Edit a Word Macro - YouTube[^]


这篇关于如何替换MS-word文本框中的文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆