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 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