我使用以下代码尝试创建.txt文件并将其上传到我的FTP服务器。我想创建一个名为Ref.txt
的文本文件,并将一些单元格值插入到文本文件中,然后将其上传到我的FTP服务器。出于某种原因,我没有收到任何错误,但文本文件没有上传到我的FTP服务器。
有人可以告诉我我哪里出错吗?
我想在用户Ref.txt
驱动器上创建名为P:/
的临时文本文件,然后打印irange("A1").value
。
Dim vPath As String
Dim vFile As String
Dim vFTPServ As String
Dim fNum As Long
vPath = "P:\"
vFile = Ref & ".txt"
vFTPServ = "********"
'Mounting file command for ftp.exe
fNum = FreeFile()
Open "P:\" & Ref & ".txt" For Output As #fNum
Print #1, "hewdenportal.co.uk" ' your login and password"
Print #1, "/public_html/ns_requests" 'change to dir on server
Print #1, "bin" ' bin or ascii file type to send
Print #1, "put " & vPath & "\" & vFile & " " & vFile ' upload local filename to server file
Print #1, "close" ' close connection
Print #1, "quit" ' Quit ftp program
Close
Shell "ftp -n -i -g -s:" & vPath & Ref & ".txt" & vFTPServ, vbNormalNoFocus
SetAttr vPath & Ref & ".txt", vbNormal
Kill vPath & Ref & ".txt"
答案 0 :(得分:0)
代码fNum
是文件的句柄,因此您应该在打印和关闭文件时使用它。在您的代码中将Print #1
更改为Print #fNum
。
同时将Close
更改为Close #fNum
。
在行Shell "ftp -n -i -g -s:" & vPath & Ref & ".txt" & vFTPServ, vbNormalNoFocus
中,-s
参数告诉ftp执行文件的内容。所以看起来你正在创建文件,然后告诉ftp使用文件中的命令上传文件?
如果要将范围的值写入文件,则Print #fNum Workbooks("book_name.xlsm").Worksheets("sheet_name").Range("A1").Value
将执行该操作。如果您以这种方式完全限定单元格地址,则始终知道您正在使用哪个工作簿和工作表。