VB6: get onreadystate value from browser control - vb6

I'm trying to get the onreadystate value from the browser control do detect when the page has finished loading. Unfortunately, the event's .returnValue gets returned empty.
Here's my code:
Dim WithEvents m_doc As HTMLDocument
Private Sub Form_Load()
Set m_doc = WebBrowser1.Document
End Sub
Private Sub m_doc_onreadystatechange()
Dim m_event As IHTMLEventObj
Set m_event = m_doc.parentWindow.event
m_value = "'" & m_event.returnValue & "'"
MsgBox "onreadystatechange: " & m_value
End Sub
Any ideas on what's wrong?

If you want to use the HTMLDocument's events try
m_doc.createDocumentFromUrl "http://www.microsoft.com", ""
Otherwise you can use the WebBrowser control's event to detect when a document is completely loaded or call the Navigate or Navigate2 method and immediately loop while polling the WebBrowser.ReadyState
WebBrowse1.Navigate2 "http://www.microsoft.com"
Do While WebBrowser1.ReadyState <> READYSTATE_COMPLETE
DoEvents
Loop
Of course don't forget to add an error handler.

Related

Enabling Outlook Plugin In Preview Mode

I have created an extension that extracts some parameters for the email and forwards it to our platform. It's working fine but now I want to make sure that my extension works even in preview mode. We don't have to open an email in order to use an extension.
I couldn't find any configuration to enable the plugin in preview mode.
It seems you need to handle the SelectionChange event of the Explorer class. It is fired when the user selects a different or additional Microsoft Outlook item programmatically or by interacting with the user interface. This event also occurs when the user (either programmatically or via the user interface) clicks or switches to a different folder that contains items, because Outlook automatically selects the first item in that folder. However, this event does not occur if the folder is a file-system folder or if any folder with a current Web view is displayed.
Public WithEvents myOlExp As Outlook.Explorer
Public Sub Initialize_handler()
Set myOlExp = Application.ActiveExplorer
End Sub
Private Sub myOlExp_SelectionChange()
MsgBox myOlExp.Selection.Count & " items selected."
End Sub
The Explorer.Selection property returns a Selection object that contains the item or items that are selected in the explorer window. Here is the sample how you can deal with the Selection object:
Sub GetSelectedItems()
Dim myOlExp As Outlook.Explorer
Dim myOlSel As Outlook.Selection
Dim mySender As Outlook.AddressEntry
Dim oMail As Outlook.MailItem
Dim oAppt As Outlook.AppointmentItem
Dim oPA As Outlook.PropertyAccessor
Dim strSenderID As String
Const PR_SENT_REPRESENTING_ENTRYID As String = "http://schemas.microsoft.com/mapi/proptag/0x00410102"
Dim MsgTxt As String
Dim x As Long
MsgTxt = "Senders of selected items:"
Set myOlExp = Application.ActiveExplorer
Set myOlSel = myOlExp.Selection
For x = 1 To myOlSel.Count
If myOlSel.Item(x).Class = OlObjectClass.olMail Then
' For mail item, use the SenderName property.
Set oMail = myOlSel.Item(x)
MsgTxt = MsgTxt & oMail.SenderName & ";"
ElseIf myOlSel.Item(x).Class = OlObjectClass.olAppointment Then
' For appointment item, use the Organizer property.
Set oAppt = myOlSel.Item(x)
MsgTxt = MsgTxt & oAppt.Organizer & ";"
Else
' For other items, use the property accessor to get the sender ID,
' then get the address entry to display the sender name.
Set oPA = myOlSel.Item(x).PropertyAccessor
strSenderID = oPA.GetProperty(PR_SENT_REPRESENTING_ENTRYID)
Set mySender = Application.Session.GetAddressEntryFromID(strSenderID)
MsgTxt = MsgTxt & mySender.Name & ";"
End If
Next x
Debug.Print MsgTxt
End Sub

VSTO Word Application events for new Word documents, windows or instances

