VbScript hta - Open a new hta by link and retrieve correct filename - filenames

I have two files; "1.hta" and "2.hta".
"1.hta" contains a simple link to file "2.hta"
2.hta
"2.hta" contains a script to determine its own filename
FullName = replace(oApp.commandLine,chr(34),"") 'oApp = HT Application ID
arrFN=split(FullName,"\")
FileName = arrFN(ubound(arrFN))
SourceDir=replace(FullName,FileName,"")
"2.hta" works perfectly when started "stand-alone" --> FileName = 2.hta
However, starting "2.hta" via link from "1.hta" I get --> FileName = 1.hta
I need a way to determine the correct filename, or does hta always retrieve the filename of the first/starting instance?

You can try like this :
<html>
<head>
<title>HTA Launch another HTA</title>
<HTA:APPLICATION
SINGLEINSTANCE="yes"
WINDOWSTATE="maximize"
>
</head>
<SCRIPT Language="vbscript">
Sub Execute(File)
Dim ws
Set ws = CreateObject("wscript.shell")
ws.run chr(34) & File & chr(34)
End sub
</SCRIPT>
<body>
<h1>This is test hta 1 ONE</h1>
Start the HTA2
</body>
</html>

Related

VB.Net Hyperlinking in EWS email with class properties

I need help getting the hyperlink to work as well as getting the value stored in YestFile.FileName to show up.
I have a list of objects of class FileFromYesterday (Collection2) with 3 properties. Two properties are just Strings (FileName and Entity) but one property (URL) contains a URL as a String. I am trying to hyperlink the URL to FileName in the main.
I have a feeling that I need to use HTML to format the email somehow but I have no Idea where to begin
Dim HyperLink As String
Dim FileLine As String
Dim NewBody As String
Dim message As New EmailMessage(EWS)
message.Subject = "Monthly Financial Imported into Docushare on " & String.Format("{0:MM-dd-yyyy}", Yesterday)
NewBody = "Total of " & Collection2.Count & " Files Imported." & "\n"
For Each YestFile In Collection2
HyperLink = YestFile.FileName.Replace(".pdf", "")
FileLine = HyperLink & "Entity: " & YestFile.Entity & Environment.NewLine
NewBody = NewBody & FileLine
Next
You should be creating a HTML document in your NewBody varible so you need the pre and post tags for a HTML document eg
<html>
<body>
</body>
</html>
the body content you want should go within between the Body Tags and for thing like NewLines you need to use the appropriate HTML tags such as
<br />
Eg what ever you end up creating in you NewBody variable you should be able to write out to a file or test in any Online HTML validator https://validator.w3.org/#validate_by_input to see if its valid and how it will look before trying to send it.

Today's Currency Exchange Rate in VBA

I am trying to convert a value to NOK(USD to NOK for instance), but I have trouble getting the whole html page to load.
I am using the code found here , but the the html i get in return display this
"<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html lang="en-US">
<head>
<title>USDNOK=X: Summary for USD/NOK- Yahoo! Finance</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"><met
So it is basically stripping the webpage, or rather is not waiting untill its done loading. The code in the link for getting webpage is this:
Function ExecuteWebRequest(ByVal url As String) As String
Dim oXHTTP As Object
If InStr(1, url, "?", 1) <> 0 Then
url = url & "&cb=" & Timer() * 100
Else
url = url & "?cb=" & Timer() * 100
End If
Set oXHTTP = CreateObject("MSXML2.XMLHTTP")
oXHTTP.Open "GET", url, False
oXHTTP.send
ExecuteWebRequest = oXHTTP.responseText
Set oXHTTP = Nothing
End Function
Maybe there is a better way of getting todays exchange rate in vba.
This code works superb, it uses Yahoo as well, but is up to date with html parsing.

Call PowerShell from VBScript with parameters

I have a very basic HTA form with a checkbox and a button. I am trying to pass the checkbox status using VBScript in my HTA to a PowerShell script, which is called when the button is clicked. Unfortunately, I am unable to pass the value of the parameter through. It keeps coming across as empty.
Code in HTA:
<html>
<head>
<title>Test Form</title>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
<hta:application applicationname="Proof of concept version="1.0" />
<script language="vbscript">
Sub Resize()
window.resizeTo 500,450
End Sub
Sub ExecutePowerShell()
Dim oShell, scriptPath, appCmd, retVal, retData, isTestCheckBoxChecked
'Collect value from input form
isTestCheckBoxChecked = document.getElementByID("input_checkBoxTest").checked
MsgBox isTestCheckBoxChecked
Set oShell = CreateObject("Wscript.Shell")
Set scriptPath = ".\TestPowershell.ps1 -isTestCheckBoxChecked " & isTestCheckBoxChecked
appCmd = "powershell.exe " & scriptPath
retVal = oShell.Run(appCmd, 1, true)
retData = document.parentwindow.clipboardData.GetData("text")
End Sub
</script>
</head>
<body onload="Resize()">
<h1>Test Form:</h1>
<div style="margin-top:10px; margin-bottom:30px;">
The scipt does the following checks:
<ul>
<li><input name="input_checkBoxTest" type="checkbox" checked="checked"/> This is a test textbox</li>
</ul>
</div>
<br /><br />
<input type="button" id="btn_execute" value="Execute" onclick="ExecutePowerShell()" />
<br /><br />
</body>
</html>
Powershell script:
#Param([Parameter(Mandatory=$true)][bool]$isTestCheckBoxChecked)
Write-host "The value is '$isTestCheckBoxChecked'"
The output I get is:
"The value is ''"
Any guidance will be appreciated.
Three things:
Don't use Set on the following statement. It's just a string, not an object, so using Set here should throw an error.
' Incorrect
Set scriptPath = ".\TestPowershell.ps1 -isTestCheckBoxChecked " & isTestCheckBoxChecked
' Correct
scriptPath = ".\TestPowershell.ps1 -isTestCheckBoxChecked " & isTestCheckBoxChecked
Your Param statement in PowerShell is commented out (#Param). Maybe this is just a typo when posting your question.
After you uncomment your Param statement, you'll get an error about converting from a string to a bool. PowerShell accepts booleans in the format $false/$true or 0/1 for False/True values, respectively. So, you have two options:
' Prefix the boolean with a '$'
scriptPath = ".\TestPowershell.ps1 -isTestCheckBoxChecked $" & isTestCheckBoxChecked
' Or, convert the boolean to a 0/1 (False/True)
scriptPath = ".\TestPowershell.ps1 -isTestCheckBoxChecked " & Abs(isTestCheckBoxChecked)

VB.NET Webbrowser How to display specified content I want

eg: the source code will be:
<html>
<head> balaba....
</head>
<body>
<div id="many_div">...</div>
<div id="main">
<div id="target">
.....balabala ...
</div>
</div>
</body></html>
Then, how to let my webbrowser only display the div with "target" id ?
Thanks!
You'll need to manipulate the HTML of the page.
I would use HtmlAgilityPack to extract the part you want and rewrite it to the same or another file:
Dim html = File.ReadAllText("c:\temp\htmlTest.htm")
Dim doc = New HtmlAgilityPack.HtmlDocument()
doc.LoadHtml(html)
Dim target = doc.GetElementbyId("target")
If target IsNot Nothing Then
Dim body = doc.DocumentNode.SelectSingleNode("//body")
body.RemoveAll()
body.PrependChild(target)
Using writer = File.OpenWrite("c:\temp\htmlTest2.htm")
doc.Save(writer)
End Using
End If
Now you just have to load this html in the WebBrowser.
If you want to get the HTML directly from internet/intranet:
Dim client As New HtmlAgilityPack.HtmlWeb()
Dim doc As HtmlAgilityPack.HtmlDocument = client.Load("http://yoururl.com")
' rest is the same as above(without doc.LoadHtml) '

How to click this button?

Hi all I am a beginner in Visual Basic. Your help is highly appreciated.
How can i click this button in a webpage?
<a class="buttonRed" href="what_would_you_do.html" onclick="this.blur();">
<span>Get Started</span>
</a>
Short example which worked for me, tested with simple html file:
ClickA.html:
<!DOCTYPE HTML>
<html lang="en">
<head>
<title><!-- Insert your title here --></title>
</head>
<body>
<a class="buttonRed" href="what_would_you_do.html" onclick="this.blur();">
<span>Get Started</span>
</a>
</body>
</html>
vba standard module:
' Add References:
' - Microsoft HTML Object Library
' - Microsoft Internet Controls
Sub test()
Dim Browser As InternetExplorer
Dim Document As htmlDocument
Set Browser = New InternetExplorer
Browser.Visible = True
Browser.navigate "C:\Temp\VBA\ClickA.html"
Do While Browser.Busy And Not Browser.readyState = READYSTATE_COMPLETE
DoEvents
Loop
Set Document = Browser.Document
Dim anchorElement As HTMLAnchorElement
Set anchorElement = Document.getElementsByClassName("buttonRed").Item(Index:=1)
anchorElement.Click
Set Document = Nothing
Set Browser = Nothing
End Sub
actually this is not a button. its anchor tag. write your code as
<a class="buttonRed" href="what_would_you_do.html">
<span>Get Started</span>
</a>
and it will redirect to "what_would_you_do.html page (if its in root)