2014-05-25 41 views
0

我有下面的函數需要一個字符串(文件的路徑),並應讀取unicode(因爲我得到字形被返回,並懷疑它是字符編碼是錯誤的 - http://msdn.microsoft.com/en-us/library/office/gg278834(v=office.15).aspx)。VBA中的無效過程調用錯誤

代碼錯誤與5 Invalid procedure call or argument

這裏的功能:

Function readFile(pathIn As String) As String 
On Error GoTo err_handle: 
    Const ForReading = 1 
    Const TristateTrue = -1 

    Dim oFSO As FileSystemObject 
    Set oFSO = New FileSystemObject 

    Dim oFS As TextStream 

    If oFSO.FileExists(pathIn) Then 
     Set oFS = oFSO.OpenTextFile(pathIn, ForReading, False, TristateTrue) 
     GoTo return_string: 
    Else 
     Debug.Print ("inVar file cannot exist!") 
     readFile = "ERROR" 
    End If 

    Exit Function 

return_string: 
    readTxtFile = oFS.ReadAll 
    oFS.Close 
    Exit Function 
err_handle: 
    Debug.Print ("Error in readFile()[B]: " & Err.Number & " " & Err.Description) 
    readFile = "ERROR" 
    oFS.Close 
    Exit Function 

End Function 

任何想法?

答:

需要使用-2,而不是-1。

+0

錯誤在什麼行上產生? –

+0

刪除On error行和下兩行(Const行)並查看發生錯誤的位置。 –

+0

解決了它。我需要使用值-2,而不是-1。 –

回答

0

常量TristateTrue = -1

你必須爲你的TristateTrue常錯誤的值。對於Unicode,它應該是1。 -1不是possible values之一。

+0

它是-1在MSoft的文章:http://msdn.microsoft.com/en-us/library/office/gg278834(v=office.15).aspx –

+0

所以它是!情節變濃了。顯然MS在某個時候改變了它......所以它將取決於你註冊的COM對象的版本。我認爲你正在嘗試其他價值觀? –

+0

COM對象?我需要什麼版本?你在談論對象庫(工具>參考),是嗎? –

0

需要使用-2而不是-1,但不知道這是否是unicode。我想要做的只是以UTF-8格式閱讀。 -2根據上面鏈接的文章對應於系統默認值。我不知道這是什麼,但它在任何情況下都有效。

相關問題