ms word - Reading a TXT File with VBA suddenly doesn't work anymore -
i in process of converting old macros word 2003 word 2013. thought have build fancy ribbon bar , done it, apparently not.
the following code supposed read whole content of txt file , put clipboard. how create object , make call function:
dim mydata dataobject set mydata = new dataobject mydata.settext loadtextfile("c:\testdocs\readthisfile.txt") mydata.putinclipboard
and actual function:
public function loadtextfile(sfile string) string dim ifile integer on local error resume next ifile = freefile open sfile input #ifile loadtextfile = input$(lof(ifile), ifile) close #ifile end function
afterwards i'm getting following error:
run-time error '-2147024809 (80070057)': dataobject:settext invalid argument.
and said: code works fine in word 2003 same file.
based on error, suggest read text file 1 line @ time, looping through till eof (end of file). give bit more flexibility. here how need modify loadtextfile
method right,
public function loadtextfile(sfile string) string on error goto errhandler dim ifile integer, strin string, tmpstr string open sfile input #ifile while not eof(ifile) line input #ifile, tmpstr strin = strin & tmpstr loop loadtextfile = strin exithandler: close #ifile exit function errhandler: msgbox "error (" & err.number & ") - " & err.description & vbcrlf & vbcrlf & "exiting loadtextfile method.", vbinformation resume exithandler end function
Comments
Post a Comment