object Obj_FileName = wordPath;
            doc = word.Documents.Open(ref Obj_FileName, ref missing, ref ReadOnly, ref missing,
          ref missing, ref missing, ref missing, ref missing,
          ref missing, ref missing, ref missing, ref Visible,
          ref missing, ref missing, ref missing,
          ref missing);
            doc.Activate();
   foreach (string key in valueCollection.Keys)
           {
               word.Selection.Find.ClearFormatting();
               word.Selection.Find.Text = "{#" + key + "#}";               word.Selection.Find.Replacement.ClearFormatting();
               word.Selection.Find.Replacement.Text = valueCollection[key];
               word.Selection.Find.Execute(ref missing, ref missing, ref missing, ref missing, ref missing,ref missing, ref missing, ref missing, ref missing, ref missing,ref replaceAll, ref missing, ref missing, ref missing, ref missing);
           }这里替换的时候,只有替换普通的字符,但用过#Test#这样的字符是包含在文本框里面放入到word的,却不能替换到,求教

解决方案 »

  1.   

    很少做Word的开发……
    帮老大顶上吧,呵呵。
      

  2.   

    下面是一段VBA的代码
     Selection.Find.Execute Replace:=wdReplaceAll
        With Selection.Find
            .Text = "1*5"
            .Replacement.Text = "A"
            .Forward = True
            .Wrap = wdFindContinue
            .Format = False
            .MatchCase = False
            .MatchWholeWord = False
            .MatchByte = False
            .MatchAllWordForms = False
            .MatchSoundsLike = False
            .MatchWildcards = True
        End With
        Selection.Find.Execute Replace:=wdReplaceAll
      

  3.   

    using Word = Microsoft.Office.Interop.Word;
      public static void DoSearchAndReplaceInWord()
            {
                // Create the Word application and declare a document
                Word.Application word = new Word.Application();
                Word.Document doc = new Word.Document();            // Define an object to pass to the API for missing parameters
                object missing = System.Type.Missing;            try
                {
                    // Everything that goes to the interop must be an object
                    object fileName = @"C:\myDocument.doc";                // Open the Word document.
                    // Pass the "missing" object defined above to all optional
                    // parameters.  All parameters must be of type object,
                    // and passed by reference.
                    doc = word.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, ref missing,
                        ref missing, ref missing, ref missing);                // Activate the document
                    doc.Activate();                // Loop through the StoryRanges (sections of the Word doc)
                    foreach (Word.Range tmpRange in doc.StoryRanges)
                    {
                        // Set the text to find and replace
                        tmpRange.Find.Text = "findme";
                        tmpRange.Find.Replacement.Text = "findyou";                    // Set the Find.Wrap property to continue (so it doesn't
                        // prompt the user or stop when it hits the end of
                        // the section)
                        tmpRange.Find.Wrap = Word.WdFindWrap.wdFindContinue;                    // Declare an object to pass as a parameter that sets
                        // the Replace parameter to the "wdReplaceAll" enum
                        object replaceAll = Word.WdReplace.wdReplaceAll;                    // Execute the Find and Replace -- notice that the
                        // 11th parameter is the "replaceAll" enum object
                        tmpRange.Find.Execute(ref missing, ref missing, ref missing,
                            ref missing, ref missing, ref missing, ref missing,
                            ref missing, ref missing, ref missing, ref replaceAll,
                            ref missing, ref missing, ref missing, ref missing);
                    }                // Save the changes
                    doc.Save();                // Close the doc and exit the app
                    doc.Close(ref missing, ref missing, ref missing);
                    word.Application.Quit(ref missing, ref missing, ref missing);
                }
                catch (Exception ex)
                {
                    doc.Close(ref missing, ref missing, ref missing);
                    word.Application.Quit(ref missing, ref missing, ref missing);
                }
            }
      

  4.   

    "{#" + key + "#}"; 用这个试了,一点问题都没有啊貌似小了 switch(key) 条件
      

  5.   

    换个实现方案
    Asp.net下C#自动化调用Word的实例与总结(转载)
    http://blog.csdn.net/soaexcel/archive/2008/09/22/2961155.aspx
      

  6.   

    用word的宏录制看录制后的对象也是如此
    Sub Test()
    '
    ' Test 宏
    '
    '
        Selection.Find.ClearFormatting
        Selection.Find.Replacement.ClearFormatting
        With Selection.Find
            .Text = "11"
            .Replacement.Text = "Test"
            .Forward = True
            .Wrap = wdFindContinue
            .Format = False
            .MatchCase = False
            .MatchWholeWord = False
            .MatchByte = True
            .MatchWildcards = False
            .MatchSoundsLike = False
            .MatchAllWordForms = False
        End With
        Selection.Find.Execute Replace:=wdReplaceAll
    End Sub
    ——————————————————————————————————————————
    但为何代码只能替换字符,而不能替换到文本框里面的字符呢?
    加分