In a VSTO addin for Word 16 (32) on a Windows 10, I keep track of each document for Ribbon purposes.
I am using the VSTO Designer's Application object. In the designer it looks like this;
Friend WithEvents Application As Microsoft.Office.Interop.Word.Application
Then in ThisAddinn I have the standard code for a WithEvents object.
Private Sub Application_NewDocument(Doc As Microsoft.Office.Interop.Word.Document) Handles Application.NewDocument
End Sub
In ThisAddin I also handle Application.DocumentOpen
After the first instance of Word is open if a user right clicks on the Word icon in the task bar and chooses Word then a new Word document is created, however the above 2 events do not fire.
I also wanted to point out that opening a document from the Word interface or clicking on a word.docx file and opening it all work. It is only when you open a new instance of work by right clicking on the word icon in the task bar.
What event do I need?
I do see that Application.WindowActivate fires. Do I need to use this?
The Application.NewDocument event is fired when a new document is created. If you are working with a document embedded within another document, this event will not occur. You may try to use the following VBA macro to make sure events are fired:
Public WithEvents appWord as Word.Application
Private Sub appWord_NewDocument(ByVal Doc As Document)
Dim intResponse As Integer
Dim strName As String
Dim docLoop As Document
intResponse = MsgBox("Save all other documents?", vbYesNo)
If intResponse = vbYes Then
strName = ActiveDocument.Name
For Each docLoop In Documents
With docLoop
If .Name <> strName Then
.Save
End If
End With
Next docLoop
End If
End Sub
The Application.DocumentOpen event is fired when a document is opened.
Public WithEvents appWord as Word.Application
Private Sub appWord_DocumentOpen(ByVal Doc As Document)
Dim intResponse As Integer
Dim strName As String
Dim docLoop As Document
intResponse = MsgBox("Save all other documents?", vbYesNo)
If intResponse = vbYes Then
strName = ActiveDocument.Name
For Each docLoop In Documents
With docLoop
If .Name <> strName Then
.Save
End If
End With
Next docLoop
End If
End Sub
At startup, you can check whether any document is opened and simulate the DocumentOpen event fired. It seems that events can be fired before you subscribe to them.

Quitting the IE in the end of VBA function

I implemented several functions which relies on downloading some information from some websites.
The simplest example of such a function is:
Public Function getSomething(webAddress As String)
Dim html As HTMLObjectElement
Set html = getWebContents(webAddress)
Set elems = html.body.getElementsByTagName(tagName)
...
End Function
The function for acquire data from websites is:
Public Function getWebContents(webAddress As String) As HTMLObjectElement
Dim ie As InternetExplorer
Dim html As HTMLDocument
Set ie = New InternetExplorer
ie.Visible = False
ie.Navigate webAddress
Do While ie.READYSTATE <> READYSTATE_COMPLETE
Application.StatusBar = "Trying ..."
DoEvents
Loop
Set getWebContents = ie.Document
'close down IE and reset status bar
'ie.Quit
Set ie = Nothing
Application.StatusBar = ""
End Function
The problem is that it seems that I need the line ie.Quit to be uncommented to close the IE instance. But when I uncomment ie.Quit the line
Set elems = html.body.getElementsByTagName(tagName)
generates errors.
It seems that I cannot use HTMLObjectElement returned by function getWebContents when IE has been quitted. How to deal with that? I could implement a try...finally block in getSomething function and open ie there and close in the finally block. However I have many functions of a similar nature and making many similar try...finally blocks seems a stupid idea.
Any thoughts?
Thanks!
You should define a procedure to handle the object lifetime from creation to destruction. You can then pass a reference for the object to the function.
Lastly, you can dispose the object even if an error occurs at any stange.
Public Sub Main()
On Error GoTo ErrProc
Dim ie As InternetExplorer
Set ie = New InternetExplorer
'....
Dim obj As Object
obj = getWebContents(ie, "url")
Leave:
ie.Quit
Set ie = Nothing
Set obj = Nothing
Application.StatusBar = ""
On Error GoTo 0
Exit Sub
ErrProc:
MsgBox Err.Description, vbCritical
Resume Leave
End Sub
Public Function getWebContents(ie As InternetExplorer, webAddress As String) As HTMLObjectElement
'...
End Function
You are keeping a pointer to the DOM in the html variable. If you close IE, you are pointing to something non-existing.
The simple answer is to close IE at the end of getSomething. In your case, this means that you have to restructure your code so that your IE variable is accessible from other places than in getWebContents

