My Access App Will NOT Run Batch File Command! (1 Viewer)

GrandMasterTuck

In need of medication
Local time
Today, 06:01
Joined
May 4, 2013
Messages
129
Hi folks. I have a vba function that takes a file named 1.jpg that is stored in the C:\APic directory and resizes it to exactly 240x300 pixels. The file name never changes, the batch file simply calls the ImageMagick script to perform the resize.

The problem? It runs perfect when I double click the batch file using Windows Explorer. But when I click my button on my Access form, which calls the function, nothing happens. And I have no idea why.

Here is the batch file code, verbatim:
Code:
@ECHO OFF
convert C:\APic\1.jpg -resize 240x300 C:\APic\1.jpg
EXIT

And here is my VBA function, verbatim:
Code:
RetVal = Shell("C:\$Data\MyAppDir\SysFiles\resize.bat", 1)

I also tried this for the VBA function:
Code:
Call Shell(Environ$("COMSPEC") & " /c  C:\$Data\MyAppDir\SysFiles\resize.bat", vbNormalFocus)

...and I tried this:
Code:
Application.FollowHyperlink "C:\$Data\MyAppDir\SysFiles\resize.bat"

Again, if I navigate to C:\$Data\MyAppDir\SysFiles, the batch file 'resize.bat' is in there. If I double-click it, it runs and resizes the file exactly as I want it to. But when I try to run this batch file from within Access using VBA, it blinks as if it's doing something, but the image remains unchanged. I don't understand why... Any ideas?
 

RuralGuy

AWF VIP
Local time
Today, 04:01
Joined
Jul 2, 2005
Messages
13,826
Have you tried removing the @ECHO OFF temporarily?
 

Minty

AWF VIP
Local time
Today, 11:01
Joined
Jul 26, 2013
Messages
10,373
What OS? If it is windows 10 try moving the image / path to your local documents folder and see if that make any difference.
 

GrandMasterTuck

In need of medication
Local time
Today, 06:01
Joined
May 4, 2013
Messages
129
Shell always worked for me.
Me too... I can't understand why this one isn't working!

Have you tried removing the @ECHO OFF temporarily?
Yep, tried it just now. No dice.

What OS? If it is windows 10 try moving the image / path to your local documents folder and see if that make any difference.
Nope. Windows 7 Pro

Might there be a way to execute the CMD command directly from Access without trying to run it from a BAT and using Access to open the BAT?
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 05:01
Joined
Feb 28, 2001
Messages
27,314
The difference might be in the default directory involved. I believe Shell and Windows Explorer don't have the same defaults. I.e. they don't necessarily launch from the same place. This is a vague and somewhat distant memory, but ...

I think using Windows Explorer to launch the .BAT file uses the default directory from which you found the file. Shell is more likely to be running from whatever folder to which you had installed your copy of Access. Odds are that they are different.

Therefore, you might need explicit directory path specifications for your command line's file reference. OR you might need to include a "CD C:\<etc>" command before you launch your script.
 

GrandMasterTuck

In need of medication
Local time
Today, 06:01
Joined
May 4, 2013
Messages
129
Therefore, you might need explicit directory path specifications for your command line's file reference.
When you refer to the 'command line', are you referring to the line of code that I enter into a command prompt? That's this line here:
Code:
convert C:\APic\1.jpg -resize 240x300 C:\APic\1.jpg
...and as you can see, it has full paths.

Are you referring to the VBA function? Right here:
Code:
RetVal = Shell("C:\$Data\MyAppDir\SysFiles\resize.bat", 1)
There's a path in there, too. I guess I'm not quite understanding what you're referring to. Where, specifically, am I missing an 'explicit directory path specification'?

. OR you might need to include a "CD C:\<etc>" command before you launch your script.
Do I add this to the batch file above the line of code? If not, how do I include the change directory switch? Where do I put that?
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 05:01
Joined
Feb 28, 2001
Messages
27,314
I DID indeed see that you were using explicit paths. But from where do you get your CONVERT verb? I know that the shortcut icon you use to launch access doesn't contain just "MSACCESS.EXE" - it contains the "C:\Program Files\etc.etc." path to Access as well as the path to your .MDB or .ACCDB file. So does the convert verb require that kind of support?

You would add the CD command before your convert command in the .BAT file IF that is what is required. And I did say "might" when I offered this solution. I'm not familiar with your conversion program so don't know what IT requires, but if it is ALSO located in your Apic folder then that might represent a reason for failure from the shell.
 

GrandMasterTuck

In need of medication
Local time
Today, 06:01
Joined
May 4, 2013
Messages
129
I DID indeed see that you were using explicit paths. But from where do you get your CONVERT verb?
The 'convert' command is part of ImageMagick's command-line verbs for performing functions. This particular verb tells the system to allow for resizing, reshaping, that sort of thing, and in my case, I'm invoking the -resize switch and specifying a width and height in pixels. More info on that HERE.

I DID indeed see that you were using explicit paths. But from where do you get your CONVERT verb? I know that the shortcut icon you use to launch access doesn't contain just "MSACCESS.EXE" - it contains the "C:\Program Files\etc.etc." path to Access as well as the path to your .MDB or .ACCDB file. So does the convert verb require that kind of support?
I don't think so, or there would have been some indication on the website help file, I would imagine. ImageMagick is installed onto your pc like any other app, and it adds some new commands to the Windows Command Prompt app that allow you to manipulate images using only command line instructions. I simply installed the app, found the correct command line instruction, customized it to do exactly what I want, then put that into a batch file. I can run the batch file and it does exactly what it is supposed to do, but if I try to run the batch file from Access (with a button, for example) it won't work.

