2011-12-23 155 views
0

我想使用VBA编写文件。我的代码如下。它第一次工作,但当我关闭Excel文件(.xlsm)并尝试再次使用它时,它不起作用。错误写入文件vba

运行宏,但新的文件没有出现

Sub LogInformation(LogMessage As String) 
    Const LogFileName As String = "TEXTFILE.db" 
    Dim FileNum As Integer 
    FileNum = FreeFile ' next file number 
    Open LogFileName For Append As #FileNum ' creates the file if it doesn't exist 
    Print #FileNum, LogMessage ' write information at the end of the text file 
    Close #FileNum ' close the file 
End Sub 
+0

是否因为'FileNum = FreeFile'总是找到'Next'文件号? – user959631 2011-12-23 21:30:58

+0

It aways returns 1 – Makah 2011-12-23 21:32:42

+0

Ohh好吧,我真的不知道VBA很多,抱歉Ohh刚刚意识到,它只在文件不存在时才创建文件。也许这就是问题所在? – user959631 2011-12-23 21:34:34

回答

3

对不起球员,当我没有得到任何错误。那是愚蠢的。 默认相对路径位于User /%MyUser%/ Documents中。

我需要使用ChDir然后打开/写入/关闭文件。

Chdir(ActiveWorkbook.Path)