VB6.0 SMS Message box - vb6

Hi guys i have this problem.
The problem is that i dont know how to make a message box whether the message is sent or failed.
how am i going to code it i totaly have no idea.
i cant search it on google. i hope you could help me out
im using GSM Modem.below is my sending code.
MSComm1.Output = "AT" & vbCrLf
Sleep 100
MSComm1.Output = "AT+CMGF=1" & vbCrLf
Sleep 200
MSComm1.Output = "AT+CMGS=" & Chr(34) & MobileNum & Chr(34) & vbCrLf
MSComm1.Output = TMPMESEJ & Chr(26)
List1.AddItem MSComm1.Input
Sleep 300

You need to handle the logic in OnComm Event of MSCOMM control.
The OnComm event is generated whenever the value of the CommEvent property of MSCOMM control changes, indicating that either a communication event or an error occurred.
You need to write a Select Case statement on MSComm.CommEvent and analyse the outcome.
Refer to http://support.microsoft.com/kb/194922

Related

Is it possible to hide an error message generated by VBS script?

I have a customisation on an application that opens the comport to start reading data from the GPS. Now that I am using a Bluetooth GPS receiver the port doesn't always open the first time. If it fails it give the message "Error 121 when attempting to open communications port COM(comport number):"
Now when it does this I can check to see if the command worked, and if it didn't fire it again... (see code for the button below) However it displays an error.
This mimics what happens when you do it manually... sometimes it takes the Bluetooth stack multiple tries to connect to the GPS... It doesn't display an error though...
Anyway, it works, but it would be great to not show the error message on the times where the comport doesn't open...
Any and all suggestions are welcome, and appreciated.
Notes:
-The program is working without problems on everything but new units which have to use the Microsoft Bluetooth stack.
-While this error is annoying, the system is otherwise working, and when the Bluetooth connection is made after 1-3 attempts, all is well...
-Before I added "On Error Resume Next" the process would stop whenever the connection didn't get made.
Sub subGPSOnOffButton
'turns the GPS and tracklog on (or off) from a custom button...
'if the gps is on, make sure the user wants it off...
'if the gps is off, turn it on, and tell the user when done.
Dim iRes
If Application.GPS.IsOpen = True Then
iRes = msgbox("GPS is already on!" & vbnewline & vbnewline & "Turn it off?", vbYesNo, "GPS Active:")
If iRes = 6 then
Application.GPS.Close()
Else
Exit Sub
End if
Else
subGPSOn
msgbox "GPS is now on!", vbOKOnly, "GPS Active:"
End If
End Sub
Sub subGPSOn
On Error Resume Next
Do While Application.GPS.IsOpen = False
' Turn the GPS on
Application.GPS.Open()
Loop
' Turn the tracklog on
Application.ExecuteCommand("gpstracklog")
End Sub

Close Open Excel Instance

Could someone please let me know if the following simple VBScript is correct?
It is supposed to close Excel after other processes have run (and left Excel open), but it doesn't work.
Set MyApp = CreateObject("Excel.Application")
MyApp.Quit
CreateObject creates a new object. If I understand your question correctly you want to attach to already running (orphaned) Excel processes to terminate them. You can do that with GetObject:
On Error Resume Next
Do
Set xl = GetObject(, "Excel.Application")
status = Err.Number
If status = 0 Then
For Each wb in xl.Workbooks
wb.Close False 'discard changes in open workbooks
Next
xl.Quit
ElseIf status <> 429 Then
WScript.Echo Err.Number & ": " & Err.Description
WScript.Quit 1
End If
Until status = 429
On Error Goto 0
Note that this will try to close all running Excel instances, discarding all changes in open workbooks. If you want it to save changes in open workbooks change the argument of the Close method to True. If you have Excel instances you want to keep running, you need to add code to exclude them from being closed.
Note also, that this will not forcibly terminate unresponsive instances. You'd need to kill the process for that:
Set wmi = GetObject("winmgmts://root/cimv2")
For Each xl In wmi.ExecQuery("SELECT * FROM Win32_Process WHERE Name = 'excel.exe'")
xl.Terminate
Next
Try this please.
ThisWorkbook.Saved = True
Application.Quit
CreateObject creates a COM object, so your
Set MyApp = CreateObject("Excel.Application")
starts a new Excel process. Use GetObject to "retrieve an existing object with the specified ProgID". See this for theory and praxis.

How does this script (sending email using Outlook) work?

