我有下面的函數需要一個字符串(文件的路徑),並應讀取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。
錯誤在什麼行上產生? –
刪除On error行和下兩行(Const行)並查看發生錯誤的位置。 –
解決了它。我需要使用值-2,而不是-1。 –