2017-05-22 42 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 
相關問題