北京 | 上海 | 天津 | 重庆 | 广州 | 深圳 | 珠海 | 汕头 | 佛山 | 中山 | 东莞 | 南京 | 苏州 | 无锡 | 常州 | 南通 | 扬州 | 徐州 | 杭州 | 温州 | 宁波 | 台州 | 福州 | 厦门 | 泉州 | 龙岩 | 合肥 | 芜湖 | 成都 | 遂宁 | 长沙 | 株洲 | 湘潭 | 武汉 | 南昌 | 济南 | 青岛 | 烟台 | 潍坊 | 淄博 | 济宁 | 太原 | 郑州 | 石家庄 | 保定 | 唐山 | 西安 | 大连 | 沈阳 | 长春 | 昆明 | 兰州 | 哈尔滨 | 佳木斯 | 南宁 | 桂林 | 海口 | 贵阳 | 西宁 | 乌鲁木齐 | 包头 |
老师,您好。
我想在新增窗体里加入上传文件的功能,将文件上传到指定的文件夹。
代码是:
Public Function UploadFile(strDestinationPath As String, _
FileType As acFileType, _
Optional bIsUpload As Boolean = True) As String
On Error GoTo ErrorHandler
Dim strSourceFile As String
Dim strDestinationFile As String
Dim strFullFileName As String
Dim FSO As Object
If Right$(strDestinationPath, 1) <> "\" Then
strDestinationPath = strDestinationPath & "\"
End If
Set FSO = CreateObject("Scripting.FileSystemObject")
If Not FSO.FolderExists(strDestinationPath) Then
MsgBox "This path[" & strDestinationPath & "]is not found,please check the path!", vbCritical, "????"
Set FSO = Nothing
Exit Function
End If
With Application.FileDialog(3)
.Title = "Choose File"
.InitialFileName = ""
.Filters.Clear
Select Case FileType
Case 1
.Filters.Add "Graphics Files", "*.jpg;*.bmp;*.gif;*.png"
Case 2
.Filters.Add "Files", "*.pdf;*.doc;*.docx;*.xls;*.rar"
Case Else
.Filters.Add "All Files", "*.*"
End Select
.AllowMultiSelect = False
If .Show Then
strSourceFile = .SelectedItems.Item(1)
strDestinationFile = strDestinationPath & Mid$(strSourceFile, InStrRev(strSourceFile, "\") + 1)
strFullFileName = Mid$(strSourceFile, InStrRev(strSourceFile, "\") + 1)
If bIsUpload = True Then
If FSO.FileExists(strDestinationFile) = True Then
If MsgBox("File Exists,overwrite it?", vbOKCancel + vbInformation, "??") = vbOK Then
FSO.copyfile strSourceFile, strDestinationFile, True
Else
Exit Function
End If
Else
FSO.copyfile strSourceFile, strDestinationFile
End If
Set FSO = Nothing
UploadFile = strDestinationFile
Else
UploadFile = strSourceFile
End If
End If
End With
ExitHere:
Set FSO = Nothing
Exit Function
ErrorHandler:
MsgBox Err.Description, vbInformation, "??"
Resume ExitHere
End Function
Private Sub cmd_addImg_Click()
Dim strDestinationPath As String
Dim strReturnFile As String
strDestinationPath = CurrentProject.Path & "\NDA\"
strReturnFile = UploadFile(strDestinationPath, acPicture, True)
If Len(strReturnFile) > 0 Then
Me.imgPath = strReturnFile
Me.Image0.Picture = strReturnFile
End If
End Sub
Private Sub Image0_DblClick(Cancel As Integer)
ShellEx Me.Image0.Picture
End Sub
出现这个错误: