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

本文关键字:搜索 结果 通配符 Word -通 Office | 更新日期: 2023-09-27 18:24:48

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

你能告诉我如何获得通配符选项

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

搜索的文本吗

我建议使用单词书签而不是占位符,因为文本不会显示给用户,你可以使用类似这样的东西自动插入文本,因为你可以获得特定书签的范围:

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);
            }
        }

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

尊敬的/FirstName//MiddleName//LastName/:

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

我还有一个Dictionary<string, string> Data,它存储每个占位符的密钥/值

    ...
    "/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/",...
        }
    }