我在excel中想编点代码,实习写文件功能,在一个按钮的事件中调用写文件模块中的一个函数,不知道为什么老提示,"编译错误:要求对象",哪位仁兄能帮帮忙,告诉我为什么呀?要有实现代码能贴出来那就太感谢啦.

解决方案 »

  1.   

    谢谢提醒,现将代码贴出:
    模块一中的代码:
    '将字符串写入文件
    Public Function writeStringToFile(sTargetString As String, sFileName As String) As String
        
        Set fs = CreateObject("Scripting.FileSystemObject")
        Set fs_jb = fs.CreateTextFile(filepathname, True)
        fs_jb.writeline (sTargetString)    
        fs_jb.Close
        writeFile = "写入文件成功!"
    End Function
    按钮事件中的代码:
    Private Sub CommandButton_getTbsDefine_Click()
        Set sRtn = writeStringToFile("aaaa", "d:\test.sql")
        MsgBox sRtn, vbInformation, "提示"
        
    End Sub
      

  2.   

      先解决你的“要求对象”的问题:  修改你的代码:'按钮事件中的代码: 
    Private Sub CommandButton_getTbsDefine_Click()    Dim sRtn$
        sRtn = writeStringToFile("aaaa", "d:\test.sql") 
        MsgBox sRtn, vbInformation, "提示" End Sub
      

  3.   

      第二个:你的Function中有个“变量”可能有误!Set fs_jb = fs.CreateTextFile(filepathname, True) 
      这里面的:filepathname 与参数中的: sFileName 不一样,不知道是不是LZ写错了。
      

  4.   

    测试
    VBScript codeSet fs_jb = fs.CreateTextFile(filepathname, True)
      

  5.   


    再测试
    VBScript codeSet 
    fs_jb = fs.CreateTextFile(filepathname, True)
      

  6.   

    测试3
      第二个:你的Function中有个“变量”可能有误! 
    VBScript codeSet fs_jb = fs.CreateTextFile(filepathname, True)  
      这里面的:filepathname 与参数中的: sFileName 不一样,不知道是不是LZ写错了。
      

  7.   

    测试4fs_jb = fs.CreateTextFile(filepathname, True) 
      

  8.   


    Set fs_jb = fs.CreateTextFile(filepathname, True)
      

  9.   


    SET fs_jb = fs.CreateTextFile(filepathname, True)