Uploading to the Web with VBA - excel

<div id="xe-editor-container-1" class="input_area xpress_xeditor_editing_area_container" style="height: 400px;">
<iframe id="editor_iframe_1" allowtransparency="true" frameborder="0" src="http://my_URL.or.kr/xe/modules/editor/styles/default/editor.html" scrolling="yes" style="width: 100%; height: 400px; display: block;">
<html xmlns="http://www.w3.org/1999/xhtml>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<link rel="stylesheet" type="text/css" href="editor.css">
<title>XpressEngine</title>
</head>
<body class="xe_content editable"></body>
</html>
</iframe>
<textarea id="xpress-editor-1" rows="8" cols="42" style="display: none; width: 100%;"></textarea>
<textarea rows="8" cols="42" class="input_syntax " style="display:none"></textarea>
</div>
I want to copy two tables, ListObjects ("Tbl1") on Sheet1, ListObjects ("Tbl2") on Sheet2, and upload them as a single post on the web.
The range of the table can be changed every time.
Logging in to the web, navigating to the bulletin board, pressing the write button and typing the title succeeded.
But I have failed to upload the post.
Perhaps you can not find the bulletin board object.
Code I created by searching the web.
The HTML above is the board's HTML code.
With ie
.navigate "http://my_URL/offering"
ieBusy ie 'Procedure fetched as search (check the ready status)
.Document.getElementsByClassName("ico_16px write")(0).Click
ieBusy ie
Dim oTitle As Object, Ocontents As Object
Set oTitle = .Document.getElementsByname("title")(0) 'Sometimes fail(sometimes Nothing)
Set oContents = .Document.getElementsByClassName("xe_content editable")(0) 'evry time fail(=Nothing)
oTitle.Value = "my Title"
oContents.Value = ????
.Document.forms(0).submit 'I could not confirm it because it did not work anymore.
End With
Sub ieBusy(ie As Object)
Do While ie.Busy Or ie.readyState < 4
DoEvents
Loop
End Sub
title HTML
board HTML

1) Use an additional timed loop to set oTitle as per https://stackoverflow.com/a/55334183/6241235
2) Your oContents variable is selecting for an element which is inside an iframe I think. I would expect you to instead be targeting a textarea element. There are two that come after the iframe. The first has id xpress-editor-1

Related

HTA. how to fade-out the embedded music mp3 (reload) [duplicate]

This question already has answers here:
Fade out of a Mp3 song using javascript
(2 answers)
Closed 12 months ago.
I really need some help IN All the examples I found the fade out effect was relying on the "audio id" element :
<audio id="myAudio">
<source src="./LZ.mp3" type='audio/mp3'>
</audio>
in all cases my HTA will not play any sound
It seem I can only use the "embed" or even the "bgsound" method
but then I'm lost on applying the needed fade out functions
The following code is the simplified Hta script I use to start a splash together with the mp3 music file.
<html>
<head>
<script language="vbscript">
winWidth=350
winHeight=90
window.resizeto winWidth,winHeight
centerX=(screen.width-winWidth)/2
centerY=(screen.height-winHeight)/2
</script>
</head>
<head>
<HTA:APPLICATION
ID="start"
VERSION="2.0"/>
<body>
<button style=" title="">Starting </button> <p>
</body>
</head>
<script type="text/javascript">
function countdown() {
var i = document.getElementById('start');
{
window.close();
}
}
setInterval(function(){ countdown(); },30000);
</script>
<body>
<embed audio src="./LZ.mp3"></audio >
</body>
</html>
The splash will last 30 secs. and on closing will cut the sounds
I would like to add a fade out effect after 25 secs (5 secs. before the end)
(I cannot apply the fade out effect directly to the mp3 file because very quickly using another script I can chose to use any other mp3 )
the music will auto-start and the fade out effect should be automatic ( I mean no manual buttons)
EDIT: thanks to LesFerch my simplified hta now is playing + the music is closing with the aimed fade out effect. I'm just trying to add again the "Starting" title and the colors effects:
<body>
<button style= title="">Starting Firefox</button> <p>
<script language="VBScript">
Dim Intrval, direction
Dim c 'Used to increment
Function Color(i) : Color = "#" & Hex(i) & Hex(ii) & Hex(i) : end function
Sub Window_Onload
c = &hff
direction = -2
Intrval = setInterval("changeColor", 20)
end sub
sub changeColor
document.body.bgColor = Color(c)
c = (c + direction)
if c > (&hff - Abs(direction)) or c < Abs(direction) then direction = -direction
end sub
</script>
Using the answer in the duplicate link, I made some adjustments to eliminate an IndexSizeError, play the mp3 without a button, and make the fade last a reasonable amount of time. Since your sample code starts off in VBScript, I've assumed that's your preferred language. If not, I can post a JScript version as well.
Note: What makes this, perhaps, not a duplicate is that, for an HTA, the document mode must be declared and set to IE=9 or higher.
Note: Document mode IE=9 and higher are much more case sensitive. For example, screen.availWidth will work, screen.AvailWidth will throw an error.
<!DOCTYPE html>
<html>
<head>
<title>Fade Test</title>
<meta charset="UTF-8" http-equiv="X-UA-Compatible" content="IE=9">
<hta:application
id=oHTA
icon=SndVol.exe
>
<script language="vbscript">
Set oWSH = CreateObject("Wscript.Shell")
Set oFSO = CreateObject("Scripting.FileSystemObject")
w = 350
h = 90
window.resizeTo w, h
window.moveTo (screen.availWidth - w)/2, (screen.availHeight - h)/2
MyPath = Mid(document.URL,8)
MyFolder = oFSO.GetParentFolderName(MyPath)
oWSH.CurrentDirectory = MyFolder
Sub window_onLoad
mp3.play
FadeOut
End Sub
Sub FadeOut
If mp3.volume >= 0.1 Then
mp3.volume = mp3.volume - 0.1
window.setTimeout "FadeOut()", 2000
Else
self.close
Exit Sub
End If
End Sub
</script>
<style>
</style>
</head>
<body>
<audio id="mp3">
<source src="./LZ.mp3" type='audio/mp4'>
</audio>
</body>
</html>

