Results 1 to 8 of 8

Thread: [RESOLVED] VBA Shell Problem

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jul 2004
    Location
    Cambridge,MA
    Posts
    20

    Resolved [RESOLVED] VBA Shell Problem

    I'm working on a program written in VBA that uses the Shell() function to call a command line executable. The problem is that this code never outputs to the file "checksum"

    VB Code:
    1. Call Shell("C:\instagram\md5sum\fciv.exe C:\instagram\replicated\" & ThisWorkbook.user & "_binlog.txt > C:\instagram\replicated\checksum")

    Does anyone know why this code might not be working correctly?

    I've run this from cmd shell and it goes off without an hitches.

    Thanks in advance,

    Ross

  2. #2
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: VBA Shell Problem

    Moved from ClassicVb.

  3. #3
    Frenzied Member zaza's Avatar
    Join Date
    Apr 2001
    Location
    Borneo Rainforest Habits: Scratching
    Posts
    1,486

    Re: VBA Shell Problem

    I think I attempted such things before, and came to the conclusion that you can't use Shell to pass a command line argument. As I recall, you can do a workaround in terms of running cmd.exe and then sending it keys, but I don't think you can do it directly like that.


    But I'm prepared to be wrong.

    zaza

  4. #4
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: VBA Shell Problem

    Here you go zaza. I added the parameters how I think you need them. Not sure if the working directory is the right one either. Also, ThisWorkbook.user does not show for me so I assume its a custom property or function.
    VB Code:
    1. Option Explicit
    2.  
    3. Private Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hwnd As Long, ByVal lpOperation As String, _
    4. ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long
    5.  
    6. Private Const SW_HIDE            As Long = 0
    7. Private Const SW_SHOWNORMAL      As Long = 1
    8. Private Const SW_SHOWMINIMIZED   As Long = 2
    9. Private Const SW_SHOWMAXIMIZED   As Long = 3
    10.  
    11. Private Sub CommandButton1_Click()
    12.     ShellExecute Application.hwnd, "Open", "C:\instagram\md5sum\fciv.exe", "C:\instagram\replicated\" & ThisWorkbook.user & "_binlog.txt > C:\instagram\replicated\checksum", "C:\instagram\replicated", SW_SHOWNORMAL
    13. End Sub
    I also have written code to shell the CMD.exe and output to a file if you need it.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  5. #5
    Frenzied Member zaza's Avatar
    Join Date
    Apr 2001
    Location
    Borneo Rainforest Habits: Scratching
    Posts
    1,486

    Re: VBA Shell Problem

    Aha, the old "ShellExecute" trick.

    Nice one RD.

    But I still don't think you can do it just by passing the whole string to Shell().

    zaza

  6. #6
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: VBA Shell Problem

    Did it work though? Also shouldnt checksum be a file and not a directory?
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  7. #7
    Frenzied Member zaza's Avatar
    Join Date
    Apr 2001
    Location
    Borneo Rainforest Habits: Scratching
    Posts
    1,486

    Re: VBA Shell Problem

    Actually, the code isn't for me...


    I'm sure he appreciates it, though.

  8. #8

    Thread Starter
    Junior Member
    Join Date
    Jul 2004
    Location
    Cambridge,MA
    Posts
    20

    Re: VBA Shell Problem

    Actually I found a near identical code snippet on the net elsewhere and still doesn't seem to work.

    However, I found another workaround to accomplish the task I needed.

    I wanted to make sure that in between programmatic writes to the binary log someone did not enter in their own statements. As the bin log will be used as publisher to a database this could be disasterous.

    Anyway, if anyone cares, instead of writing the binlog to a file everytime, it writes to a BLOB field in a table. Only when the binlog is finally being used is the contents of the BLOB written to the file, thereby ensuring no tampering in the meantime.

    Even better, and MD5 in mySQL of the BLOB before the write, then a re-read of the binlog file back into the BLOB and an md5 after (before committing a transaction) provides yet another check

    Anyway, I think zaza is correct.

    Thanks anyway.

    R

Posting Permissions

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



Click Here to Expand Forum to Full Width