mscomm与调制解调器的兼容性

时间:2013-04-23 07:53:57

标签: vba modem

我正在使用一个相当简单的脚本,自动拨打一组数字。它在我的计算机(带有集成调制解调器的笔记本电脑)上正常工作,但在我的同事计算机上(首先设计脚本)它适用于第一个号码,然后它根本不起作用(调制解调器赢了)除非我们重新启动计算机,否则不要拨打任何号码,也不会显示任何错误。什么设置或mscomm属性可以解释这种行为?我怀疑也许我在通话结束时关闭了扬声器,并且在新的开始时不要打开扬声器......

    Dim CancelFlag As Integer
Dim RingCount As Integer
Dim raccrocherFlag As Integer

' Démarrage
Private Sub btnStart_Click()
' On Error GoTo Gest_err
    For Each numtel In Sheets("ListeTRS").Range("b2:b10000")
            ' Si bouton Cancel a été pressé, on arrête le script
            If CancelFlag = 1 Then
                UserForm1.Hide
                Exit Sub
            End If


            valNumtel = Val(numtel)
            If valNumtel = 0 Then
                MsgBox "Fin de fichier."
                Exit Sub
            Else
                ActiveSheet.Range("A" & numtel.Row).Select
                If Application.Wait(Now + TimeValue("0:00:2")) Then Dial numtel, numtel.Row
            End If
    Next
End Sub


Private Sub Dial(Number, Indice)
Dim DialString As String
Dim FromModem As String
Dim dummy As Integer

' AT is the Hayes compatible ATTENTION command and is required to send commands to the modem.
' DT means "Dial Tone." The Dial command uses touch tones, as opposed to pulse (DP = Dial Pulse).
' Numbers is the phone number being dialed.
' A semicolon tells the modem to return to command mode after dialing (important).
' A carriage return, vbCr, is required when sending commands to the modem.

' Concatene 0 avant le numéro
dialnumber = "0" & Number

DialString = "ATDT" + dialnumber + ";" + vbCr

' Communications port settings.
' A faire > detecter le port ou le demander à l'usager?
Dim comPort As Integer
comPort = 3
MSComm1.CommPort = comPort
MSComm1.Settings = "9600,N,8,1"
RingCount = 0

On Error Resume Next
    ' Ouvrir le port s'il est fermé
    If (MSComm1.PortOpen = False) Then
        MSComm1.PortOpen = True
    End If


    If Err Then
        MsgBox "COM" & comPort & " n'est pas disponible. Changer la propriété à un autre port."
        Exit Sub
    End If

' Flush the input buffer.
MSComm1.InBufferCount = 0

' Dial the number.
        Sheets("ListeTRS").Range("D" & Indice).Value = "Appel en cours du " & dialnumber & " § " & Now
        MSComm1.Output = DialString


         ' On attend 10 seconde puis on lis les infos disponible dans le tampon du modem
        If Application.Wait(Now + TimeValue("0:00:20")) Then
        FromModem = FromModem + MSComm1.Input
        End If


            ' Raccrocher
            MSComm1.Output = "ATH" + vbCr



' Disconnect the modem.
MSComm1.Output = "ATH" + vbCr

' Close the port.
MSComm1.PortOpen = False
End Sub

Private Sub cmdStop_Click()
CancelFlag = 1
End Sub

Private Sub Worksheet_Activate()
'Setting InputLen to 0 tells MSComm to read the entire contents of the
'input buffer when the Input property is used.
MSComm1.InputLen = 0
End Sub



Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Excel.Range, Cancel As Boolean)
Dim Number As String

' Get the number to dial.
Number = Target.Value
If Number = "" Then Exit Sub

' Dial the selected phone number.
Dial (Number)
End Sub

1 个答案:

答案 0 :(得分:1)

我自己也处于类似情况。

您必须明确对象,在本例中为Excel。假设您已将xlApp命名为excel.aplication,请尝试以下操作:

For Each numtel In xlapp.Sheets("ListeTRS").Range("b2:b10000")

If valNumtel = 0 Then
                MsgBox "Fin de fichier."
                Exit Sub
            Else
                xlApp.ActiveSheet.Range("A" & numtel.Row).Select
                If xlApp.Application.Wait(Now + TimeValue("0:00:2")) Then Dial numtel, numtel.Row
            End If