Make Excel VBA wait until operation is finished

Wannes Demaeght

I have made a macro in VBA to split PDF files into single pages.

Sub SplitFiles()

'This needs PDFTK installed

Dim fso As New FileSystemObject
Dim fol As Folder
Dim fil As File
Dim i As Integer
Dim command As String

On Error Resume Next

Set fol = fso.GetFolder("C:\Users\wanne\OneDrive\Documents\Bijberoep\Scans\Splitsen\")

i = 1

For Each fil In fol.Files
    command = ("C:\Program Files (x86)\PDFtk\bin\pdftk.exe " & fil & " burst output C:\Users\wanne\OneDrive\Documents\Bijberoep\Scans\Klaar\Klaar" & i & "-%d.pdf")
    Shell command
    i = i + 1
Next fil

Application.Wait (Now + TimeValue("0:00:05"))

Kill "C:\Users\wanne\OneDrive\Documents\Bijberoep\Scans\Splitsen\*.pdf"
Kill "C:\Users\wanne\OneDrive\Documents\Bijberoep\Scans\Klaar\*.txt"

End Sub

The problem is that I had to give a timeout of 5 seconds, or else the files get deleted immediately, and no output is created.

How can I make excel wait until all files are processed?

Wannes Demaeght

Thanks Nathan, you've put me on the right path:

Sub SplitFiles()

'This needs PDFTK installed

Dim fso As New FileSystemObject
Dim fol As Folder
Dim fil As File
Dim i As Integer
Dim command As String
Dim number As Long

On Error Resume Next

Set fol = fso.GetFolder("C:\Users\wanne\OneDrive\Documents\Bijberoep\Scans\Splitsen\")
number = fol.Files.Count

i = 1
For Each fil In fol.Files
    command = ("C:\Program Files (x86)\PDFtk\bin\pdftk.exe " & fil & " burst output C:\Users\wanne\OneDrive\Documents\Bijberoep\Scans\Klaar\Klaar" & i & "-%d.pdf")
    Shell command
    If i = number Then
        Application.Wait (Now + TimeValue("0:00:02"))
        Kill "C:\Users\wanne\OneDrive\Documents\Bijberoep\Scans\Splitsen\*.*"
        Kill "C:\Users\wanne\OneDrive\Documents\Bijberoep\Scans\Klaar\*.txt"
        Shell ("C:\Program Files (x86)\View and Rename PDF\viewAndRename.exe")
    Else
    i = i + 1
    End If
Next fil

End Sub

This code works, but I hade to declare a variable as long first, or it would get stuck in a loop.

Still a timeout of 2 seconds, because not all files got deleted without the delay (probably because they were still being processed by pdftk)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

How to wait until all async calls are finished

From Dev

Make slideToggle wait until other div's slideUps have finished?

From Dev

Wait until forEach loop is finished?

From Dev

Wait until function is finished

From Dev

How to make python wait until the previous task is finished?

From Dev

Wait until NSURLConnection sendAsynchronousRequest is finished

From Dev

how to wait until a web request with HttpWebRequest is finished?

From Dev

On click myFunction and wait until it is finished

From Dev

How to wait until some jQuery methods are finished?

From Dev

wait until completion block not finished in AFJSONRPCClient request

From Dev

How to wait until an animation is finished in Swift?

From Dev

Sequelize wait until loop finished with callback

From Dev

Wait until gobalEval has finished

From Dev

How to wait until my batch file is finished

From Dev

How to wait until networking by Alamofire has finished?

From Dev

C# .Net - How to make application wait until all threads created in Library are finished

From Dev

How do I make an Excel RefreshAll wait to close until finished?

From Dev

Android AsyncTask wait until finished

From Dev

How to make Gulp wait until dest file is finished?

From Dev

How to wait until all NSOperations is finished?

From Dev

Wait until my thread is finished

From Dev

jQuery mobile: Wait until $.getJSON is finished

From Dev

How to make python wait until the previous task is finished?

From Dev

looping for jquery load and wait until finished

From Dev

wait until completion block not finished in AFJSONRPCClient request

From Dev

Wait until gobalEval has finished

From Dev

Wait until process is finished running

From Dev

How to make JS wait until protocol execution finished

From Dev

How to make a python Script wait until download has finished

Related Related

  1. 1

    How to wait until all async calls are finished

  2. 2

    Make slideToggle wait until other div's slideUps have finished?

  3. 3

    Wait until forEach loop is finished?

  4. 4

    Wait until function is finished

  5. 5

    How to make python wait until the previous task is finished?

  6. 6

    Wait until NSURLConnection sendAsynchronousRequest is finished

  7. 7

    how to wait until a web request with HttpWebRequest is finished?

  8. 8

    On click myFunction and wait until it is finished

  9. 9

    How to wait until some jQuery methods are finished?

  10. 10

    wait until completion block not finished in AFJSONRPCClient request

  11. 11

    How to wait until an animation is finished in Swift?

  12. 12

    Sequelize wait until loop finished with callback

  13. 13

    Wait until gobalEval has finished

  14. 14

    How to wait until my batch file is finished

  15. 15

    How to wait until networking by Alamofire has finished?

  16. 16

    C# .Net - How to make application wait until all threads created in Library are finished

  17. 17

    How do I make an Excel RefreshAll wait to close until finished?

  18. 18

    Android AsyncTask wait until finished

  19. 19

    How to make Gulp wait until dest file is finished?

  20. 20

    How to wait until all NSOperations is finished?

  21. 21

    Wait until my thread is finished

  22. 22

    jQuery mobile: Wait until $.getJSON is finished

  23. 23

    How to make python wait until the previous task is finished?

  24. 24

    looping for jquery load and wait until finished

  25. 25

    wait until completion block not finished in AFJSONRPCClient request

  26. 26

    Wait until gobalEval has finished

  27. 27

    Wait until process is finished running

  28. 28

    How to make JS wait until protocol execution finished

  29. 29

    How to make a python Script wait until download has finished

HotTag

Archive