2017-05-22 40 views
0

我需要这个VBA宏,它允许用户从他们自己的文件夹中选择PDF文件,然后宏应该将PDF复制到固定的文件夹目标并根据在ComboBoxes中选择两个值。VBA复制和重命名用户在对话框中选择的文件

我已经尝试了下面的代码,但在最后一句失败。任何人都可以帮助我?

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:\05_RAP\Klement\Test" 
    End If 
End Sub 
+0

虽然搜索了很多,你找到了什么代码?你尝试过吗?发生了什么? –

回答

0

好了,你想要的步骤是:基于combo boxes

如果您在使用任何代码时遇到任何特定问题,请相应地更新您的问题以获取特定答案。

+0

嗨大卫,谢谢你的回答和快速回复! 我知道代码的步骤,并了解它的各个部分,但正如我所提到的,我退出了新的行列,因此无法确定如何组合各个步骤。 – Dyrlund

+0

我明白了。编辑你的问题并发布你的“个人步骤”,告诉我们他们是否工作以及你试图将他们结合起来。我相信我们可以帮助你。 –

0

这有点旧了,但是如果你仍然在寻找答案,那么就像从msoFileDialogOpen前删除下划线和空格一样简单吗?当我复制并粘贴到Excel中时,它最初不会编译;但纠正后,它运行良好。也就是说,它复制了一个文件,在我创建的K:\05_RAP\Klement\文件夹中将其命名为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:\05_RAP\Klement\Test\" & strFName 

End If 

End Sub 
相关问题