Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

WebBrowser, NavigateError event not firing 1

Status
Not open for further replies.

Den1se

Programmer
Dec 8, 2005
4
0
0
GB
Hi,
I am having trouble with the WebBrowser control, the server is sending back an error code but the navigateerror event is not being fired for some reason. I am trying to upload a file using the WebBrowser control in MS Word. I have IE 6 installed.

Is there any other way of finding out the response?

The code is below, thanks for your help.

Option Explicit

Private WithEvents WebBrowser As InternetExplorer
Private strResult As String

Const URL As String = "
Private Sub Class_Initialize()

End Sub

Public Function SaveToServer(objDocument As Document, strUsername As String, strPassword As String) As String


' Upload it
UploadFile URL, objDocument.FullName, strUsername, strPassword

SaveToServer = strResult

End Function


Private Sub UploadFile(DestURL As String, FileName As String, _
strUsername As String, strPassword As String, _
Optional ByVal FieldName As String = "File")

Dim sFormData As String
Dim formData As String

'Boundary of fields.
'Be sure this string is Not In the source file
Const Boundary As String = "---------------------------0123456789012"

'Get source file As a string.
sFormData = GetFile(FileName)

formData = "--" + Boundary + vbCrLf
formData = formData + "Content-Disposition: form-data; name=""username""" & vbCrLf & vbCrLf
formData = formData + strUsername + vbCrLf

formData = formData + "--" + Boundary + vbCrLf
formData = formData + "Content-Disposition: form-data; name=""password""" + vbCrLf + vbCrLf
formData = formData + strPassword + vbCrLf

'Build source form with file contents
formData = formData + "--" + Boundary + vbCrLf
formData = formData + "Content-Disposition: form-data; name=""" + FieldName + """;"
formData = formData + " filename=""" + FileName + """" + vbCrLf
formData = formData + "Content-Type: application/upload" + vbCrLf + vbCrLf

formData = formData + sFormData
formData = formData + vbCrLf + "--" + Boundary + "--" + vbCrLf

'Post the data To the destination URL
IEPostStringRequest DestURL, formData, Boundary
End Sub

'sends URL encoded form data To the URL using IE
Private Function IEPostStringRequest(URL As String, formData As String, Boundary As String)

On Error GoTo localError

'Create InternetExplorer
Set WebBrowser = New InternetExplorer


'Send the form data To URL As POST request
Dim bFormData() As Byte
ReDim bFormData(Len(formData) - 1)
bFormData = StrConv(formData, vbFromUnicode)

WebBrowser.Visible = False
strResult = ""
WebBrowser.Navigate URL, , , bFormData, _
"Content-Type: multipart/form-data; boundary=" + Boundary + vbCrLf

Do While WebBrowser.Busy
' Sleep 100
DoEvents
Loop


WebBrowser.Quit

Exit Function

localError:

If Not WebBrowser Is Nothing Then
WebBrowser.Quit
End If

End Function

'read binary file As a string value
Function GetFile(FileName As String) As String
Dim FileContents() As Byte
Dim intFileNumber As Integer
ReDim FileContents(FileLen(FileName) - 1)
intFileNumber = FreeFile

Open FileName For Binary As intFileNumber

Get intFileNumber, , FileContents
Close intFileNumber

GetFile = StrConv(FileContents, vbUnicode)
End Function

Private Sub WebBrowser_NavigateError(ByVal pDisp As Object, URL As Variant, Frame As Variant, StatusCode As Variant, Cancel As Boolean)

If StatusCode = 403 Then
' Bad username/password
strResult = "Incorrect Username/Password"
ElseIf StatusCode = 500 Then
' Bad data /broken server
strResult = "Server error - you may be able to try again"
Else
strResult = "Unknown error"
End If

Cancel = True
End Sub
 
I'm pretty sure that the [tt]NavigateError()[/tt] event will only fire when a WebBrowser object is embedded in a UserForm.

Work around, create a form with a WebBrowser control, load the form in lieu of creating a new [tt]InternetExplorer[/tt] object, but keep the form hidden.

You will still be able to use all the properties and methods but the WebBrowser object will throw errors that the UserForm in Word will be able to capture and handle.

Hope this helps,
CMP

Instant programmer, just add coffee.
 
Hi, thanks for your response.

I have tried adding a UserForm with a WebBrowser ctrl on but I still get the same problem however I am new to VB and I'm not sure if what I have done is what you mean.

Instead of Set WebBrowser = new InternetExplorer

I have put this code in (also changing the
Private WithEvents WebBrowser As InternetExplorer
to
Private WithEvents WebBrowser As WebBrowser):

Load frmBrowser
frmBrowser.Hide

Set WebBrowser = frmBrowser.WebBrowser1

But the navigateerror still isn't being fired. :-(

 
[ol][li]Navigate to the VBE pane ([tt]Alt+F11[/tt])[/li]
[li]Create a new UserForm ([tt]Insert, UserForm[/tt])[/li]
[li]Add a WebBrowser Control to the form ([tt]Tools, Additional Controls[/tt][ul][li]Select [tt]Microsoft Web Browser[/tt][/li][li]This will add the control to the Control Toolbox (the globe)[/li][li]Click this new icon then draw the new control on the form[/ul][/li]
[li]Right Click on the form and select [tt]View Code[/tt][/li]
[li]Move all your existing code into the module for this new UserForm.[/li][/ol]
Note that you will need to change all the instances of [tt]WebBrowser[/tt] to [tt]WebBrowser1[/tt] (the default name for this control). The easiest way is to start typing [tt]Me.[/tt] since this should cause a flyout menu for all the posibilies.

Work calls,
CMP

Instant programmer, just add coffee.
 
Thanks for that post.

I have now added the WebBrowser control to my existing form, moved the code above into that module and removed
Private WithEvents WebBrowser1 As WebBrowser

The navigateerror event is now being fired. Thanks for your help.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top