vba - 将文件复制到使用表字段作为名称的一部分创建的文件夹中
问题描述
我在这里找到了一些 VBA 例程,并尝试使它们适应我的情况。它工作正常,但是当我尝试将文件复制到使用其名称中的常量“进程”和变量 [ID] 字段创建的文件夹时,我收到一条错误消息。
错误在这一行
FileCopy f.SelectedItems(i), "O:\docs\process\ " & (me.ID)
Private Sub Comando356_Click()
' lets say my current record has an ID field value 345
' The routine will check if folder O:\docs\process345 exists
' If the folder does not exist, then the folder is created:
If
Len(Dir("O:\docs\process" & (Me.ID), vbDirectory)) = 0 Then
MkDir "O:\docs\process" & (Me.ID)
End If
‘ So far it works perfectly: if the folder does not exist, is created
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)
' My problem is the next line: folder O:\docs\process345 exists but I get an error 76 “Path not Found”
FileCopy f.SelectedItems(i), "O:\docs\process" & (me.ID)
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
解决方案
添加斜杠并添加文件名
Private Sub Comando356_Click()
' lets say my current record has an ID field value 345
' The routine will check if folder O:\docs\process345 exists
' If the folder does not exist, then the folder is created:
If
Len(Dir("O:\docs\process" & (Me.ID), vbDirectory)) = 0 Then
MkDir "O:\docs\process" & (Me.ID)
End If
‘ So far it works perfectly: if the folder does not exist, is created
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)
' My problem is the next line: folder O:\docs\process345 exists but I get an error 76 “Path not Found”
' add some debugging
Debug.Print ("in=" & f.SelectedItems(i) & " out=" & "O:\docs\process" & (me.ID) & "\" & sFile)
' add a Slash and add the FileName
FileCopy f.SelectedItems(i), "O:\docs\process" & (me.ID) & "\" & sFile ' <<<<
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
推荐阅读
- mongodb - 如何在 Apollo 解析器(Meteor/Apollo/Graphql)中返回聚合查询?
- javascript - JQuery:一旦元素出现,使用 ClearInterval 停止刷新
- html - 如何在html中进行高级文本调整?
- node.js - Firebase 云函数增量计数器
- c# - C# MongoDB 按计算排序
- python - lmfit 步进函数和步长
- scala - 无法访问资源下的文件(streamlets)
- java - JPA 从现有的 CriteriaQuery 创建计数查询
- c++ - 将标量添加到特征矩阵(向量)
- firebase - Flutter 和 Firebase - Apple 登录不起作用