1、用VBA打开一个文件,需要在文件的最开始处,追加一些字符串,2、例如文件原先是
<TS>
<defaultcodec>GB2312</defaultcodec>3、我想在文件头上追加(不是覆盖)新的内容<!DOCTYPE TS>4、期望结果是
<!DOCTYPE TS><TS>
<defaultcodec>GB2312</defaultcodec>5、目前我的做法是写把文件以二进制的形式读取出来,转换为UNICODE的字符串,然后把该字符串前面加上我想要的字符,再重新把组合的字符写回文件中,可是我文件里面的中文,会出现乱码6、请问有什么好的办法实现我的功能
在线等,谢谢!

解决方案 »

  1.   


    '//你试试这个代码
    Private Sub Command1_Click()
        Dim s As String
        Open "C:\Test.txt" For Input As #1
        s = StrConv(InputB(LOF(1), #1), vbUnicode)           '将文件内容附给变量   S
        Close #1
        s = "<!DOCTYPE TS>" & s '//添加
        Open "C:\Test1.txt" For Output As #2
        Print #2, s
        Close #2
    End Sub
      

  2.   

    恩?我在自己机子上试验中文英文都行的啊,不知道是不是你txt的字体问题啊,要不你把你的txt数据贴一部分出来看看!
      

  3.   

    我的文档其实是XML的文件,把下面代码拷贝到一个XML编辑工具里,存成.ts的格式,
    追加处理后,你用XML的工具打开,注意中文的地方有乱码出现
    <TS>
    <defaultcodec>GB2312</defaultcodec>
    <context>
        <name>QCdlATCOutputWdg</name>
        <message>
            <source>&#xb9;&#xa4;&#xbe;&#xdf;&#xba;&#xc5;</source>
            <translation type="unfinished"></translation>
        </message>
        <message>
            <source>ATC &#xb6;&#xaf;&#xd7;&#xf7;</source>
            <translation type="unfinished"></translation>
        </message>
        <message>
            <source>&#xb0;&#xd1;&#xd6;&#xf7;&#xd6;&#xe1;&#xc9;&#xcf;&#xb5;&#xc4;&#xb9;&#xa4;&#xbe;&#xdf;&#xb1;&#xa3;&#xb4;&#xe6;&#xb5;&#xbd;&#xb5;&#xb6;&#xbf;&#xe2;&#xd6;&#xd0;&#xa3;&#xac;&#xb0;&#xd1;&#xd6;&#xb8;&#xb6;&#xa8;&#xb5;&#xc4;&#xb9;&#xa4;&#xbe;&#xdf;&#xb0;&#xb2;&#xd7;&#xb0;&#xb5;&#xbd;&#xd6;&#xf7;&#xd6;&#xe1;&#xc9;&#xcf;.</source>
            <translation type="unfinished">把主轴上的工具保存到刀库,把指定工具安装到主轴上.</translation>
        </message>
        <message>
            <source>&#xca;&#xe4;&#xc8;&#xeb;&#xb9;&#xa4;&#xbe;&#xdf;&#xba;&#xc5;&#xa3;&#xac;&#xb0;&#xb4;&#xd1;&#xad;&#xbb;&#xb7;&#xc6;&#xf4;&#xb6;&#xaf;</source>
            <translation type="unfinished">输入工具号,按【循环启动】。</translation>
        </message>
    </context>
    </TS>
      

  4.   


    原来你那是ts文件啊,不知道能不能先写成txt再重命名为.ts文件啊,呵呵!
      

  5.   

    一楼的稍加改动即可:Private Sub Command1_Click()
        Dim s() As byte
        dim sOutput as string
        Open "C:\Test.txt" For Input As #1
        redim s(lof(#1))
        get #1,,s
        Close #1
        
        sOutput = "<!DOCTYPE TS>" & s '//添加
        Open "C:\Test1.txt" For Output As #2
        put #2,,sOutput
        put #2,,s
        Close #2
    End Sub