首页 > 解决方案 > 复制和重命名文件

问题描述

我有一个访问数据库(后端)。大小正在急剧增加,我敢肯定是因为我的主表中有一个附件字段。

我决定存储文件路径名而不是文件本身;
我在这里找到了@Fionnuala,这个 VBA 例程和函数对我来说是完美的(我刚刚将 AllowMultiSelect 更改为 FALSE)。

如何将选定的文件复制到网络文件夹,然后使用字段 ID 作为名称的一部分对其进行重命名。喜欢 [ID] 和“lto.pdf”</p>

ID 是我的主表中的一个字段;
复制后的文件名将是O:/DOCS/678LTO.PDF

Private Sub Command7_Click()

Dim f As Object

Set f = Application.FileDialog(3)

f.AllowMultiSelect = FALSE

If f.Show Then
    For i = 1 To f.SelectedItems.Count
        sFile = Filename(f.SelectedItems(i), sPath)
        MsgBox sPath & "---" & sFile

'Here I need to copy the file to O:\DOCS
'And rename it as [ID] & “lto.pdf” resulting O:\DOCS\328LTO.pdf

    Next
End If

End Sub


Public Function Filename(ByVal strPath As String, sPath) As String
    sPath = Left(strPath, InStrRev(strPath, "\"))
    Filename = Mid(strPath, InStrRev(strPath, "\") + 1)
End Function

标签: vbams-access

解决方案


使用文件复制方法

'Here I need to copy the file to O:\DOCS
'And rename it as [ID] & “lto.pdf” resulting O:\DOCS\328LTO.pdf
FileCopy f.SelectedItems(i), "O:\DOCS\" & Your[ID] & "LTO.pdf"

推荐阅读