0

I will be using ZINT as my barcode generator in my Access program. I want to call it by using SHELL command from within Access VBA. My problem is, I can't call ZINT from command prompt. Here is my code.

Private Sub cmdTest_Click()
    Dim strToPrint, strInfoBarcode As String

    strInfoBarcode = "Batch #699"

    strToPrint = "%ProgramFiles%\zint\zint.exe  -o c:\path\699.png -b 58 --vers=10 -d " & strInfoBarcode
    Call Shell("cmd.exe /S /K" & strToPrint, vbMinimizedNoFocus)
End Sub

When i run the above code, the error shows

'C:\Program' is not recognized as an internal or external command,
operable program or batch file.

Could someone shows the right way to call the ZINT program from Access VBA?

y2a
  • 121
  • 1
  • 12
  • Are you missing a space in Program Files? Guessing that's just a typo, but you should be putting quotes around any path with a space. – Tim Williams Feb 19 '18 at 07:36
  • You're missing a space when concatenating the strings, after `/K` – Erik A Feb 19 '18 at 07:53
  • Yes... i forget to add space after /K, but that didn't solve the zint path problem. Still the same error. – y2a Feb 19 '18 at 08:01

2 Answers2

1

Are you missing a space in Program Files? Guessing that's just a typo, but you should be putting quotes around any path with a space.

Private Sub cmdTest_Click()
    Dim strToPrint, strInfoBarcode As String

    strInfoBarcode = "Batch #699"

    strToPrint = """%ProgramFiles%\zint\zint.exe""  -o c:\path\699.png -b 58 --vers=10 -d """ & strInfoBarcode & """"
    Call Shell("cmd.exe /S /K " & strToPrint, vbMinimizedNoFocus)
End Sub

Likewise probably a good idea to also quote the barcode if it might contain any spaces (as shown in the ZINT wiki page on Excel integration: http://zint.org.uk/Excel.aspx).

EDIT: you were also missing a space after the /K flag, but I don't think you even need to use cmd.exe here.

Tim Williams
  • 154,628
  • 8
  • 97
  • 125
  • I use `%ProgramFiles%` as the environment variable for C:\Program Files\. I have try the above code, but still the same error shows. I will check the link above. – y2a Feb 19 '18 at 07:45
  • My mistake - I'm not a big user of Shell. General point about quoting any argument which might contain a space applies though... – Tim Williams Feb 19 '18 at 07:49
  • I have read the link above, and remove the `cmd.exe /S /K` command from the Shell command. Thanks for pointing out the right direction. Also find help from this [Get Program Files Path](https://stackoverflow.com/questions/8701967/get-path-of-program-files-folder-that-contains-32-bit-programs). How to inform my new working code? Can I edit my question above? – y2a Feb 19 '18 at 08:46
0

To help others with the same problem, here is my working code.

Private Sub cmdTest_Click()
    Dim strZintPath, strImagePath, strToPrint, strInfoBarcode As String

    strZintPath = Get32BitProgramFilesPath() & "\Zint\"
    strImagePath = CurrentProject.Path & "\images\barcode\"
    strInfoBarcode = "699"

    strToPrint = strZintPath & "zint.exe -b 58 -o " & Chr(34) & strImagePath & strInfoBarcode & ".png" & Chr(34) & " --vers=10 -d " & Chr(34) & strInfoBarcode & Chr(34)
    Call Shell(strToPrint, vbMinimizedNoFocus)

End Sub

And this is the Function that I copy from Get Path of Program Files

Function Get32BitProgramFilesPath() As String
    If Environ("ProgramW6432") = "" Then
       '32 bit Windows
       Get32BitProgramFilesPath = Environ("ProgramFiles")
    Else
       '64 bit Windows
       Get32BitProgramFilesPath = Environ("ProgramFiles(x86)")
    End If
End Function
y2a
  • 121
  • 1
  • 12