+ Reply to Thread
Results 1 to 8 of 8

Compressing multi used variables

Hybrid View

  1. #1
    Valued Forum Contributor
    Join Date
    05-14-2009
    Location
    gold coast
    MS-Off Ver
    Excel 2007
    Posts
    843

    Compressing multi used variables

    How are you guys.

    What i would like to know is, In the code im using to make a phone call from a userform the varibles are set in each and every sub. It all works well though could they be compressed into only being used once like an option variable or somethink like that.
    i know that more that one variable can be decleared at the same time of the same type "dim x, Y, z as long.

    Option Explicit
    
    Const WM_CLOSE = &H10
    Const WM_DESTROY = &H2
    'Const SW_HIDE = 0
    Const WM_SETTEXT = &HC
    Const WM_COMMAND = &H111
    Const VK_RETURN = &HD
    Const WM_CHAR = &H102
    Const WM_KEYDOWN = &H100
    Const WM_KEYUP = &H101
    Const VK_SPACE = &H20
    
    Const GW_CHILD As Long = 5
    'Const WM_CHAR = 258
    Const VK_F10 = &H79
    Const VK_F5 = &H74
    Const VK_L = &H4C
    
    Const SW_HIDE = 0
    Const SW_SHOW = 5
    
    Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
    
    'API use Phone Dialer to make call
    Private Declare Function tapiRequestMakeCall Lib "TAPI32.DLL" _
    (ByVal DestAddress As String, ByVal AppName As String, ByVal CalledParty As String, _
    ByVal Comment As String) As Long
    
    'API find applcation by full caption
    Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" _
    (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
    
    'API bring Window to foreground
    Private Declare Function SetForegroundWindow Lib "user32" (ByVal hwnd As Long) As Long
    
    'API set ShowWindow to hide window
    Private Declare Function ShowWindow Lib "user32" (ByVal hwnd As Long, ByVal nCmdShow As Long)
    
    'API send message to application
    Private Declare Function PostMessage Lib "user32" Alias "PostMessageA" _
    (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
    
    Private Declare Function WaitForSingleObject Lib "kernel32" _
    (ByVal hHandle As Long, ByVal dwMilliseconds As Long) As Long
    
    Private Declare Function CloseHandle Lib "kernel32" _
    (ByVal hObject As Long) As Long
    
    Private Declare Function OpenProcess Lib "kernel32" _
    (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, _
    ByVal dwProcessId As Long) As Long
    
    Private Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hwnd As Long, lpdwProcessId As Long) As Long
    
    Private Declare Function TerminateProcess Lib "kernel32" (ByVal hProcess As Long, ByVal uExitCode As Long) As Long
    
    Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
    
    Private Const INFINITE = -1&
    Private Const SYNCHRONIZE = &H100000
    
    '******************************************************************************************
    
    Sub bussinessphone()                                               'Bussiness Phone Number
    On Error GoTo HandleErr                                         '*******************
    Dim varDialWait             As Variant
    Dim varStart                As Variant
    Dim lThreadId               As Long
    Dim lProcessId              As Long
    Dim ret                     As Long
    Dim strDial                 As String
    Dim lngRetVal               As Long
    Static blnDialing           As Boolean 'Prevent Re-Entry Into Sub While Dialing
    Dim strMsg                  As String
    Dim varReturn               As Long
    Dim lngX                    As Long
    Dim AppCaption              As String
    Dim hwnd                    As Long
    
    DoEvents
    
    blnDialing = False
    If blnDialing Then Exit Sub
    If Combobox_List_Search.TextBox9.Value = "" Then Exit Sub
                             'Conferm Dial Message
    If msgbox("Dial Number " _
    & vbNewLine _
    & Combobox_List_Search.ComboBox2.Value & " " & Combobox_List_Search.TextBox20.Value & " " _
    & "Mobile Phone" _
    & vbNewLine _
    & Combobox_List_Search.TextBox9.Text, _
    vbQuestion Or vbYesNo, "Phone Dialler") = vbNo Then Exit Sub Else
    
    strDial = Combobox_List_Search.TextBox9 ' Number To Dial
    lngRetVal = tapiRequestMakeCall(Trim$(strDial), "", "", "") 'Make call to Number Through Dialer.exe
    If lngRetVal = 0 Then blnDialing = True 'Exit sub on tapiRequestMakeCall Fail
    
    Call TestPlayWavFile ' Play Dial Sound Message
    
    For lngX = 1 To 10 ' Number of Loop Times For Sleep
    DoEvents
    Sleep 1000 ' Sleep 1 Second
    Next lngX
        
    'hwnd = FindWindow(vbNullString, AppCaption)
    
    hwnd = FindWindow(vbNullString, "Call Status")
        If hwnd <> 0 Then Call SendMessage(hwnd, WM_CLOSE, 0&, 0&)
    hwnd = FindWindow(vbNullString, "Phone Dialer")
        If hwnd <> 0 Then PostMessage hwnd, WM_CLOSE, 0&, 0&
    hwnd = FindWindow(vbNullString, "Dialer")
        If hwnd <> 0 Then
            PostMessage hwnd, WM_KEYDOWN, VK_RETURN, 0
            PostMessage hwnd, WM_KEYUP, VK_RETURN, 0
        End If
    
    Exit Sub
    HandleErr:
    Select Case Err.Number
    Case 94 'Invalid Use of Null - if double-click on blank, open dialer
    msgbox "here" 'Call Shell("c:\windows\dialer.exe", vbNormalFocus)
    Exit Sub
    Case Else
    'modHandler.LogErr ("frm0Telephone"),("Telephone_Numbers_DblClick" )
    End Select
    'Resume Exit_Here
    End Sub
    Sub homephone()                                               'Home Phone Number
    On Error GoTo HandleErr                                         '*******************
    Dim varDialWait             As Variant
    Dim varStart                As Variant
    Dim lThreadId               As Long
    Dim lProcessId              As Long
    Dim ret                     As Long
    Dim strDial                 As String
    Dim lngRetVal               As Long
    Static blnDialing           As Boolean 'Prevent Re-Entry Into Sub While Dialing
    Dim strMsg                  As String
    Dim varReturn               As Long
    Dim lngX                    As Long
    Dim AppCaption              As String
    Dim hwnd                    As Long
    
    DoEvents
    
    blnDialing = False
    If blnDialing Then Exit Sub
    If Combobox_List_Search.TextBox10.Value = "" Then Exit Sub
                             'Conferm Dial Message
    If msgbox("Dial Number " _
    & vbNewLine _
    & Combobox_List_Search.ComboBox2.Value & " " & Combobox_List_Search.TextBox20.Value & " " _
    & "Mobile Phone" _
    & vbNewLine _
    & Combobox_List_Search.TextBox10.Text, _
    vbQuestion Or vbYesNo, "Phone Dialler") = vbNo Then Exit Sub Else
    
    strDial = Combobox_List_Search.TextBox10 ' Number To Dial
    lngRetVal = tapiRequestMakeCall(Trim$(strDial), "", "", "") 'Make call to Number Through Dialer.exe
    If lngRetVal = 0 Then blnDialing = True 'Exit sub on tapiRequestMakeCall Fail
    
    Call TestPlayWavFile ' Play Dial Sound Message
    
    For lngX = 1 To 10 ' Number of Loop Times For Sleep
    DoEvents
    Sleep 1000 ' Sleep 1 Second
    Next lngX
        
    'hwnd = FindWindow(vbNullString, AppCaption)
    
    hwnd = FindWindow(vbNullString, "Call Status")
        If hwnd <> 0 Then Call SendMessage(hwnd, WM_CLOSE, 0&, 0&)
    hwnd = FindWindow(vbNullString, "Phone Dialer")
        If hwnd <> 0 Then PostMessage hwnd, WM_CLOSE, 0&, 0&
    hwnd = FindWindow(vbNullString, "Dialer")
        If hwnd <> 0 Then
            PostMessage hwnd, WM_KEYDOWN, VK_RETURN, 0
            PostMessage hwnd, WM_KEYUP, VK_RETURN, 0
        End If
    
    Exit Sub
    HandleErr:
    Select Case Err.Number
    Case 94 'Invalid Use of Null - if double-click on blank, open dialer
    msgbox "here" 'Call Shell("c:\windows\dialer.exe", vbNormalFocus)
    Exit Sub
    Case Else
    'modHandler.LogErr ("frm0Telephone"),("Telephone_Numbers_DblClick" )
    End Select
    'Resume Exit_Here
    End Sub
    Sub phonemobile()                                               'Mobile Phone Number
    On Error GoTo HandleErr                                         '*******************
    Dim varDialWait             As Variant
    Dim varStart                As Variant
    Dim lThreadId               As Long
    Dim lProcessId              As Long
    Dim ret                     As Long
    Dim strDial                 As String
    Dim lngRetVal               As Long
    Static blnDialing           As Boolean 'Prevent Re-Entry Into Sub While Dialing
    Dim strMsg                  As String
    Dim varReturn               As Long
    Dim lngX                    As Long
    Dim AppCaption              As String
    Dim hwnd                    As Long
    
    DoEvents
    
    blnDialing = False
    If blnDialing Then Exit Sub
    If Combobox_List_Search.TextBox11.Value = "" Then Exit Sub
                             'Conferm Dial Message
    If msgbox("Dial Number " _
    & vbNewLine _
    & Combobox_List_Search.ComboBox2.Value & " " & Combobox_List_Search.TextBox20.Value & " " _
    & "Mobile Phone" _
    & vbNewLine _
    & Combobox_List_Search.TextBox11.Text, _
    vbQuestion Or vbYesNo, "Phone Dialler") = vbNo Then Exit Sub Else
    
    strDial = Combobox_List_Search.TextBox11 ' Number To Dial
    lngRetVal = tapiRequestMakeCall(Trim$(strDial), "", "", "") 'Make call to Number Through Dialer.exe
    If lngRetVal = 0 Then blnDialing = True 'Exit sub on tapiRequestMakeCall Fail
    
    Call TestPlayWavFile ' Play Dial Sound Message
    
    For lngX = 1 To 10 ' Number of Loop Times For Sleep
    DoEvents
    Sleep 1000 ' Sleep 1 Second
    Next lngX
        
    'hwnd = FindWindow(vbNullString, AppCaption)
    
    hwnd = FindWindow(vbNullString, "Call Status")
        If hwnd <> 0 Then Call SendMessage(hwnd, WM_CLOSE, 0&, 0&)
    hwnd = FindWindow(vbNullString, "Phone Dialer")
        If hwnd <> 0 Then PostMessage hwnd, WM_CLOSE, 0&, 0&
    hwnd = FindWindow(vbNullString, "Dialer")
        If hwnd <> 0 Then
            PostMessage hwnd, WM_KEYDOWN, VK_RETURN, 0
            PostMessage hwnd, WM_KEYUP, VK_RETURN, 0
        End If
    
    Exit Sub
    HandleErr:
    Select Case Err.Number
    Case 94 'Invalid Use of Null - if double-click on blank, open dialer
    msgbox "here" 'Call Shell("c:\windows\dialer.exe", vbNormalFocus)
    Exit Sub
    Case Else
    'modHandler.LogErr ("frm0Telephone"),("Telephone_Numbers_DblClick" )
    End Select
    'Resume Exit_Here
    End Sub
    Last edited by D_Rennie; 07-05-2009 at 09:56 AM.

  2. #2
    Valued Forum Contributor
    Join Date
    05-14-2009
    Location
    gold coast
    MS-Off Ver
    Excel 2007
    Posts
    843

    Re: Compressing multi used variables

    I call these subs from the userform that why i made 3 copys like that.

  3. #3
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,984

    Re: Compressing multi used variables

    Really, you only want one DialNumber routine, and then when you call it, you pass the number you want to ring as an argument. Your three routines are unnecessary as they are almost identical.
    Everyone who confuses correlation and causation ends up dead.

  4. #4
    Valued Forum Contributor
    Join Date
    05-14-2009
    Location
    gold coast
    MS-Off Ver
    Excel 2007
    Posts
    843

    Re: Compressing multi used variables

    Yes i had thet as a brain wave as i was typing it.
    There realy is only about 3 changes between them.
    I would like to keep it in its own module and im not sure if?how i can pass through the info for the double clicks of the textboxes. ( the only reason i want to keep it in its own module its getting aroung 1500 lines of code and i would get confused without some seperation"
    There are 3 text boxes that have own numbers each with a double click. so if there where to share the same sub im thinking that i would need to store the number as a variable and use it for the tapirewuestmakecall. or what ive played with before using other userform items Textboxes captions list ect That seam sloppy though.

    Cheers ill take a good look at how i can do it,

  5. #5
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,984

    Re: Compressing multi used variables

    Your dialling routine would be like this:
    Sub DialPhone(strDial As String)                                               'Mobile Phone Number
    On Error GoTo HandleErr                                         '*******************
    Dim varDialWait             As Variant
    Dim varStart                As Variant
    Dim lThreadId               As Long
    Dim lProcessId              As Long
    Dim ret                     As Long
    Dim strDial                 As String
    Dim lngRetVal               As Long
    Static blnDialing           As Boolean 'Prevent Re-Entry Into Sub While Dialing
    Dim strMsg                  As String
    Dim varReturn               As Long
    Dim lngX                    As Long
    Dim AppCaption              As String
    Dim hwnd                    As Long
    
    DoEvents
    
    blnDialing = False
    If blnDialing Then Exit Sub
    lngRetVal = tapiRequestMakeCall(Trim$(strDial), "", "", "") 'Make call to Number Through Dialer.exe
    If lngRetVal = 0 Then blnDialing = True 'Exit sub on tapiRequestMakeCall Fail
    
    Call TestPlayWavFile ' Play Dial Sound Message
    
    For lngX = 1 To 10 ' Number of Loop Times For Sleep
    DoEvents
    Sleep 1000 ' Sleep 1 Second
    Next lngX
        
    'hwnd = FindWindow(vbNullString, AppCaption)
    
    hwnd = FindWindow(vbNullString, "Call Status")
        If hwnd <> 0 Then Call SendMessage(hwnd, WM_CLOSE, 0&, 0&)
    hwnd = FindWindow(vbNullString, "Phone Dialer")
        If hwnd <> 0 Then PostMessage hwnd, WM_CLOSE, 0&, 0&
    hwnd = FindWindow(vbNullString, "Dialer")
        If hwnd <> 0 Then
            PostMessage hwnd, WM_KEYDOWN, VK_RETURN, 0
            PostMessage hwnd, WM_KEYUP, VK_RETURN, 0
        End If
    
    Exit Sub
    HandleErr:
    Select Case Err.Number
    Case 94 'Invalid Use of Null - if double-click on blank, open dialer
    MsgBox "here" 'Call Shell("c:\windows\dialer.exe", vbNormalFocus)
    Exit Sub
    Case Else
    'modHandler.LogErr ("frm0Telephone"),("Telephone_Numbers_DblClick" )
    End Select
    'Resume Exit_Here
    End Sub
    and you would call it from each control using:
    DialPhone Combobox_List_Search.TextBox10
    for example.

  6. #6
    Valued Forum Contributor
    Join Date
    05-14-2009
    Location
    gold coast
    MS-Off Ver
    Excel 2007
    Posts
    843

    Re: Compressing multi used variables

    Thankyou.
    I will have a look after the weekend,

  7. #7
    Valued Forum Contributor
    Join Date
    05-14-2009
    Location
    gold coast
    MS-Off Ver
    Excel 2007
    Posts
    843

    Re: Compressing multi used variables

    I havent tryed anythink yet "not enough time todoy"
    Though what i got from it is when calling a sub, A variable can be set to be used through the module. So DialPhone Combobox_List_Search.TextBox10.
    Would that still be call DialPhone Combobox_List_Search.TextBox10?
    then Sub DialPhone(strDial As String) textbox10 is like the transphered variable set as strdial.
    If thats correct it makes sence though leaves alot of questions of what would refer to what using more than one variable.

    Thankyou.

  8. #8
    Valued Forum Contributor
    Join Date
    05-14-2009
    Location
    gold coast
    MS-Off Ver
    Excel 2007
    Posts
    843

    Re: Compressing multi used variables

    romperstomper thanks for the help ive had a play around and understand passing the variables farily well. It works well with the dialing & left the msgbox in the module and passing variables to that fine.
    Cheers.

    The thing i dont get is why it dosent need the Call like i had before.
    Last edited by D_Rennie; 07-05-2009 at 09:55 AM.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1