如何将WORD模板中的字符串替换,如将(name) 换成姓名张三?

解决方案 »

  1.   

    打开Word,在菜单的"工具"->"宏"->"录制新宏",然后开始对Word操作(比如 将(name) 换成姓名张三 等),操作完了停止录制宏,在"宏"->"Visual Basic编辑器"->"模块"里的模块就有你录制的过程的代码,把需要的代码稍微修改就可以直接应用到vb里
      

  2.   

    引用Mircrosoft.Word11.Object Library(office 2003),低版本的自己查找
      Dim wordApp As New Word.Application
        With wordApp
            .Visible = True
            .Activate
            .Documents.Open ("c:\1.doc")
            .Selection.Find.ClearFormatting
       .Selection.Find.Replacement.ClearFormatting
        With .Selection.Find
            .Text = "name"
            .Replacement.Text = "张三"
            .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 With
      

  3.   

    to jobs002
    一个变量替换已解决,但替换多个时却有问题,如(name) 换成姓名张三,(sex)换成性别男,即无法换成多个
      

  4.   

    那就用faysky2() 的方法,自己录制一个宏,宏的Selection是word.Application的子对象
      

  5.   

    Dim wordApp As New Word.Application
        With wordApp
            .Visible = True
            .Activate
            .Documents.Open ("c:\1.doc")
            .Selection.Find.ClearFormatting
       .Selection.Find.Replacement.ClearFormatting
        With .Selection.Find
            .Text = "name"
            .Replacement.Text = "张三"
            .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
         With .Selection.Find
            .Text = "sex"
            .Replacement.Text = "男"
            .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 With
      

  6.   

    jobs002(赏花赏月赏春光) 的方法确实不错
      

  7.   

    wordApp.Selection.Find.ClearFormatting
    With wordApp.Selection.Find
    .Text = "(jsyname)"
    .Replacement.Text = jsyname
    .Execute Replace:=wdReplaceAll
    .Text = "(yzbm)"
    .Replacement.Text = yzbm
    .Execute Replace:=wdReplaceAll