How to click on the button after data paste

I am trying to click on the button after I have pasted a value on a text box. However none of the code I tried seems to work. On the same column there are 2 text boxes and few buttons. I managed to open up a new frame after clicked on the 1st button with the below code:
Set the_button_elements = doc.getElementsByTagName("div")
For Each button_element In the_button_elements
If button_element.getAttribute("class") = "pzbtn-mid" Then
button_element.Click
Exit For
End If
Next button_element
I have tried by changing the tag name and attribution and also the below code but it still doesn't work:
doc.querySelectorAll("[type=button]").item(3).Click
Below is the element for the 1st button which work with the above code:
<button type="button" class="pzhc" id="AcctNumber"
disabled="" onclick="this.disabled=true;javascript: LoadAcct();"
title="Search">
<div class="pzbtn-lft">
<div class="pzbtn-rgt">
<div class="pzbtn-mid" data-click="...">
<img class="pzbtn-i">Go</div></div></div></button>
Below is the element for the next button which I am trying to find the code to make it work:
<button type="button" class="pzhc" id="SearchBtn"
disabled="true"onclick="getCaseDetails();">
<div class="pzbtn-lft">
<div class="pzbtn-rgt">`enter code here`
<div class="pzbtn-mid">
<img class="pzbtn-i">Search</div></div></div></button>
Appreciate someone could guide me by providing me the code that click on the button as the button is still grey out even after the text pasted. However this is the same situation for the 1st code but it still works.
Perhaps the issue relates to the disabled attribute. If we want to click the button, we should make sure it is enabled status. After clicking the first button or in the textbox change event, you could use the following JavaScript code to enable the Search button:
document.getElementById("SearchBtn").disabled = false;
You could check the following sample:
<button type="button" class="pzhc" id="AcctNumber" onclick='this.disabled=true;javascript: alert("Go"); document.getElementById("SearchBtn").disabled = false;'
title="Search">
<div class="pzbtn-lft">
<div class="pzbtn-rgt">
<div class="pzbtn-mid" data-click="...">
<img class="pzbtn-i">Go
</div>
</div>
</div>
</button>
<button type="button" class="pzhc" id="SearchBtn"
disabled="true" onclick='javascript: alert("Search");'>
<div class="pzbtn-lft">
<div class="pzbtn-rgt">
`enter code here`
<div class="pzbtn-mid">
<img class="pzbtn-i">Search
</div>
</div>
</div>
</button>
Then, if I use the following VBA script click the button, it will click the Go button, then, enable the search button and click it:
Sub login()
Const Url$ = "https://dillion132.github.io/vbatestpage.html"
Dim ie As Object
Set ie = CreateObject("InternetExplorer.Application")
With ie
.navigate Url
ieBusy ie
.Visible = True
'Find the related button
Dim button_go_element As Object, button_search As Object
Set button_go_element = .document.getElementById("AcctNumber")
Set button_search = .document.getElementById("SearchBtn")
button_go_element.Click
button_search.Click
End With
End Sub
Sub ieBusy(ie As Object)
Do While ie.Busy Or ie.readyState < 4
DoEvents
Loop
End Sub

