Option Explicit
Const C_URL = "http://www.**************.com"
Const C_URL1 = C_URL & "/user"
Const C_URL2 = C_URL & "/create/question"
Const C_USER = "EE-Test"
Const C_PASS = "EE-Test"
Dim objIE As InternetExplorer
Sub DoProcess()
Set objIE = New InternetExplorer
objIE.Visible = True
Dim h As HTMLDocument
Dim f As HTMLFormElement
Dim fe As HTMLFormElement
Dim fsb As HTMLInputButtonElement
' login
objIE.navigate C_URL1
DoWait
Set h = objIE.document
Set f = h.forms("user-login")
If Not f Is Nothing Then
For Each fe In f.elements
If fe.Name = "name" Then fe.innerText = C_USER
If fe.Name = "pass" Then fe.innerText = C_PASS
Next
f.submit
DoWait
Set f = Nothing
End If
Set h = Nothing
Dim rw As Integer ' row
rw = 2
' now post questions
While ActiveSheet.Cells(rw, 1) <> ""
Debug.Print "row: " & rw
objIE.navigate C_URL2
DoWait
Set h = objIE.document
h.getElementById("switch_edit-body").Click
DoWait
Set fsb = Nothing
Set f = h.forms("question-form")(0) ' there are 2 forms with this ID/name (1 hidden), we need the 1st one
For Each fe In f.elements
Dim feName As String, feID As String
feName = ""
feID = ""
On Error Resume Next
feName = fe.Name
feID = fe.ID
If Err.Number <> 0 Then Err.Clear
On Error GoTo 0
Debug.Print "*Name:" & feName & "*ID:" & feID & "*"
If feName = "title" Then fe.innerText = ActiveSheet.Cells(rw, 1)
If feID = "edit-taxonomy-3_1" Then
fe.selectedIndex = CLng(ActiveSheet.Cells(rw, 2))
fe.FireEvent "onchange", fe
DoWait
End If
If feID = "edit-taxonomy-3_2" Then ' subcategory is hidden
fe.selectedIndex = 1 'CLng(ActiveSheet.Cells(rw, 2))
End If
If feName = "body" Then fe.innerText = ActiveSheet.Cells(rw, 3)
If feID = "edit-submit" Then Set fsb = fe ' Save button
Next
If fsb Is Nothing Then
Debug.Print "* form submit"
f.submit
Else
Debug.Print "* Save click"
fsb.Click
End If
DoWait
Set fsb = Nothing
Set f = Nothing
Set h = Nothing
rw = rw + 1
Wend
' done
objIE.Quit
Set objIE = Nothing
End Sub
Sub DoWait()
While objIE.Busy Or objIE.readyState <> READYSTATE_COMPLETE
DoEvents
Wend
End Sub
PS: to use this code you need to add 2 references to your Excel VBA project: "Microsoft HTML Object Library" and "Microsoft Internet Controls".Function GetCatIndex(sCatName As String)
Select Case sCatName
Case "Hardware":
GetCatIndex = 2
Case "Linux":
GetCatIndex = 3
Case "Apple":
GetCatIndex = 4
' ...
Case "Windows OS":
GetCatIndex = 18
' unknown -> Anonymous?
Case Else:
GetCatIndex = 1
End Select
End Function
and then use that on line 61 of the posted code:
fe.selectedIndex = GetCatIndex(ActiveSheet.Cells(rw, 2))
If feID = "edit-taxonomy-3_1" Then
Dim fse As HTMLSelectElement, idx As Long
Set fse = fe
For idx = 1 To fse.Options.Length - 1
Debug.Print "** check option: " & idx & " - " & fse.Options(idx).Text
If fse.Options(idx).Text = ActiveSheet.Cells(rw, 2) Then
fse.selectedIndex = idx
Exit For
End If
Next
' set default category?
If fse.selectedIndex = 0 Then fse.selectedIndex = 1
Set fse = Nothing
'fe.selectedIndex = GetCatIndex(ActiveSheet.Cells(rw, 2))
'fe.FireEvent "onchange", fe
'DoWait
End If
sub category is not mandateok I did put the code for that back in the previous post but either way shouldn't matter I guess. Probably for testing (like I did at first) using a question that contains too few words is what went wrong earlier?
This link is fine
http://tinyurl.com/d57ffgs
Need to login to get to the page
Its a secured user account so will disable it after some time
We can do it with excel i guess but not sure
thanks for your help