用VB编的一小工具要对WORD文档中进行大量的字符串替换。先引用“Microsoft Word xx.x Object Libaray”后假如处理c:\temp\test.docDim Word As Object
Set Word = GetObject("c:\temp\test.doc")
Word.Application.Visible = True     Set myRange = ActiveDocument.Content
     myRange.Find.Execute FindText:="图象", replacewith:="图像", Replace:=wdReplaceAll
     myRange.Find.Execute FindText:="帐户", replacewith:="账户", Replace:=wdReplaceAll
     myRange.Find.Execute FindText:=ChrW(8898), replacewith:="∩", Replace:=wdReplaceAll
……我想让这些字符串替换能随时根据需要进行修改,设想将这些替换串写进一文本文件(如为zdth.txt)中:
1.如下的文本中“→”前为要替换的原串,“→”后为替换后的字符串。
2.由于WORD中有相当一部分字符并不在文本文件中正确显示,所以我采用CheW(xxxx)码值的方法表示(如下面第3例)。
3.替换后的字符串全为GB字符"图象"→图像
"帐户"→账户
ChrW(8898)→∩
……这样,以后要增删替换串时,就不必修改源代码,直接修改zdth.txt即可。不知此要求怎么实现,请高手相助。本人学VB不久,只能算入门级,请说明详细一点,谢谢!
不好意思,30分是坛上允许我给出的最高分。

解决方案 »

  1.   

    你这个问题不难,但是手头没有现成的例子,你可以去 vba专区去找以前的帖子,哪里又,我找到过.或者涌另外一个方法,打开一个word,吧你的文档复制上去,然后录制宏,  然后吧你宏里的代码直接考过来就可以运行了, 宏真的是一个太牛的东西了.
      

  2.   

    Dim Filenum As Long
    Dim strtmp As String
    Dim strText As String
    Dim strArray() As String
    Dim i As Integer
    Dim strOld As String
    Dim strNew As String
    Filenum = FreeFile()
    Open App.Path + "\zdth.txt" For Input As FilenumDo
        Line Input #Filenum, strtmp
        strText = strText + strtmp
    Loop Until EOF(Filenum)Close #FilenumstrArray() = Split(strText, ",")
    For i = 0 To UBound(strArray) - 1
        strOld = Mid(strArray(i), 1, InStr(strArray(i), "-") - 1)
        strNew = Mid(strArray(i), InStr(strArray(i), "-") + 1, Len(strArray(i)) - InStr(strArray(i), "-"))
        
        myRange.Find.Execute FindText:=strOld, replacewith:=strNew, Replace:=wdReplaceAll
        Debug.Print strArray(i)
    Next i代码中省略了打开Word部分,lz自己写吧,主要参考我写的替换不部分就好了。其实就是读取一个特定格式的txt文档,然后循环替换就好了。
    按照我程序的写法,txt文档应该写成下面的格式图象-图像,
    帐户-账户,
    ChrW(8898)-∩,