2011-12-23 111 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)