title: Convert Image from VBA Using ImageMagick
body: <p>I would like to convert images downloaded from the internet[1] to JPGs with ImageMagick in VBA.  So far, I've attempted two methods that have both failed.</p>
  <p>First, I tried using the ImageMagickObject 1.0 Type Library:</p>
  <pre class="lang-vb prettyprint-override"><code>Private Sub CommandButtonOkay_Click()
      Dim sURL As String, sNetFile As String, sLocalFile As String, _
          cmd As String, RetVal As Integer, img As Object
      Set img = New ImageMagickObject.MagickImage
      sURL = UserForm1.TextBoxImgURL
      sLocalFile = &quot;C:\temp\&quot; &amp; UserForm1.TextBoxName
      DownloadFile sURL, sLocalFile ' Function to download image from a URL and save it to a local directory
      RetVal = img.Convert(sLocalFile, sLocalFile &amp; &quot;.jpg&quot;) '&lt;-- This line produces the error
      UserForm1.Hide
  End Sub
  </code></pre>
  <p>This ends up giving me the following error:</p>
  <p><a href="https://i.stack.imgur.com/Bmptm.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Bmptm.jpg" alt="MSVB Error Message" /></a></p>
  <p>The source file (&quot;C:\temp\image&quot;) exists, but the file that was to be created (&quot;C\temp\image.jpg&quot;) does not.  This is very similar to the question posted <a href="https://stackoverflow.com/questions/26064458/calling-imagemagick-com-from-vbscript-in-excel">here</a>, but I have not been able to find a solution to that so far.</p>
  <p>Second, I tried just calling ImageMagick using the <code>Shell</code> command:</p>
  <pre class="lang-vb prettyprint-override"><code>Private Sub CommandButtonOkay_Click()
      Dim sURL As String, sNetFile As String, sLocalFile As String, _
          cmd As String, RetVal As Integer
      sURL = UserForm1.TextBoxImgURL
      sLocalFile = &quot;C:\temp\&quot; &amp; UserForm1.TextBoxName
      DownloadFile sURL, sLocalFile ' Function to download image from a URL and save it to a local directory
      RetVal = Shell(&quot;convert.exe &quot;&quot;&quot; &amp; sLocalFile &amp; &quot;&quot;&quot; &quot;&quot;&quot; &amp; sLocalFile &amp; &quot;.jpg&quot;&quot;&quot;)
      UserForm1.Hide
  End Sub
  </code></pre>
  <p>When I run this, the image gets downloaded just fine, but the image isn't converted and no error is thrown.  Furthermore, when I execute the command that the <code>Shell</code> command executes in a separate command window, the conversion happens exactly as I would expect.</p>
  <p>So the question then seems to be why is the ImageMagick command working beautifully when it is operating in its own command prompt, but not working at all when operating from within VBA?</p>
  <p>[1] I don't know if this is useful information or not, but I'm downloading the images from the internet programmatically, so I have no means of knowing what format I'm getting; however, the image I've been using to test this with is a PNG.</p>