5

Well, hello, i've a doubt. The question is how i can open whatsapp desktop from a button in VB6 in a specific number with a message. For example in C# i can do that with the property Process:

var process = $"whatsapp://send?phone=54123456789&text=hello!!";
Process.Start(process);

But in vb6 i don't have idea how i can do it 'cause the method can be:

Shell "C://path_to_whatsapp_installed/whatsapp.exe" 

But with that i can't open in a specific chat

  • 5
    Use the ShellExecute API, just as you would for opening an HTTP URL, E.g. https://stackoverflow.com/questions/4530741/visual-basic-open-url-with-default-browser – Alex K. Nov 07 '22 at 12:10
  • 2
    The way you do it in VB.NET is the same as you do it in C#: use the [System.Diagnostics.Process.Start](https://learn.microsoft.com/en-us/dotnet/api/system.diagnostics.process.start?view=net-6.0) method. – Hel O'Ween Nov 07 '22 at 12:25
  • 1
    But, i said VB6 not VB.nET @HelO'Ween –  Nov 07 '22 at 17:18
  • 2
    Indeed. Me <- tomatoes on eyes. Sorry. – Hel O'Ween Nov 08 '22 at 10:19

2 Answers2

2

You can use the ShellExecute function:

Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hwnd As Long, ByVal lpOperation As String, ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long 

Const SW_SHOWNORMAL = 1
Const SW_SHOWMAXIMIZED = 3

Call it inside your form like this:

ShellExecute Me.hWnd, "Open", "whatsapp://send?phone=54123456789&text=hello!", "", "", SW_SHOWMAXIMIZED
AmigoJack
  • 5,234
  • 1
  • 15
  • 31
Ehab
  • 284
  • 1
  • 9
  • 1
    Wow, that's awesome! thanks for the help. i've a doubt, what about `ByVal lpParameters As String, ByVal lpDirectory As String` what i can put in there? –  Nov 08 '22 at 19:03
  • 2
    @FrancoJoelBalsamo Here's a link to [the documentation](https://support.microsoft.com/en-us/topic/wd2000-how-to-call-the-shellexecute-windows-api-function-80da207b-2fa3-ac60-e871-f0a63164bad7). – Brian M Stafford Nov 08 '22 at 21:26
  • i've a doub, is possible add a file like a pdf/xls/jpeg to send? or not? –  Nov 14 '22 at 16:29
1

Win32 ShellExecute wrapper. I have this in common helper a class.

Private Const SW_HIDE As Long = 0
Private Const SW_SHOWNORMAL As Long = 1
Private Const SW_SHOWMINIMIZED As Long = 2
Private Const SW_SHOWMAXIMIZED As Long = 3
Private Const SW_SHOWNOACTIVATE As Long = 4
Private Const SW_SHOW As Long = 5
Private Const SW_MINIMIZE As Long = 6
Private Const SW_SHOWMINNOACTIVE As Long = 7
Private Const SW_SHOWNA As Long = 8
Private Const SW_RESTORE As Long = 9
Private Const SW_SHOWDEFAULT As Long = 10
Private Const SW_FORCEMINIMIZE As Long = 11

' ShellOpenDocument verbs

Public Enum ShellExecuteVerbs
   sevNULL
   sevEdit
   sevExplore
   sevFind
   sevOpen
   sevPrint
   sevRunAs
End Enum

Private Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" ( _
   ByVal hWnd As Long, _
   ByVal lpOperation As String, _
   ByVal lpFile As String, _
   ByVal lpParameters As String, _
   ByVal lpDirectory As String, _
   ByVal nShowCmd As Long _
   ) As Long

'------------------------------------------------------------------------------
'Purpose  : Opens a document with the registered application for this file type
'
'Prereq.  : -
'Parameter: sFileName      - Fully qualified filename
'           eShellVerb     - The action the associated application should do with documentName
'           lWindowState   - Window state and/or focus of the associated application
'           hWndParent     - Parent window handle
'           sWorkingDirectory - Working directory
'Returns  : > 32 = Success
'Note     : See https://docs.microsoft.com/en-us/windows/win32/api/shellapi/nf-shellapi-shellexecutea
'           for possible error codes <= 32
'------------------------------------------------------------------------------
Public Function ShellOpenDocument( _
   ByVal sFileName As String, _
   Optional ByVal eShellVerb As ShellExecuteVerbs = sevOpen, _
   Optional ByVal lWindowState As Long = SW_SHOWNORMAL, _
   Optional ByVal hWndParent As Long = 0, _
   Optional ByVal sWorkingDirectory As String = vbNullString _
   ) As Long
   
   Dim sVerb As String
   
   Select Case eShellVerb

      Case ShellExecuteVerbs.sevNULL
         sVerb = vbNull
      Case ShellExecuteVerbs.sevEdit
         sVerb = "edit"
      Case ShellExecuteVerbs.sevExplore
         sVerb = "explore"
      Case ShellExecuteVerbs.sevFind
         sVerb = "find"
      Case ShellExecuteVerbs.sevOpen
         sVerb = "open"
      Case ShellExecuteVerbs.sevPrint
         sVerb = "print"
      Case ShellExecuteVerbs.sevRunAs
         sVerb = "runas"
      Case Else
         sVerb = vbNull
   End Select
   
   If Len(sWorkingDirectory) < 1 Then
      sWorkingDirectory = App.Path
   End If
   
   ShellOpenDocument = ShellExecute(hWndParent, _
                           sVerb, _
                           sFileName, _
                           vbNullString, _
                           sWorkingDirectory, _
                           lWindowState)

End Function
Hel O'Ween
  • 1,423
  • 9
  • 15