2017-01-03 1 views
0

최근 Microsoft Word에서 일상 작업 중 일부를 자동화하기 위해 vba를 실험하기 시작했습니다. 사용자가 대상 폴더 및 선택한 대상 폴더에 복사 할 파일 (.doc)을 선택할 수 있도록하는 코드 작업 중입니다.vba를 사용하여 Microsoft Word 파일을 다른 폴더에 붙여 넣기 복사

다음 코드는 오류없이 실행되지만 파일은 대상 폴더에 붙여 넣기되지 않습니다.

이 분의 문제를 해결하는 데 많은 도움을 주시면 대단히 감사하겠습니다.

감사합니다,

데릭

Sub copydocs() 

Dim items As Long 
Dim file_path As Variant 
Dim folder_path As Variant 

    'Ask user for input' 

    items = InputBox("Give me some input") 


    'Select Destination Folder 
    With Application.FileDialog(msoFileDialogFolderPicker) 
     folder_path = .SelectedItems(1) 
     .Show 
    End With 


    ' Open the file dialog 
    For i = 1 To items 
     With Application.FileDialog(msoFileDialogFilePicker) 
      .AllowMultiSelect = True 
      .Show 
      file_path = .SelectedItems(1) 
     End With 

     ' Copy paste  
     Dim fs As Object 
     Set fs = CreateObject("Scripting.FileSystemObject") 
     fs.CopyFile file_path, folder_path 
     Set fs = Nothing 

    Next i  

End Sub 

답변

0

는 문제의 몇 가지가 있습니다.

변수 i는 어디에도 선언되어 있지 않습니다.

대화 상자가 반환되기 전에 폴더 경로를 저장하려고했습니다.

Sub copydocs() 
    Dim i As Integer    ' CHANGE: New declare. 
    Dim fs As Object    ' CHANGE: Moved to top. 
    Dim items As Long 
    Dim file_path As Variant 
    Dim folder_path As Variant 

    'Ask user for input. 
    items = InputBox("Give me some input") 

    'Select Destination Folder 
    With Application.FileDialog(msoFileDialogFolderPicker) 
     ' CHANGE: Switched order of next two lines. 
     .Show 
     folder_path = .SelectedItems(1) 
    End With 

    ' Open the file dialog 
    For i = 1 To items 
     With Application.FileDialog(msoFileDialogFilePicker) 
      .AllowMultiSelect = True 
      .Show 
      file_path = .SelectedItems(1) 
     End With 

     ' Copy paste 
     Set fs = CreateObject("Scripting.FileSystemObject") 
     fs.CopyFile file_path, folder_path 
     Set fs = Nothing 
    Next i 
End Sub 
관련 문제