Why does my VBA script not continue after debugging?

Whenever I hit an error with my script, the focus turns to the VBA code and the offending line. I fix it, and hit save. Then I notice that the script is no longer running, even after I make sure that it's not paused.
For example, right now I'm using a Form_Timer() event to do some testing (interval set to 1000ms). To test the script again, I just set it to a minute in the future (e.g. if the current time is 8:54:00 AM I set it to fire at 8:55:00 AM). But this stops working after an error. Does anyone know why this is? I don't want to have to tell my users to close and re-open their copies of the Access DB just to make the script work again.
Code:
Private Sub Form_Timer()
On Error GoTo ErrorHandler
current_date_time = Now
If current_date_time = #6/28/2016 8:52:00 AM# Then
MsgBox ("the current_date_time variable holds: " & current_date_time)
'Declare objects
Dim dbs As DAO.Database
Dim rst As DAO.Recordset
Dim qdf As DAO.QueryDef
Dim oApp As Outlook.Application
Dim oMail As Outlook.MailItem
Dim mail_body As String
'Set objects
Set dbs = CurrentDb
Set qdf = dbs.QueryDefs("qry_BMBFLoc")
Set rst = qdf.OpenRecordset
Set oApp = New Outlook.Application
Set oMail = oApp.CreateItem(olMailItem)
mail_body = "The following jobs do not have the special BF location set in Job Orders: " & vbCrLf
If Not (rst.EOF And rst.BOF) Then
rst.MoveFirst
Do Until rst.EOF = True
mail_body = mail_body & rst!job & "-" & rst!suffix & vbCrLf
rst.MoveNext
Loop
'Email contents
oMail.Body = mail_body
oMail.Subject = "Blah"
oMail.To = "someone#something.com"
oMail.Send
'Close stuff
rst.Close
dbs.Close
Set rst = Nothing
Set oMail = Nothing
Set oApp = Nothing
End If
End If
Exit Sub
ErrorHandler:
Dim msg As String
If Err.Number <> 0 Then
msg = "email Form Timer Error #" & Str(Err.Number) & " error Line: " & Erl & Chr(13) & Err.Description
MsgBox msg, , "Error", Err.HelpFile, Err.HelpContext
End If
Exit Sub
End Sub
In order to reactivate the code, you could close the form when the error is triggered. The user would then have to reload the form to complete the action.
However, without any intervention the error is likely to occur again.
Edit: Or you could write a Function to automatically close, and re-open the offending form. Calling it in the on error command.
When there is an error in access form, the timer will stop working, you don't need to close and reopen the whole database, only the form to start the timer again. Otherwise you can add a button called "refresh" and bind macro to it which will turn the timer on again.
Yeah this sucks. I am writing a vba script for outlook and so the only way to debug is to close and reopen outlook after every error.

Sending data before winsock closes in Visual Basic 6?

I'm trying to send data to my server when my client closes the form or when the stop button is hit and for some reason it's not working.
Winsock.SendData "USERLEAVES" & txtUser.Text
Winsock.Close
It's like the winsock is closing before the data can be sent. How can I fix this?
have a look at the _SendComplete() event
for example, using a form level boolean :
Option Explicit
Private mblnClosing As Boolean
Private Sub Command1_Click()
Winsock1.SendData "USERLEAVES" & txtUser.Text
mblnClosing = True
End Sub
Private Sub Form_Load()
mblnClosing = False
End Sub
Private Sub Winsock1_SendComplete()
If mblnClosing Then
Winsock1.Close
End If
End Sub

Resources