VBA: Launch a command line

Hello,

In order to dynamically convert a PNG to GIF for use in a UserForm, I found an ".exe" that allows for the conversion.

However, to work, it requires that we drag and drop the PNG to convert directly onto the ".exe".

Using the cmd console, if I enter the path of the exe -> (space) -> path of the PNG, it works.
Also, I would like to be able to execute this command line automatically in VBA. (And close the cmd window once finished).

I found the Shell function online. I can execute the cmd, but I need to enter the command line manually.

I do not understand how to fill in the arguments of the Shell function for this.

Can someone help me?

Thank you.

11 answers

  1. Thank you very much. I looked and tried, but it's still not working.

    Here is the code I entered:
    Sub test() Dim appli As String Dim fichier As String appli = "C:\apng2gif.exe" fichier = DOSSIER_LOGOS & "toto.png" 'DOSSIER_LOGOS is a function that returns 'the path of the folder (as String), this path 'contains spaces Shell "C:\WINDOWS\system32\cmd.exe " & appli & " " & """ & fichier & """, vbMinimizedNoFocus End Sub
    0
    1. Hello,

      it's still not working
      Doesn’t mean anything, what’s happening or not. An error message?
      Set a breakpoint on the Shell line
      Run the code, hover the mouse over DO_DIRECTORY_LOGOS to see the content
      Maybe missing a \ before Toto.png

      To check:
      Would be missing two " before "C: and one after file

      Example:
      Shell """C:\Program Files (x86)\Nuance\PDF Professional 8\bin\GaaihoDoc.exe"" """ & path1 & """", vbNormalFocus
      0
  2. The console launches, but it is empty. It only shows a positioning on the document folder and that's it.

    I tried placing the double quotes exactly like in your example, but it reacts in exactly the same way.

    The DOSSIER_LOGOS function returns the path with a \ at the end.

    I don't understand what's wrong.
    0
    1. Hello,

      DOSSIER_LOGOS

      Where does this thing come from?

      Hover the mouse cursor over the file
      0
  3. As I said, this function returns a String that is the path to a folder.
    This function returns:
     Function DOSSIER_LOGOS() as String DOSSIER_LOGOS = ThisWorkbook.Path & "\Images\Logos\" End Function


    When I hover the mouse in debug mode, it correctly returns this. Moreover, I also tested putting a
    MsgBox DOSSIER_LOGOS
    in a test procedure, and it correctly displays the path.

    So I am sure that the problem does not come from there. The folders "Images" and "Logos" are spelled correctly (via copy/paste to be sure).
    0
    1. Hello to both of you,

      I launch the app with a simple shell command and it works. However, the app window opens and I have to click on convert
      With other commands, including the one from the site given by cs_Le Pivert, the app window opens too...
      0
      1. Thank you both very much! The method provided by cs_Le Pivert works. And for me, I don't have to click in the console. Perfect! I tested it by typing the command directly. Now I just need to generate it with variables, and it will be good.

         Public Sub prcStartMSConfig64Cmd() Const WND_HIDE As Integer = 0 Dim objWshShell As Object Set objWshShell = CreateObject("WScript.Shell") objWshShell.Run "%windir%\Sysnative\cmd.exe " & _ & "/C start c:\apng2gif.exe c:\toto.png", WND_HIDE, True 'I only modified this line Set objWshShell = Nothing End Sub 
        0
        1. Re,
          I'm getting the same thing with this:

          Shell "cmd /c start " & appli & " " & fichier & """", vbMinimizedFocus
          0
        2. @f894009Great minds think alike. I just tested this as well, and it works for me too. It seems that what I was missing was the
          /c start
          . I hadn't included that before.
          0
        3. @sifusaladeRe,

          what I was missing was the /c start

          No, it works without it, I tried with it to see the difference..
          0
        4. @f894009For me, it didn't work without it. I'm completely lost. I'm going to take a few days off and take another look at this. Because right now, I'm totally out of it. I don't understand. But I'm sure I've missed something.

          Especially since even when I put the
          /c start
          back in, after trying without it, it didn't work anymore; it directly opened the PNG. So I'm sure I've messed up somewhere, my brain can't keep up right now ????.. I'm doing everything wrong.

          Thank you very much anyway for this help and the time spent ????
          0
        5. @sifusaladeHello,
          try writing the applied values and file in the command line, with start for you. It will only create a simple string between quotes.
          0
      2. thank you.
        (PS: my smileys have been replaced by ??? so it changes the meaning of the sentence lol)
        I’m going to take a break from VBA for a few days. And I will come back to clarify my (or my) mistakes. This could be useful to others ^_^
        0
        1. Hello,

          Even after resting, I'm still flying high. I realized that the problem comes from the double quotes.

           Sub Test(Str_Fichier as String) 'Str_Fichier contains the complete path to a file Dim appli As String Dim Fichier As String Dim commande As String appli = DOSSIER_LOGOS & "apng2gif.exe" Fichier = DOSSIER_LOGOS & Str_NomFichier commande = "cmd /C start " & appli & " " & Fichier Shell commande, vbMinimizedNoFocus End Sub 


          During my last tests, it worked because for the appli variable I had
          appli = "C:\apng2gif.exe"
          (a path without spaces).
          However, now that I've replaced it, it doesn't work anymore.

          The DOSSIER_LOGOS function returns a path that contains spaces.
          (D:\99 - Système\Utilisateurs\Moi\Bureau\Dossier Appli\Images\Logos\)

          And the problem comes from there.

          I still haven't understood how to place my double quotes.

          As it stands, I'm getting an error message saying that Windows cannot find D:\99.
          0
          1. Hello,
            Get out of the sand!

            There’s something, why D: is more than C:?
            In your code add this line
            after commande=
            Debug.print commande


            set a breakpoint on line Shell

            Run the code
            Display the execution window to see the content of commande
            0
        2. In fact, I initially set C: for testing, but the path of Appli should follow the folder containing the Excel file.
          So potentially D: and paths with spaces. I wanted to test how it would react.

          I thought it would be enough to change the content of my variable Appli once I found the right command. But I was completely wrong.

          I did plenty of tests including debug.print.

          with
           command = "cmd /c start " & """" & appli & """" & " " & """" & Fichier & """" Debug.Print command 

          Debug.Print gives me:
          cmd /c start "D:\99 - Système\Utilisateurs\Moi\Bureau\Dossier Appli\Images\Logos\apng2gif.exe" "D:\99 - Système\Utilisateurs\Moi\Bureau\Dossier Appli\Images\Logos\58.png"


          and
          Shell command, vbMinimizedNoFocus

          I have double quotes surrounding my paths, but it directly opens the file 58.png. However, I want to launch this file 58.png with the application apng2gif.exe (Appli).

          PS: my desktop is not on C:, but on D:
          0
          1. Re,
            Bizard.

            Before Appli=, add this line
            Chdir DOSSIER_LOGOS
            Change the application and file assignment without DOSSIER_LOGOS
            to position yourself in the directory of your files.
            0
        3. THANK YOU SO MUCH !!!!! It works.

          In fact, just a minute before seeing this wonderful response, I was thinking it was impossible and I had solved the issue by copying "apng2gif.exe" into "C:\TEMP\". This allowed me to have a path without spaces for the application. And it forced me to delete "C:\TEMP\apng2gif.exe" in Workbook_BeforeClose.

          But now, it's much better. It's really what I wanted to do in the first place.

          I'm not the kind of person to ask for help just so that people do the work for me. I'm also looking to understand and learn.

          If I understand correctly, the line Chdir DOSSIER_LOGOS puts me in that folder in the console, right? And so if that's the case, and if I understand correctly, by being positioned there, there's no need for an absolute path anymore.
          0
          1. Hello,
            David Bowie.
            0