我需要这个VBA宏,它允许用户从自己的文件夹中选择一个PDF文件,然后宏应该将PDF复制到固定文件夹目标,并根据在ComboBox中选择的两个值重命名文件。
我尝试了以下代码,但在最后一句失败了。有人可以帮助我吗?
Sub add_testrepport()
Dim intChoice As Integer
Dim strPath As String
'allow the user to select one file
Application.FileDialog(msoFileDialogOpen).AllowMultiSelect = False
'make the file dialog visible to the user
intChoice = Application.FileDialog(msoFileDialogOpen).Show
'determine what choice the user made
If intChoice <> 0 Then
'get the file path selected by the user
strPath = Application.FileDialog( _
msoFileDialogOpen).SelectedItems(1)
'copy the file path to filecopy
FileCopy strPath, "K: 5_RAPKlementTest"
End If
End Sub
这有点旧,但如果您仍在寻找答案,那么从msoFileDialogOpen
前面删除下划线和空格一样简单吗? 当我复制并粘贴到 Excel 中时,它最初不会编译;但在纠正这一点后,它运行良好。也就是说,它复制了一个文件,在我创建的K: 5_RAPKlement
文件夹中将其命名为Test
(无扩展名(。
如果您打算将 Test 作为具有文件原始文件名的文件夹,您可以尝试以下代码(将 2 行留空,然后缩进 4 个空格以在 StackOverflow 中发布代码(:
Sub add_testrepport()
Dim intChoice As Integer
Dim strPath As String
Dim strFName As String
'allow the user to select one file
Application.FileDialog(msoFileDialogOpen).AllowMultiSelect = False
'make the file dialog visible to the user
intChoice = Application.FileDialog(msoFileDialogOpen).Show
'determine what choice the user made
If intChoice <> 0 Then
'get the file path selected by the user
strPath = Application.FileDialog(msoFileDialogOpen).SelectedItems(1)
' get file name from path
strFName = Mid(strPath, InStrRev(strPath, "") + 1, Len(strPath))
'copy the file path to filecopy
FileCopy strPath, "K: 5_RAPKlementTest" & strFName
End If
End Sub
,所以你想要的步骤是:
选择一个文件
复制该文件
重命名该文件基于组合框。
如果您在使用任何代码时遇到任何特定问题,请相应地更新您的问题以获得特定答案。