2017-02-08 7 views
3

マクロは2ヶ月からうまく動作していますが、今は別の問題のためにいくつかの助けが必要です。 当社のサーバ上にコントローラを搭載しており、pdfを添付してお客様にメールを送信しています。今、このコントローラーと私のマクロは時々同時に動いています。私のマクロがpdfsを作成している時、コントローラーはそれを送ろうとしていますが、すでに作成されているので、できません。 今、私はマクロがpdfを別のフォルダに保存できると思って、その後、すべてのファイルを正しいフォルダにコピーして送信します。VBAコピーでフォルダ内のすべてのファイルを貼り付けます

Function Copy() 

    Dim MyFile2 As Sting 
    Dim myPath2 As String, myPath3 As String 

    myPath2 = "L:\Host_Export\Pdf-Kundenmail\Test\" 
    myPath3 = "L:\Host_Export\Pdf-Kundenmail\" 
    MyFile2 = Dir(myPath2 & "*.*") 
    Do 
     If MyFile2 = "" Then Exit Do 
     FileCopy myPath2 & MyFile2, myPath3 & MyFile2 
     End If 
     myFile2 = Dir 
    Loop 

End Function 

しかし、私はそれを実行した場合にエラーがあります:

私のコードはこれです、このようなerror on compilation userdefined typ could not be defined.https://i0.wp.com/www.port135.com/wp-content/uploads/2012/08/error1-1.png。 私はalredyグーグルが、この問題を解決するために何かを設定したりインポートする方法を取得しないでください。

+0

myfile2 = dirはどういう意味ですか? – User632716

+0

このコードの3行目に「Dim MyFile2 As String'」というエラーが表示されます。私はエラーのために "Do"パートを試みることはできませんでした、私はそれを切り替えます。ああ、これは別の間違いです 'myfile2 = dir'、ありがとう! – Patrick

+2

'Dim MyFile2 As Sting' - >' Dim MyFile2 As String'です。そのようなミスタイプを避けるために 'Option Explicit'を使用してください – user3598756

答えて

1

あなたのコードは、あなたが間違った文字列を綴ったので、@ user3598756言ったようにコンパイルされません。フォームを改善するには、do whileループを使用してif文とdo文を結合します。

Function Copy() 
    Dim MyFile2 As String 
    Dim myPath2 As String, myPath3 As String 

    myPath2 = "L:\Host_Export\Pdf-Kundenmail\Test\" 
    myPath3 = "L:\Host_Export\Pdf-Kundenmail\" 
    MyFile2 = Dir(myPath2 & "*.*") 
    Do while MyFile2 <> "" 
     FileCopy myPath2 & MyFile2, myPath3 & MyFile2 
     myFile2 = Dir 
    Loop 
End Function 
+0

これは私のコードの代わりに簡単です。 – Patrick

1

以下のサブフォルダは、すべてのファイルをソースフォルダからコピー先フォルダにコピーします。

Sub AllFiles() 
    Dim FSO As Object 
    Dim FromPath As String 
    Dim ToPath As String 

     FromPath = "C:\Users\Alam\Music\Awlad Hossain" 'Souece Folder 
     ToPath = "C:\MyExcelFiles" 'Destination folder 

      If Right(FromPath, 1) = "\" Then 
       FromPath = Left(FromPath, Len(FromPath) - 1) 
      End If 

      If Right(ToPath, 1) = "\" Then 
       ToPath = Left(ToPath, Len(ToPath) - 1) 
      End If 

     Set FSO = CreateObject("scripting.filesystemobject") 

      If FSO.FolderExists(FromPath) = False Then 
       MsgBox FromPath & " doesn't exist" 
       Exit Sub 
      End If 

     FSO.CopyFolder Source:=FromPath, Destination:=ToPath 
     MsgBox "You can find the files and subfolders from " & FromPath & " in " & ToPath 

    End Sub 

More details here:

関連する問題