I DID indeed see that you were using explicit paths. But from where do you get your CONVERT verb? I know that the shortcut icon you use to launch access doesn't contain just "MSACCESS.EXE" - it contains the "C:\Program Files\etc.etc." path to Access as well as the path to your .MDB or .ACCDB file. So does the convert verb require that kind of support?

You would add the CD command before your convert command in the .BAT file IF that is what is required.
Ah, okay, I wasn't quite sure where to put that. I'll try that when I get to work tomorrow. I guess it's possible that Access is invoking the command from a different path than Windows Explorer, which would explain why it ran from Windows Explorer but didn't run from Access. That makes sense.

I'm not familiar with your conversion program so don't know what IT requires, but if it is ALSO located in your Apic folder then that might represent a reason for failure from the shell.
Nope. Installed to Program Files (64 bit app)... oh... you know what? I wonder if it has to do with the fact that ImageMagick is a 64 bit app and my copy of Access is 32 bit...?

EDIT: Welp, that wasn't it, either. I can't seem to get ImageMagick to work for me. So I tried IrfanView (which also has command line instructions you can run with a batch file) and lo and behold, it works great. So, solved. Thanks to everyone who offered ideas!
 
Last edited:

Galaxiom

Super Moderator
Staff member
Local time
Today, 20:01
Joined
Jan 20, 2009
Messages
12,856
Change "Exit" to "Pause" to leave the window open so that that any messages can be read.
 

jdraw

Super Moderator
Staff member
Local time
Today, 06:01
Joined
Jan 23, 2006
Messages
15,394
If you want to try just Access and Windows
Here's a function and a test routine. I found this on UA and tested it with a few of my images.
It was written by a Spanish MVP.
Emilio Sancha
Access MVP 2006-2011
It uses Microsoft Windows Image Acquisition Library vX.X which is included in OS since Vista apparently. Just set the reference as per the jpg attached.

I'm using win10

Code:
'****************************************************************************
'* Redimensionar
'* re-escala la imagen pasada como parámetro
'* Deberá incluir una referencia a Microsoft Windows Image Acquisition Library vX.X
'* Argumentos: strArchivo => ruta del archivo a redimensionar
'* lngAlto => alto en pixels a aplicar
'* lngAncho => ancho en pixels a aplicar
'* uso: Redimensionar "C:\Temp\prueba.PNG"
'* ESH 16/08/09
'* Si utilizas este codigo, respeta la autoría y los créditos
'****************************************************************************


'---------------------------------------------------------------------------------------
' Procedure : Redimensionar
' Author    : mellon  from http://www.utteraccess.com/forum/index.php?showtopic=2035296&hl=
' Date      : 03/03/2016
' Purpose   :'* Resize
'* Rescaled last image as a parameter  'by Emilio Sancha Spanish  MVP
'***** Must include a reference to Microsoft Windows Image Acquisition Library vX.X
'*****                                   /
'*****Not required for Vista and newer. + **********************************************
'*****                                   \
'* Arguments: strArchivo => file path to resize
'* LngAlto => height in pixels to apply
'* LngAncho => width in pixels to apply
'* Usage: Resize "C: \ Temp \ test.png"
'* ESH 16/08/09
'* If you use this code, respects the authorship and credits
'---------------------------------------------------------------------------------------
'
Public Sub Redimensionar(strArchivo As String, lngAlto As Long, lngAncho As Long)
    Dim Imagen As WIA.ImageFile, _
        IP As WIA.ImageProcess, _
        strEscalado As String

    

    On Error GoTo Redimensionar_TratamientoErrores

    Set Imagen = CreateObject("WIA.ImageFile")
    Set IP = CreateObject("WIA.ImageProcess")

    Imagen.LoadFile (strArchivo)
    IP.Filters.Add (IP.FilterInfos("Scale").FilterID)
    IP.Filters(1).Properties("MaximumWidth").value = lngAncho
    IP.Filters(1).Properties("MaximumHeight").value = lngAlto
    Set Imagen = IP.Apply(Imagen)

    strEscalado = Replace$(strArchivo, ".", ".redim.")
    ' si el archivo ya existe lo elimino
    If Not Dir$(strEscalado) = vbNullString Then Kill strEscalado
    Imagen.SaveFile (strEscalado)


Redimensionar_Salir:
    If Not Imagen Is Nothing Then Set Imagen = Nothing
    If Not IP Is Nothing Then Set IP = Nothing
    On Error GoTo 0
    Exit Sub

Redimensionar_TratamientoErrores:
    MsgBox "Error " & err & "  in line " & Erl & " en proc.: Redimensionar de Módulo: mdlEscanearWIA (" & err.Description & ")", vbCritical + vbOKOnly, "ATENCION"
    Resume Redimensionar_Salir

    On Error GoTo 0
    Exit Sub


End Sub    ' Redimensionar

This was one of my tests. You 'll have to change the drive/path etc as appropriate.
Code:
'---------------------------------------------------------------------------------------
' Procedure : ImageResizeTest
' Author    : mellon
' Date      : 03/03/2016
' Purpose   : Works very well in my test
' Calls:  Redimensionar
'---------------------------------------------------------------------------------------
'
Sub ImageResizeTest()

Dim sPath As String
   On Error GoTo ImageResizeTest_Error

sPath = "C:/users/mellon/documents/tenancy.jpg"
 Call Redimensionar(sPath, 1500, 1600)

   On Error GoTo 0
   Exit Sub

ImageResizeTest_Error:

    MsgBox "Error " & err.number & " (" & err.Description & ") in procedure ImageResizeTest of Module UtterAccessRelated"
End Sub
 

Attachments

  • WiindowsImageReference.jpg
    WiindowsImageReference.jpg
    8.1 KB · Views: 80
Last edited:

Users who are viewing this thread

Top Bottom