vba上传指定文件ftp服务器

一、需求分析:

  • 在excel表格中指定文件路径,将指定文件上传ftp服务器

二、操作流程:

2.1 【开发工具】-【宏】

在这里插入图片描述

2.2 【宏】-【编辑】

在这里插入图片描述

2.3 【把脚本复制进去】

在这里插入图片描述

2.4 脚本如下

Sub 按钮1_Click()
Dim i, str1, str2, str3, str4, str5, str6, str7, str8, str9, str10, d
Dim myarray()
On Error Resume Next
Set mysheet1 = ThisWorkbook.Worksheets("sheetName自定义")
Set fs = CreateObject("Scripting.FileSystemObject")

'获取本地路径
If mysheet1.Cells(2, 3) <> "" Then
str3 = Replace(Sheet1.Cells(2, 3), "/", "\")
str3 = Trim(str3)
If Right(str3, 1) <> "\" Then
str3 = str3 & "\"
'MsgBox str6
End If
End If

'循环扫描文件名,生成一个只有文件名字的字符串
For i = 4 To 100
If mysheet1.Cells(i, 3) <> "" Then
str1 = Replace(Sheet1.Cells(i, 3), "/", "\")
str1 = Trim(str1)
str4 = str3 & str1
str5 = "Echo mput " & Chr(34) & str4 & Chr(34) & " >>ftp.up"
str9 = str9 & " " & str5 'str9所有要上传的文件
End If
Next
'MsgBox str9

'上传
Set fsd = CreateObject("Scripting.FileSystemObject")
str10 = str3 & "1.bat" '脚本
str11 = "Echo open IP地址>ftp.up" '远程路径
str12 = "Echo 用户名>>ftp.up" '账号
str13 = "Echo 密码>>ftp.up" '密码

Set fid = fsd.CreateTextFile(str10, True) '后面开始写脚本
fid.WriteLine ("@Echo Off ") '开远程
fid.WriteLine (str11)
fid.WriteLine (str12)
fid.WriteLine (str13)
fid.WriteLine ("Echo Cd .\User >>ftp.up")
fid.WriteLine ("Echo binary>>ftp.up")
fid.WriteLine ("Echo prompt >>ftp.up")
fid.WriteLine ("Echo lcd " & Chr(34) & str3 & Chr(34) & ">>ftp.up")
fid.WriteLine (str9)
fid.WriteLine ("Echo bye>>ftp.up")
fid.WriteLine ("FTP -s:ftp.up")
fid.WriteLine ("del ftp.up /q")
fid.Close
str16 = "cmd.exe /c " & str10 '运行脚本
'MsgBox str16
Shell str16

 MsgBox "传输完成"
End Sub

2.5 修改位置

2.5.1 修改sheet名称和表格一致

在这里插入图片描述

2.5.2 修改Cells(2,3)
  • 指的是地2行
    在这里插入图片描述
2.5.3 修改4 to 100
  • 4指的是从第4行的开始
  • 100指的是从第100行开始
    在这里插入图片描述
2.5.4 修改Cells(i,3)
  • i指的是行,3指的是第几列
2.5.5 修改发ftp信息
  • ip地址
  • 用户名
  • 密码

在这里插入图片描述

2.5.6 保存脚本

在这里插入图片描述

三、添加上传按钮

  • 【开发工具】-【插入表单控件】
    在这里插入图片描述
    -【指定宏】
    在这里插入图片描述
    -【选择指定的宏名】
    在这里插入图片描述
  • 【双击修改按钮名称】

效果图:

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

校验版本

Sub 文件上传ftp服务器()
Dim i, str1, str2, str3, str4, str5, str6, str7, str8, str9, str10, d
Dim myarray(), MyFile As Object
Set MyFile = CreateObject("Scripting.FileSystemObject")
On Error Resume Next
Set mysheet1 = ThisWorkbook.Worksheets("sheetName自定义")
Set fs = CreateObject("Scripting.FileSystemObject")

'获取本地路径
If mysheet1.Cells(2, 3) <> "" Then
str3 = Replace(Sheet1.Cells(2, 3), "/", "\")
str3 = Trim(str3)
If Right(str3, 1) <> "\" Then
str3 = str3 & "\"
'MsgBox str6
End If
End If

'循环扫描文件名,生成一个只有文件名字的字符串
For i = 4 To 100
If mysheet1.Cells(i, 3) <> "" Then
str1 = Replace(Sheet1.Cells(i, 3), "/", "\")
str1 = Trim(str1)
str4 = str3 & str1

If MyFile.FileExists(str4) = True Then
Else
MsgBox str4 & " 文件不存在"
End If

str5 = "Echo mput " & Chr(34) & str4 & Chr(34) & " >>ftp.up"
str9 = str9 & " " & str5 'str9所有要上传的文件
End If
Next
'MsgBox str9



'上传
Set fsd = CreateObject("Scripting.FileSystemObject")
str10 = str3 & "1.bat" '脚本
str11 = "Echo open ip地址>ftp.up" '远程路径
str12 = "Echo 用户名>>ftp.up" '账号
str13 = "Echo 密码>>ftp.up" '密码

Set fid = fsd.CreateTextFile(str10, True) '后面开始写脚本
fid.WriteLine ("@Echo Off ") '开远程
fid.WriteLine (str11)
fid.WriteLine (str12)
fid.WriteLine (str13)
fid.WriteLine ("Echo Cd .\User >>ftp.up")
fid.WriteLine ("Echo binary>>ftp.up")
fid.WriteLine ("Echo prompt >>ftp.up")
fid.WriteLine ("Echo lcd " & Chr(34) & str3 & Chr(34) & ">>ftp.up")
fid.WriteLine (str9)
fid.WriteLine ("Echo bye>>ftp.up")
fid.WriteLine ("FTP -s:ftp.up")
fid.WriteLine ("del ftp.up /q")
fid.Close
str16 = "cmd.exe /c " & str10 '运行脚本
'MsgBox str16
Shell str16

 MsgBox "传输完成"
End Sub

无校验版本

Sub 按钮1_Click()
Dim i, str1, str2, str3, str4, str5, str6, str7, str8, str9, str10, d
Dim myarray()
On Error Resume Next
Set mysheet1 = ThisWorkbook.Worksheets("sheetName自定义")
Set fs = CreateObject("Scripting.FileSystemObject")

'获取本地路径
If mysheet1.Cells(2, 3) <> "" Then
str3 = Replace(Sheet1.Cells(2, 3), "/", "\")
str3 = Trim(str3)
If Right(str3, 1) <> "\" Then
str3 = str3 & "\"
'MsgBox str6
End If
End If

'循环扫描文件名,生成一个只有文件名字的字符串
For i = 4 To 100
If mysheet1.Cells(i, 3) <> "" Then
str1 = Replace(Sheet1.Cells(i, 3), "/", "\")
str1 = Trim(str1)
str4 = str3 & str1
str5 = "Echo mput " & Chr(34) & str4 & Chr(34) & " >>ftp.up"
str9 = str9 & " " & str5 'str9所有要上传的文件
End If
Next
'MsgBox str9



'上传
Set fsd = CreateObject("Scripting.FileSystemObject")
str10 = str3 & "1.bat" '脚本
str11 = "Echo open ip地址>ftp.up" '远程路径
str12 = "Echo 用户名>>ftp.up" '账号
str13 = "Echo 密码>>ftp.up" '密码

Set fid = fsd.CreateTextFile(str10, True) '后面开始写脚本
fid.WriteLine ("@Echo Off ") '开远程
fid.WriteLine (str11)
fid.WriteLine (str12)
fid.WriteLine (str13)
fid.WriteLine ("Echo Cd .\User >>ftp.up")
fid.WriteLine ("Echo binary>>ftp.up")
fid.WriteLine ("Echo prompt >>ftp.up")
fid.WriteLine ("Echo lcd " & Chr(34) & str3 & Chr(34) & ">>ftp.up")
fid.WriteLine (str9)
fid.WriteLine ("Echo bye>>ftp.up")
fid.WriteLine ("FTP -s:ftp.up")
fid.WriteLine ("del ftp.up /q")
fid.Close
str16 = "cmd.exe /c " & str10 '运行脚本
'MsgBox str16
Shell str16

 MsgBox "传输完成"
End Sub
'模板: Option Explicit Public Declare Function InternetOpen Lib "wininet.dll" Alias "InternetOpenA" _ (ByVal sAgent As String, ByVal LAccessType As Long, ByVal sProxyName As String, _ ByVal SProxyBypass As String, ByVal lFlags As Long) As Long Public Declare Function InternetConnect Lib "wininet.dll" Alias "InternetConnectA" _ (ByVal hInternetSession As Long, ByVal sServerName As String, _ ByVal nServerPort As Integer, ByVal sUsername As String, _ ByVal sPassword As String, ByVal lService As Long, _ ByVal lFlags As Long, ByVal lContext As Long) As Long Public Declare Function FtpGetFile Lib "wininet.dll" Alias "FtpGetFileA" _ (ByVal hFtpSession As Long, ByVal lpszRemoteFile As String, _ ByVal lpszNewFile As String, ByVal fFailIfExists As Boolean, _ ByVal dwFlagsAndAttributes As Long, ByVal dwFlags As Long, _ ByVal dwContext As Long) As Boolean Public Declare Function FtpPutFile Lib "wininet.dll" Alias "FtpPutFileA" _ (ByVal hFtpSession As Long, ByVal lpszLocalFile As String, _ ByVal lpszRemoteFile As String, ByVal dwFlags As Long, _ ByVal dwContext As Long) As Boolean Public Declare Function FtpDeleteFile Lib "wininet.dll" Alias "FtpDeleteFileA" _ (ByVal hFtpSession As Long, ByVal lpszFileName As String) As Boolean Public Declare Function FtpRenameFile Lib "wininet.dll" Alias "FtpRenameFileA" _ (ByVal hFtpSession As Long, ByVal lpszExsiting As String, ByVal lpszNew As String) As Boolean Public Declare Function InternetCloseHandle Lib "wininet.dll" (ByVal hInet As Long) As Integer Public Declare Function FtpFindFirstFile Lib "wininet.dll" Alias "FtpFindFirstFileA" _ (ByVal hFtpSession As Long, ByVal lpszSearchFile As String, _ lpFindFileData As WIN32_FIND_DATA, ByVal dwFlags As Long, _ ByVal dwContent As Long) As Long Public Declare Function InternetFindNextFile Lib "wininet.dll" Alias "InternetFindNextFileA" _ (ByVal hFind As Long, lpvFndData As WIN32_FIND_DATA) As Long Public Type FILETIME dwLowDateTime As Long dwHighDateTime As Long End Type Public Type WIN32_FIND_DATA dwFilAttributes As Long ftCreationTime As FILETIME ftLastAccessTime As FILETIME ftLastWriteTime As FILETIME nFileSizeHigh As Long nFileSizeLow As Long dwReserved0 As Long dwReserved1 As Long cFileName As String * 260 cAlternate As String * 14 End Type Public Declare Function GetOpenFileName Lib "comdlg32.dll" Alias _ "GetOpenFileNameA" (pOpenfilename As OPENFILENAME) As Long Type OPENFILENAME lStructSize As Long hwndOwner As Long hInstance As Long lpstrFilter As String lpstrCustomFilter As String nMaxCustFilter As Long nFilterIndex As Long lpstrFile As String nMaxFile As Long lpstrFileTitle As String nMaxFileTitle As Long lpstrInitialDir As String lpstrTitle As String flags As Long nFileOffset As Integer nFileExtension As Integer lpstrDefExt As String lCustData As Long lpfnHook As Long lpTemplateName As String End Type '窗体: Private Sub Command1_Click() 'FTP下载 lnginet = InternetOpen(vbNullString, INTERNET_OPEN_TYPE_PRECONFIG, _ vbNullString, vbNullString, 0&) If lnginet Then lnginetconn = InternetConnect(lnginet, "219.131.192.243", 0, _ "posui", "djposui", 1, 0, 0) If lnginetconn Then blnRC = FtpGetFile(lnginetconn, "/load.txt", "c:\load.txt", 0, 0, 1, 0) If blnRC Then MsgBox "download ok!!!" End If InternetCloseHandle lnginetconn InternetCloseHandle lnginet MsgBox "close ok!!!" Else MsgBox "can't connect" End If Else MsgBox "ftp wrong" End If End Sub Private Sub Command2_Click() 'FTP上传 lnginet = InternetOpen(vbNullString, INTERNET_OPEN_TYPE_PRECONFIG, _ vbNullString, vbNullString, 0&) If lnginet Then lnginetconn = InternetConnect(lnginet, "219.131.192.243", 0, _ "administrator", "vai8888", 1, 0, 0) If lnginetconn Then blnRC = FtpPutFile(lnginetconn, "c:\1.txt", "/1.txt", 0, 0) If blnRC Then MsgBox "download ok!!!" End If InternetCloseHandle lnginetconn InternetCloseHandle lnginet MsgBox "close ok!!!" Else MsgBox "can't connect" End If Else MsgBox "ftp wrong" End If End Sub 方法2: '部件INET Private Sub Command1_Click() Me.Inet1.Execute Me.Inet1.URL, "send c:\11.txt /1.txt" '保存 End Sub Private Sub Command2_Click() Me.Inet1.Execute Me.Inet1.URL, "get /2.txt c:\2.txt" '下载 End Sub
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

gblfy

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值