VBA question: Cannot get html element after clicked to another page

I wish to make a macro which can auto login a page and then autofill the search input field and finally web scraping the search result.
I have been struggling for a long time that I can successfully login the page. However, when I click the login button, I cannot further get the input tag for the search field as well as adding the value to that input field. The index of those input tags are absolutely correct.
Below is my vba script, many thanks!
Sub login_and_search()
Set ie = CreateObject("InternetExplorer.Application")
With ie
.Visible = True
.Navigate "http://loginpage.html"
Do While .Busy Or _
.readyState <> 4
DoEvents
Loop
Set inputElement = .document.getELementsByTagName("input")
inputElement.Item(0).Value = "abc" 'username
inputElement.Item(1).Value = "xyz" 'password
inputElement.Item(2).Click
Do While ie.Busy Or _
.readyState <> 4
DoEvents
Loop
Set searchField = .document.getELementsByTagName("input")
searchField.Item(0).Value = "searchitems" 'search field
searchField.Item(1).Click
End With
End Sub
Below are the 2 html files, sorry for the too simplified html files as I roughly make them for testing the vba script only. The first one is the login page and the second one is the page after login.
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<form class="" action="search.html" method="post">
<input type="text" name="" value="">
<input type="password" name="" value="">
<input type="submit" name="" value="login">
</form>
</body>
</html>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<form class="" action="searchResults.html" method="post">
<input type="text" name="" value="">
<input type="submit" name="" value="Search">
</form>
</body>
</html>
There is no error showing when running the macro, I think it is a logic error instead of syntax error.
Once again, thanks all!

Read straight web content with Excel VBA

