+ Reply to Thread
Results 1 to 11 of 11

VBA Shell Command Assistance Needed

  1. #1
    Forum Contributor
    Join Date
    05-13-2010
    Location
    Dallas, TX
    MS-Off Ver
    2019
    Posts
    129

    VBA Shell Command Assistance Needed

    I'm trying to execute a command line command from within VBA to combine PDFs. I have a tool called pdftk that'll take 2 input files and combine them into one. It has a command structure of:

    pdftk "input file1" "input file2" output "output file"

    Here's an example of one that works just fine from a command window:

    pdftk "F:\Donor Central\Scripts\Joel's COE Test Folder\LC-SJL1046.pdf" "F:\Donor Central\Scripts\Joel's COE Test Folder\LC-SJx1046.pdf" output "C:\_My Files\_My OCCF Files\SugarSyncTemp\combinedx.pdf"

    Now for the VBA code and my frustration:

    This works fine as well:

    Please Login or Register  to view this content.
    But when I want to use variable substitution I can't get it to work at all: Here's my code:

    Please Login or Register  to view this content.
    If I look at the variable ZZ in the immediate window it looks the same as what's in the Shell command, yet it doesn't work.

    It would be great if I could pause the DOS command window after the command executed to see what's wrong, but even when I do a Call Shell it executes and immediately flashes away. Is there a way to stop the command window from closing so fast?

    Really, my question is, what's wrong with my variable substitution and why won't it work? I sure would appreciate the help.

  2. #2
    Forum Expert
    Join Date
    02-14-2009
    Location
    .
    MS-Off Ver
    ................
    Posts
    2,840

    Re: VBA Shell Command Assistance Needed

    You can use the '/K' parameter to keep the DOS box open after the command completes.

    Please Login or Register  to view this content.
    Plus, if there is more information than can be viewed on a single screen, piping it through MORE will pause when the output fills the screen, you press any key to continue.

    Please Login or Register  to view this content.
    I'm passing on your double quote query - too late to make sense of """""'s & """'s

  3. #3
    Forum Contributor
    Join Date
    05-13-2010
    Location
    Dallas, TX
    MS-Off Ver
    2019
    Posts
    129

    Re: VBA Shell Command Assistance Needed

    Quote Originally Posted by cytop View Post
    You can use the '/K' parameter to keep the DOS box open after the command completes.

    Please Login or Register  to view this content.
    Plus, if there is more information than can be viewed on a single screen, piping it through MORE will pause when the output fills the screen, you press any key to continue.

    Please Login or Register  to view this content.
    I'm passing on your double quote query - too late to make sense of """""'s & """'s
    Hmm. I thought I replied to this yesterday but I don't see it now. Anyway, here goes:

    The Shell Environ$("COMSPEC") & " /k" enabled me to get further along and at least see the errors my command was producing so I could fix my code, so thanks. However, I couldn't see anything but the errors. It didn't echo my command like I would have expected. In fact, once I got it working all I saw was 2 C prompts in the command window. One for it begging and another after my command had ended. how do I get it to echo my commands?

    My problem with my code was that I didn't need double quotes around my specifications when using variable substitution, like I need when just specifying it in the command itself. The variable substitution has already occurred so I needed the command just as I would type it in the command window myself at that point.

  4. #4
    Forum Expert
    Join Date
    02-14-2009
    Location
    .
    MS-Off Ver
    ................
    Posts
    2,840

    Re: VBA Shell Command Assistance Needed

    I can't say how you would do that as I don't have PDFtk. However, the help page for the command line facility does mention
    By default, pdftk runs quietly. Append 'verbose' to the end and it will speak up.
    https://www.pdflabs.com/docs/pdftk-m.../#dest-verbose

  5. #5
    Forum Contributor
    Join Date
    05-13-2010
    Location
    Dallas, TX
    MS-Off Ver
    2019
    Posts
    129

    Re: VBA Shell Command Assistance Needed

    Thanks. That gives me the results of the command as it runs, but it still doesn't echo the command itself. That's the part I'd want to see if my command is formatted incorrectly like it was.

  6. #6
    Forum Expert
    Join Date
    02-14-2009
    Location
    .
    MS-Off Ver
    ................
    Posts
    2,840

    Re: VBA Shell Command Assistance Needed

    Sorry, I misunderstood.

    Taking the 2nd block of code in post #1 as an example
    Please Login or Register  to view this content.
    That prints a mess of doubles quotes and leaves the DOS box open for you to enjoy.

  7. #7
    Forum Contributor
    Join Date
    05-13-2010
    Location
    Dallas, TX
    MS-Off Ver
    2019
    Posts
    129

    Re: VBA Shell Command Assistance Needed

    Interesting. That gets me what I originally wanted and shows me my command, but for some reason adding verbose to the end of the command now doesn't work. If I eliminate echo it works and shows me everything but the command I issued itself, but with echo verbose doesn't work.

  8. #8
    Forum Expert
    Join Date
    02-14-2009
    Location
    .
    MS-Off Ver
    ................
    Posts
    2,840

    Re: VBA Shell Command Assistance Needed

    Just one of those things, I guess - but it seems your initial query is on the way to being resolved?

  9. #9
    Forum Contributor
    Join Date
    05-13-2010
    Location
    Dallas, TX
    MS-Off Ver
    2019
    Posts
    129

    Re: VBA Shell Command Assistance Needed

    Quote Originally Posted by cytop View Post
    Just one of those things, I guess - but it seems your initial query is on the way to being resolved?
    Yes. I now know how to get one or the other. I'll mark this as solved and give you credit.

  10. #10
    Forum Expert
    Join Date
    02-14-2009
    Location
    .
    MS-Off Ver
    ................
    Posts
    2,840

    Re: VBA Shell Command Assistance Needed

    Again, popping back to your initial post - personally I have always found multiple " marks to be as confusing as hell. There is another way to do it, which I find a lot easier to follow, and that's to use the CHR() function to insert a double quote in a string.

    Something like
    Please Login or Register  to view this content.
    As you can see the executable name is a simple string, The Chr(34) adds a double quote and the 1st file name is simply quote delimited as normal and is followed by another Chr(34). Same for the second file but note the space between the 1st & 2nd file must be inserted outside the calls to the Chr() function.

    Am only mentioning this now as I didn;t want to confuse the thread with another way of doing it.

    Above as an example only. It may work but is obviously not tested.

  11. #11
    Forum Contributor
    Join Date
    05-13-2010
    Location
    Dallas, TX
    MS-Off Ver
    2019
    Posts
    129

    Re: VBA Shell Command Assistance Needed

    Quote Originally Posted by cytop View Post
    Again, popping back to your initial post - personally I have always found multiple " marks to be as confusing as hell. There is another way to do it, which I find a lot easier to follow, and that's to use the CHR() function to insert a double quote in a string.

    Something like
    Please Login or Register  to view this content.
    As you can see the executable name is a simple string, The Chr(34) adds a double quote and the 1st file name is simply quote delimited as normal and is followed by another Chr(34). Same for the second file but note the space between the 1st & 2nd file must be inserted outside the calls to the Chr() function.

    Am only mentioning this now as I didn;t want to confuse the thread with another way of doing it.

    Above as an example only. It may work but is obviously not tested.
    I agree. That makes things a lot simpler and easier to follow. Thx.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Shell command to rum dos copy command on vba
    By lepmor in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 06-17-2015, 07:04 PM
  2. Shell command in VBA ?
    By RustyNail in forum Excel General
    Replies: 3
    Last Post: 04-26-2015, 10:46 PM
  3. VBA Shell command
    By sumanc_nitr in forum Excel Programming / VBA / Macros
    Replies: 28
    Last Post: 03-06-2014, 07:04 AM
  4. Using macro to execute a command in Shell Command
    By aadarsh in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 10-19-2010, 07:06 PM
  5. shell command
    By jtwork in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 01-15-2007, 08:07 AM
  6. Shell command
    By Viktor Ygdorff in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 07-13-2006, 12:45 PM
  7. [SOLVED] Help with shell command
    By St in forum Excel General
    Replies: 3
    Last Post: 11-04-2005, 10:45 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1