Ms access VBA-浏览、选择文件并将文件另存为
我正在用表单和报表创建access数据库。在我的一个表单中,我希望能够转到一个文件夹,选择一个文件(.pdf、.doc、.xls),然后将其保存到一个专用的“附件”文件夹中 我知道OLE对象和附件函数,但这两个都是在数据库perse中保存附件。我试图远离这一点,因为有必要随时访问附件,并使数据库更轻 我一直在玩VBA游戏Ms access VBA-浏览、选择文件并将文件另存为,ms-access,vba,savefiledialog,save-as,Ms Access,Vba,Savefiledialog,Save As,我正在用表单和报表创建access数据库。在我的一个表单中,我希望能够转到一个文件夹,选择一个文件(.pdf、.doc、.xls),然后将其保存到一个专用的“附件”文件夹中 我知道OLE对象和附件函数,但这两个都是在数据库perse中保存附件。我试图远离这一点,因为有必要随时访问附件,并使数据库更轻 我一直在玩VBA游戏 Application.FileDialog(msoFileDialogFilePicker) 及 这是我迄今为止所做的,但它不起作用: Option Compare Dat
Application.FileDialog(msoFileDialogFilePicker)
及
这是我迄今为止所做的,但它不起作用:
Option Compare Database
Private Sub Select_Save_Click()
Call SelectFile
End Sub
Public Function SelectFile() As String
Dim FD As FileDialog
Dim File_Name As String
Dim path As String
path = "O:\foldername"
Set FD = Application.FileDialog(msoFileDialogFilePicker)
With FD
.AllowMultiSelect = False
.Title = "Please select file to save as attachment"
If .Show = True Then
File_Name = Dir(.SelectedItems(1))
SelectFile = .SelectedItems(1)
new_name = path & File_Name
.SelectedItems.Item(1).SaveAsFile new_name
Me.Attach_Save = new_name
Else
Exit Function
End If
Set FD = Nothing
End With
End Function
请考虑使用as.SaveAsFile方法在FD对象内可能不起作用。另外,主要与使用电子邮件附件的MS Outlook VBA关联
另外,在FD对象外部的字符串变量上使用Dir()
,而不是在变量数组上使用。在FD对象内部选择EditEMS(1)
最后,请注意,我在路径字符串中添加了最后一个反斜杠
Dim FD As FileDialog
Dim File_Name As String
Dim path As String
path = "O:\foldername\"
Set FD = Application.FileDialog(msoFileDialogFilePicker)
With FD
.AllowMultiSelect = False
.Title = "Please select file to save as attachment"
If .Show = True Then
SelectFile = .SelectedItems(1)
Else
Exit Function
End If
End With
File_Name = Dir(SelectFile)
FileCopy SelectFile, path_name & File_Name
Set FD = Nothing
请告诉我们更多关于不工作在这里意味着什么。错误消息?
Dim FD As FileDialog
Dim File_Name As String
Dim path As String
path = "O:\foldername\"
Set FD = Application.FileDialog(msoFileDialogFilePicker)
With FD
.AllowMultiSelect = False
.Title = "Please select file to save as attachment"
If .Show = True Then
SelectFile = .SelectedItems(1)
Else
Exit Function
End If
End With
File_Name = Dir(SelectFile)
FileCopy SelectFile, path_name & File_Name
Set FD = Nothing