there are many article on this site on how to read tags and tables in web sites with Excel VBA, but I am stuck here.
This website gives me business locations after entering a Zip code.
("Where is the closest location relative to my Zip Code")
I managed to navigate to the site, enter the Zip code and click Submit:
Dim Browser As SHDocVw.InternetExplorer
Dim HTMLDoc As MSHTML.HTMLDocument
Set Browser = New SHDocVw.InternetExplorer ' create a browser
Browser.Visible = True ' make it visible
Application.StatusBar = ".... opening page"
Browser.navigate "https://www.thewebsite.com" ' navigate to page
WaitForBrowser Browser, 1 ' wait for completion or timeout
Application.StatusBar = "gaining control over DOM object"
Set HTMLDoc = Browser.document ' load the DOM object
WaitForBrowser Browser, 1
HTMLDoc.getElementById("ZipCode").Value = "28278"
HTMLDoc.getElementById("localTeamZipSubmit").Click
The site opens and the relevant content looks like this:
<div>
<div class="columns">
<div class="column boldText paddingFive" style="padding-left: 20px; width: 70px;">
Location:
</div>
<div class="column paddingTopFive">CHARLOTTE</div>
</div>
<div class="columns">
<div class="column boldText paddingFive" style="padding-left: 20px; width: 120px;">
Location Number:
</div>
<div class="column paddingTopFive">102340</div>
</div>
<div class="columns">
<div class="column boldText paddingTopFive paddingLeftTwenty" style="vertical-align: top;">
Address:
</div>
<div class="column paddingTopFive paddingLeftTwenty">
<div>8848 Main St.</div>
<div>Suite F</div>
<div></div>
<div>Charlotte, NC 27218</div>
</div>
</div>
<div class="columns">
<div class="column boldText paddingFive" style="padding-left: 20px; width: 70px;">
Phone:
</div>
<div class="column paddingTopFive">(704) 911-4440</div>
</div>
<div class="columns">
<div class="column boldText paddingFive" style="padding-left: 20px; width: 70px;">
Fax:
</div>
<div class="column paddingTopFive">(704) 911-4441</div>
</div>
</div>
As you can see, this section has no table, no named tags and classes that are use over and over.
I was not able to read this information yet. I would be happy to get the whole blob into a String and parse it"
"Text = HTMLDoc.getEverything()"
Thanks a lot for your help!!!
In the meantime I found another code snippet that I modified but I am getting stuck at the same point:
Post and submit works but how to get the answer....
{ Private Sub PostalCodes()
Dim ie As Object
Set ie = CreateObject("InternetExplorer.Application")
On Error GoTo errHandler
ie.Visible = 1
With ie
.navigate "https://www.pattersondental.com/ContactUs/MyLocalTeam"
Do While .busy: DoEvents: Loop
Do While .ReadyState <> 4: DoEvents: Loop
With .document.Forms("GetBranchFromZipForm")
.ZipCode.Value = "28273"
.submit
End With
' Do While Not CBool(InStrB(1, .document.URL, _
' "cp_search_response-e.asp"))
' DoEvents
' Loop
Do While .busy: DoEvents: Loop
Do While .ReadyState <> 4: DoEvents: Loop
' MsgBox .document.all.tags("Colums").Item(1).Rows(1).Cells(1).innerText
MsgBox .document.all.tags("Colums").innerText
' MsgBox .document}
I guess I have to search no for "how to dissect a HTML document"...
Add on:
It seems that while ie is a valid item (in the watch window) IE.Document is empty... why can this be, The website is still there with new data.
I even tried another code snippet that looks for open websites in IE, it finds the site (with the correct data) but the document is still empty and getelementBY... does not find anything of course.
I am about to start drinking...
I can't believe it.
After 3 days of poking I found this:
With ActiveSheet.QueryTables.Add(Connection:="URL;
https://www.pattersondental.com/ContactUs/MyLocalTeam",
Destination:=Range("A1"))
.PostText = "ZipCode=70032"
.RefreshStyle = xlOverwriteCells
.SaveData = True
.Refresh
I don't pretend to understand why it works, but is does.
John, I will still check out, what you suggested. Thanks

HTML Drop down list onchange

I have a small DropDownList with 3 options .
Problem is when I select , let's say , 2nd option .
No probs , a new page is opened .
If I click on option 1 or 3 , no probs .
However , if I click on 2nd option again , instead of clicking on 1 or 2 , no new page is opened .
Is there a way to correct this , so that clicking on whatever always opens a new window .
Thanks...Vern
SORRY I COULDN'T GET THE "CODE" OR "HTML" TO WORK .
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>myFavs-HtmlProbs.html</title>
<meta name="Generator" content="PureBasic">
<meta name="Description" content="Your description here...">
<meta name="Description" content="...Created by myFavs % PureBasic...">
<style type="text/css">
</style>
</head>
<body text="#000000" style="background-color:#A69E80; text-align:center;">
<br> <br> <br>
<!--dd <div style=-->
<table width="100%" border="0" style="text-align:center" cellspacing="0" cellpadding="0">
<tr style="text-align:center">
<!-- EXAMPLE: <MenuName="aaMost-Used"> --> <!-- rgb(250,240,255) -->
<select style="width:200px; font:14px Arial Black; color:rgb(0,0,0); background-color:rgb(231,169,126);" name="menu" onchange="window.open(this.value)">
<option selected="0" value="">aaMost-Used</option>
<option value="http://alternativeto.net/">AlternativeToSoftwares</option>
<option value="https://www.biblegateway.com/reading-plans/chronological/today?version=NLT">One Year Chronological Bible NLT</option>
<option value="http://www.portablefreeware.com/">portablefreeware.com/</option>
</select></td></tr></table><br></body></html>
Just add this attribute to your select tag
onfocus="this.selectedIndex=0;"
It will change the option to the default option each time after you select one, in your case
aaMost-Used and hence you can then select your required next option again.
When you click on an option more than once, it does not open the page because the option is already selected, and no change occurs. Therefore, onchange isn't fired.
You can actually open a link twice by opening that link, opening another (deselects first) and then clicking on the first link again.
But here's the fix.
Change onchange="window.open(this.value)" to onclick="window.open(this.value)" (onchange to onclick).
That way, the link will be opened whenever an option is clicked, rather than when it changes.
JSFiddle: https://jsfiddle.net/SanPilot/mqqh5u73/1/

Resources