Can anyone help me understand how MAPI works? I have this simple vbscript that uses MAPI to send an email out. It works perfectly fine, but, I don't know how it is doing that...what's happening in the background?
Const ForReading = 1
Set args = WScript.Arguments
sqlFile = args.Item(0)
logFile = args.Item(1)
Dim ToAddress
Dim FromAddress
Dim MessageSubject
Dim MyTime
Dim MessageBody
Dim MessageAttachment
Dim ol, ns, newMail
MyTime = Now
ToAddress = "my#email.com"
MessageSubject = "Subject goes here"
MessageBody = "Body message goes here."
MessageAttachment = ""&logFile&""
Set ol = WScript.CreateObject("Outlook.Application")
Set ns = ol.getNamespace("MAPI")
Set newMail = ol.CreateItem(olMailItem)
newMail.Subject = MessageSubject
newMail.Body = MessageBody & vbCrLf & MyTime
newMail.RecipIents.Add(ToAddress)
newMail.Attachments.Add(MessageAttachment)
newMail.Send
Thanks in advance...
What you are doing in this code above is actually using the installed version of Outlook to send the mail message. Outlook may very well be using MAPI, but the API interface you are actually using here is COM based automation of Outlook. Code such as this will utilize Outlook to send email through any mail transportation system that Outlook can be configured to use. However, that is a lot of overhead just to send an email message. Depending on what email server(s) you might have local to you on your network or via there may be much more efficient ways to send email. But if this works fine and meets your needs currently, don't take that to mean that there is anything wrong with doing it the way you have above. It is all about understanding the tools available to you and how to best apply them to your particular problem space.
In your code, you are controlling Outlook and telling it to create an email message. You are passing off the message to Outlook and then Outlook is actually using the configured profile it has to determine how to hand off the message to a configured Exchange server or other installed/configure mail transport agents (mta).

Visio automation: get process ID

I'm running Visio using automation and I'm having trouble getting the process ID of the Visio process to check when its complete. Here's my VB script:
Set visio = CreateObject("Visio.InvisibleApp")
Wscript.Echo visio.ProcessID
Set document = visio.Documents.OpenEx("somefile.vsd", &H88)
document.ExportAsFixedFormat 1, "somefile.pdf", 1, 0
visio.Quit
and running it with cscript // nologo.
The problem is that visio.ProcessID returns a number that isn't the actual Windows process ID (e.g. 6613 when the actual process ID is 8146). The cscript host seems to finish before the Visio process exits causing problems cleaning up temp files.
Here is the Visio reference notes for:
ProcessID
Quit
So the question is: how can I get the Visio process ID or detect when it has properly exited?
Thanks!
Just noticed this in the help for Visio.Application.ProcessID:
"The value returned by ProcessID is not the same as the Windows Process ID of the current Visio instance."
So I guess these are just so you can distinguish between multiple instances of Visio.
There are also Visio.Application.WindowHandle32 and Visio.Application.WindowHandle, which might be helpful, although maybe not so much for an invisible app instance.
Can't see how this id would help you. I suspect you get an error in visio. Have you tried with
on error resume next
and after each line that could give an error
if err.number <> 0 then
wscript.echo err.description
err.clear
end if
There seems to be an issue if you omit parameters, so use them all.
See http://msdn.microsoft.com/en-us/library/office/ms409271(v=office.12).aspx for the values.
Before quiting use document.saved = true, you could check first if the result file exists.
EDIT: check if a process is running, could be you have to adapt the name of the service (check your téaskmanager)
set service = GetObject ("winmgmts:")
for each Process in Service.InstancesOf ("Win32_Process")
If lcase(Process.Name) = "visio.exe" then
wscript.echo "visio still running"
wscript.quit
End If
next
wscript.echo "visio no longer running"
EDIT2: to get the processid of the active visio app (If more than one Visio instance is running, GetObject returns the active instance. When a program is run as an add-on or by double-clicking a shape, the active instance is the one that the program was run from. Otherwise, it is the instance that was most recently run or brought to the front. If no Visio instance is running, GetObject causes an error)
cfr http://webmail.vh.com.tw/Microsoft/Developing%20Microsoft%20Visio%20Solutions/27.htm
set appObj = GetObject(, "visio.application")
if appObj Is Nothing Then
wscript.echo "There is no active Visio."
else
wscript.echo "ProcessID: " & appObj.ProcessID
end if

How to know a winsock has completed receiving file?

I'm using the Winsock control:
Private Sub Form_Load()
Winsock1.Connect "stackoverflow.com", 80
End Sub
Private Sub Winsock1_Close()
Winsock1.Close
End Sub
Private Sub Winsock1_Connect()
Winsock1.SendData "GET /questions/8624871/vb6-alternative-to-inet-webbrowser-control HTTP/1.1" & vbCrLf & "Host: stackoverflow.com" & vbCrLf & vbCrLf
End Sub
Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long)
Dim s As String
Winsock1.GetData s, vbString
RichTextBox1.Text = RichTextBox1.Text & s
End Sub
How can I know that the control has completed receiving the file when the header doesn't contain Content-Length?
I've heard of some ways, like when Winsock1.state is 0 it means that the connection is closed, but sometimes it remains in some other state, like 7, so I need another solution.
You need to parse the received HTTP headers. RFC 2616 Section 4.4 explains how they tell you the length of the data and how the data needs to be read.
There are a number of components you can use in VB6 for making HTTP requests. Some come with VB6, some are part of Windows now. Most of these are far better than rolling your own HTTP on top of TCP using the Winsock control. Header processing is just one of the things they can assist you in.
If you still want to roll your own HTTP client, you should read the HTTP spec rather than guessing.
HTTP transfers use the Content-Length header to determine the length of the data. If the header is missing, then the end of data is signalled by the connection closing.

Resources