Office Word Library - 通过通配符搜索的结果

发布于 2025-01-06 12:13:53 字数 262 浏览 1 评论 0原文

我有一个 MS Word 2003 文档,其中包含 /FirstName/、/LastName/ 等占位符...我使用 Microsoft Office 12 Library 读取文件并使用 Find 通过通配符进行搜索.Text =“/[AZ]*/”。它工作正常,Find.Execute 确实准确地找到了占位符。但因为它只返回布尔值,所以我不知道如何获取占位符本身。

您能告诉我如何获取通过通配符选项搜索的文本吗

I have a MS Word 2003 documents that contains placeholders such as /FirstName/, /LastName/, etc... I used Microsoft Office 12 Library to read the file and search by wildcards with Find.Text = "/[A-Z]*/". It works fine, and Find.Execute does find the placeholder exactly. But because it returns only the boolean value, I don't know how to get the placeholder itself.

Can you please show me how to get the text that is searched by a wildcard option

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

找回味觉 2025-01-13 12:13:53

我建议使用单词书签而不是占位符,因为文本不会向用户显示,您可以使用此功能在您想要的任何位置自动插入文本,因为您可以获得特定书签的范围:

protected void insertTextAt(string bookmarkName, string text,
            bool useDefaults = true, string fontName = "Arial", 
            int fontSize = 11, int bold = 0,bool newLine = true)
        {
            try
            {
                Object oBookMarkName = bookmarkName;
                WordInterop.Range wRng =
                    this.wDoc.Bookmarks.get_Item(ref oBookMarkName).Range;
                wRng.Text = text;
                if (!useDefaults)
                {
                    wRng.Font.Bold = bold;
                    wRng.Font.Name = fontName;
                    wRng.Font.Size = fontSize;
                }
                if (newLine)
                {
                    wRng.Text += "\r\n";
                }
                wRng.Font.Bold = 0;
            }
            catch (Exception e)
            {
                String exceptionString = String.Format("Bookmark {0} could not"
                +" be found in template {1}",bookmarkName,this.template);
                throw new Exception(exceptionString,e);
            }

        }

I would suggest using word bookmarks instead of placeholders, because the text isn't shown to the user and you could use something la this to automatically insert text wherever you want because you can get the range of a specific bookmark:

protected void insertTextAt(string bookmarkName, string text,
            bool useDefaults = true, string fontName = "Arial", 
            int fontSize = 11, int bold = 0,bool newLine = true)
        {
            try
            {
                Object oBookMarkName = bookmarkName;
                WordInterop.Range wRng =
                    this.wDoc.Bookmarks.get_Item(ref oBookMarkName).Range;
                wRng.Text = text;
                if (!useDefaults)
                {
                    wRng.Font.Bold = bold;
                    wRng.Font.Name = fontName;
                    wRng.Font.Size = fontSize;
                }
                if (newLine)
                {
                    wRng.Text += "\r\n";
                }
                wRng.Font.Bold = 0;
            }
            catch (Exception e)
            {
                String exceptionString = String.Format("Bookmark {0} could not"
                +" be found in template {1}",bookmarkName,this.template);
                throw new Exception(exceptionString,e);
            }

        }
蓝眼睛不忧郁 2025-01-13 12:13:53

这是我的代码,供您参考。
首先,这是 letter.doc 文件

亲爱的/名字//中间名//姓氏/:

欢迎来到我们的节目。我们致力于为您提供最高的
客户服务质量......

我还有一个 Dictionary存储每个占位符的键/值的数据

    ...
    "/FirstName/" : "read from database"
    "/MiddleName/" : "read from database"
    "/LastName/" : "read from database"
    ...

我有一个读取 .doc 文件并替换占位符的方法:

    oWordApp = new MSWord.ApplicationClass();
    doc = oWordApp.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);
    doc.Activate();
    doc.Select();

    oWordApp.Selection.Find.ClearFormatting();
    oWordApp.Selection.Find.MatchWildcards = true;
    oWordApp.Selection.Find.Wrap = MSWord.WdFindWrap.wdFindContinue;
    oWordApp.Selection.Find.Text = "/[A-Z]*/";

    bool isFound = true;
    while(isFound == true) {
        isFound = oWordApp.Selection.Find.Execute(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);

        if( isFound == true ) {
            //use the database to do the replacing
            //how to get the placeholder itself, such as "/FirstName/", "/LastName/",...
        }
    }

Here is my code, for your interest.
Firstly, this is the letter.doc files

Dear /FirstName/ /MiddleName/ /LastName/:

Welcome to our program. We are committed to giving you the highest
quality of customer service....

I also have a Dictionary<string, string> Data that store the key/value for each placeholder

    ...
    "/FirstName/" : "read from database"
    "/MiddleName/" : "read from database"
    "/LastName/" : "read from database"
    ...

And I have a method that reads the .doc file and replaces the placeholders :

    oWordApp = new MSWord.ApplicationClass();
    doc = oWordApp.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);
    doc.Activate();
    doc.Select();

    oWordApp.Selection.Find.ClearFormatting();
    oWordApp.Selection.Find.MatchWildcards = true;
    oWordApp.Selection.Find.Wrap = MSWord.WdFindWrap.wdFindContinue;
    oWordApp.Selection.Find.Text = "/[A-Z]*/";

    bool isFound = true;
    while(isFound == true) {
        isFound = oWordApp.Selection.Find.Execute(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);

        if( isFound == true ) {
            //use the database to do the replacing
            //how to get the placeholder itself, such as "/FirstName/", "/LastName/",...
        }
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文