How to play WAV file using VBScript in HTML - audio

How to play WAV file using VBScript in HTML?
I have got following code but it is not working.
<script type="text/vbscript" language="VBScript">
Dim strSoundFile, strCommand
strSoundFile = "C:\Sounds\Sound1.wav"
Set objShell = CreateObject("Wscript.Shell")
strCommand = "sndrec32 /play /close " & chr(34) & strSoundFile & chr(34)
objShell.Run strCommand, 0, True
</script>
I found another code but it doesnt work as well if I use it in HTML page but it is working great in a *.VBS.
Sub Play(SoundFile)
Dim Sound
Set Sound = CreateObject("WMPlayer.OCX")
Sound.URL = SoundFile
Sound.settings.volume = 100
Sound.Controls.play
do while Sound.currentmedia.duration = 0
wscript.sleep 100
loop
wscript.sleep(int(Sound.currentmedia.duration)+1)*1000
End Sub
I have found this link https://support.microsoft.com/es-es/kb/279022
but I am not shure if it is a correct way...
The way is working fine via in BODY tag is following
<object classid="clsid:6BF52A52-394A-11D3-B153-00C04F79FAA6" id="WindowsMediaPlayer"
width="242" height="202" style="position:absolute; left:1;top:1;">
<param name="URL" value="C:\Sound1.wav">
<param name="autoStart" value="1">
</object>
Could it be done using this?
<script type="text/vbscript" language="VBScript"> HERE </script>
I am using IE8 under MS Windows 7 Pro.

Finally I could find correct approach to do what I need is described here
Basically the idea is following
We cannot use code above in the HTML instead we have to use embedded Windows Media Player object.
We can create any methods we need to manipulate that object in VBScript.

Related

Using Classic ASP to bring in spreadsheet content including emojis

I need to write a small script to take the contents of an XLS file and lay them out nicely in a web page. So far, so easy. However, the spreadsheet text will be the content of a series of social media posts which includes emojis within that content. I did a very basic test and found that the emojis turn into question marks when dropped to the page by the script.
I don't particularly like emojis myself but I don't get to choose whether they are included in the text, and they need to appear in the end result.
Here's what a stripped back version of the script looks like, with no formatting etc.
<%# Language=VBScript%>
<html><head></head>
<body>
<%
Dim objConn, objRS
Set objConn = Server.CreateObject("ADODB.Connection")
filepath = "xls/OPG.xls"
objConn.Open "DRIVER={Microsoft Excel Driver (*.xls)}; IMEX=1; HDR=NO; Excel 8.0; DBQ=" & Server.MapPath(filepath) & "; "
strSQL = "SELECT * FROM A1:C400"
Set objRS=objConn.Execute(strSQL)
Set DataList = CreateObject("ADOR.Recordset")
Do Until objRS.EOF
Response.Write objRS.Fields("Post Description").Value & "<br /><br />"
objRS.MoveNext
Loop
objConn.Close
Set objConn=Nothing
%>
</body>
</html>
Here's what the original XLS file looks like...
...and here's how that comes into the web page...
How do I get around this? Any ideas?
Thanks
The Microsoft Access Database Engine is able to read and display emojis from Excel files:
https://www.microsoft.com/en-us/download/details.aspx?id=54920
You will need to set your codepage and charset to UTF-8 though:
<%#LANGUAGE="VBSCRIPT" CODEPAGE="65001"%>
<%
response.Charset = "utf-8"
%>
Connection string:
objConn.Open "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & Server.MapPath(filepath) & ";Extended Properties=""Excel 12.0 Xml;HDR=YES;IMEX=1"";"
Tested and working.

How to retrieve specific information from XML type document

I`m working on a VBA macro in Excel, to gather information from a CNC program code.
So far, I have gotten Material type, thickness, x & Y sizes, and qty used.
I`m trying to get the 'cutting length' now - so I can use it in costing calculations.
Here is the XML code segment :
<Info num="6" name="Tools">
<MC machine="psys_ETN_5">
<Tool name="TN901" length="16262.96209" time="53.72817301" cutoutArea="8138.657052"/>
</MC>
</Info>
There are lots of 'Info' lines.
There may be more than one 'Tool' line, but I`m only after anything from line with 'TN901'.
The data I`m trying to capture is the value of 'Length="######.##"'
I`ve captured everything else I need from code like this :
<Material>316</Material>
<SheetX>2000</SheetX>
<SheetY>1000</SheetY>
<Thickness>3</Thickness>
</Material>
using code like this:
For Each nodemat In XMLDataDrg.SelectNodes("//Material")
Matl = nodemat.Text
Worksheets("Sheet4").Range("H" & RowA).Value = Matl
Next
For Each nodesht In XMLDataDrg.SelectNodes("//Thickness")
Thk = nodesht.Text
Worksheets("Sheet4").Range("I" & RowA).Value = Thk
Next
But that type of code does not get the cutting length.
Any help please ? :)
Thanks
Simon
Thickness is saved as XML element in your example.
The length is stored as an XML attribute.
(see https://www.xmlfiles.com/xml/xml-attributes/)
To read an XML attribute please have a look at:
Read XML Attribute VBA
Based on the code presented there you should be able to solve your issue with:
'Include a reference to Microsoft XML v3
Dim XMLDataDrg As DOMDocument30
Set XMLDataDrg = New DOMDocument30
XMLDataDrg.Load ("C:\...\sample.xml")
'...
Dim id As String
id = XMLDataDrg.SelectSingleNode("//Info/MC/Tool").Attributes.getNamedItem("length").Text
You can use an xpath to restrict to Tool elements with attribute name having value TN901 then loop all the attributes and write out. I am reading your XML from a file on desktop.
Option Explicit
Public Sub test()
Dim xmlDoc As Object
Set xmlDoc = CreateObject("MSXML2.DOMDocument")
With xmlDoc
.validateOnParse = True
.setProperty "SelectionLanguage", "XPath"
.async = False
If Not .Load("C:\Users\User\Desktop\Test.xml") Then
Err.Raise .parseError.ErrorCode, , .parseError.reason
End If
End With
Dim elem As Object, attrib As Object
For Each elem In xmlDoc.SelectNodes("//Tool[#name='TN901']")
For Each attrib In elem.Attributes
Debug.Print attrib.nodeName, attrib.Text
Next
Next
End Sub
Result:

Play sound file from within VBScript when msgbox appears

I am trying to play a sound file from within a VBScript when a certain msgbox appears. The only problem is that I will be sending this elsewhere and the person who receives it won't have the same pathname as the audio file that I want to play. I was thinking about putting all of the sound files that I want to use in the same folder as the script and then sending that folder, but I don't know how to make sure the sound file will play.
So I guess the biggest question is how to generalize the pathname so that anyone can hear the file from within the script from any machine.
Here is my code so far:
if intAnswer3 = vbyes then
strSoundFile = "C:\pathname"
Set objShell = CreateObject("Wscript.Shell")
strCommand = "sndrec32 /play /close " & chr(34) & strSoundFile & chr(34)
objShell.Run strCommand, 0, True
Assume that you have a folder named Music with your script, so you can use a relative path like this ./Music/Matrix.mp3
So you can give a try like this :
Option Explicit
Dim Msg,Question,PathSound
Msg = "Did you want to hear some music ?"
PathSound = "./Music/Matrix.mp3" 'Relative Path
Question = MsgBox(Msg,VbQuestion+VbYesNo,Msg)
If Question = VbYes Then
Call Play(PathSound)
Else
Wscript.Quit()
End If
'**********************************************************
Sub Play(SoundFile)
Dim Sound
Set Sound = CreateObject("WMPlayer.OCX")
Sound.URL = SoundFile
Sound.settings.volume = 100
Sound.Controls.play
do while Sound.currentmedia.duration = 0
wscript.sleep 100
loop
wscript.sleep(int(Sound.currentmedia.duration)+1)*1000
End Sub
'**********************************************************
And if you like to play the music online, so you can do it like this :
Option Explicit
Dim Msg,Question,PathSound
Msg = "Did you want to hear some music ?"
PathSound = "http://hackoo.alwaysdata.net/Matrix.mp3"
Question = MsgBox(Msg,VbQuestion+VbYesNo,Msg)
If Question = VbYes Then
Call Play(PathSound)
Else
Wscript.Quit()
End If
'**********************************************************
Sub Play(SoundFile)
Dim Sound
Set Sound = CreateObject("WMPlayer.OCX")
Sound.URL = SoundFile
Sound.settings.volume = 100
Sound.Controls.play
do while Sound.currentmedia.duration = 0
wscript.sleep 100
loop
wscript.sleep(int(Sound.currentmedia.duration)+1)*1000
End Sub
'**********************************************************
I hope that this answer can help you to complete your main script ;)
Did you mean something like that :
Description :
This Vbscript "PlayListSongs.vbs" scan into a folder and its subfolders for songs and create a playlist in a text file in order to play it in background.
Update : I add another vbscript to stop and kill the "wscript.exe" process in order to stop music playing in background.
I add another vbscript to play the playlist with Windows Media Player in foreground.
in sum you can found into zip 3 vbscript
1- To play the playlist in the background.
2- To Stop the music.
3- To Play the music with Windows Media Player in foreground.
So you can download it from here and test it

ASP FileObject.Name converting to '?'

I have a script that lists all files in a directory, then for each one it will Response.Write the name and how many downloads it has.
I have everything completed, but when I went for a test, the files that have "odd" characters in the name are replace with a ?
I'm guessing, that since some files have foreign languages as there name, and that some have the iPhone emoji icons in the name, that it doesn't recognize it and puts a ? instead, but this is a serious issue since I can't give the correct file name back to the user, then that incorrect name is fed back into the url to download. (Which doesn't work)
Any suggestions?
Edit:
set fs=Server.CreateObject("Scripting.FileSystemObject")
set fo=fs.GetFolder(Server.MapPath("."))
for each file in fo.files
if fs.GetExtensionName(file.Path) = "plist" then
dim tempList, tempName, ...
tempList = split(file.Name, ".")
'Manipulate name and data ...
Response.write(name)
end if
next
The file names themselves have odd characters, and file.Name returns a ? instead of what is actually there.
18アイコン is one example.
Here's some code which works fine for me:
<%# Language="VBScript" CodePage="65001" %><%
Option Explicit
Response.CodePage = 65001
Response.CharSet = "utf-8"
Dim fs, fo, file
Set fs = Server.CreateObject("Scripting.FileSystemObject")
Set fo = fs.GetFolder(Server.MapPath("."))
For Each file In fo.files
If fs.GetExtensionName(file.Path) = "plist" Then
' Do whatever here...
Response.Write file.Name & "<br>"
End If
Next
%>
If you are using any variables that you didn't dimension beforehand, you'll need to remove the Option Explicit; otherwise, VBScript will complain that you didn't dimension them.
Edit: I copy & pasted the wrong code; this code works.

How could I refer to built-in MS Excel type with OLE

my application should perform some simple actions in Excel, like adding charts, list objects and so on. I'm using OLE connection. The problem is, that some Excel methods taking built-in types (enumerations) as arguments. And I have no ideas about referring to them. For example:
WorkBook.ActiveSheet.ListObjects.Add(xlSrcRange, Range("$D$5:$J$15"), , xlNo).Name = "Table1"
xlSrcRange and xlNo belong to the built-in enumeration. I tried to refer to them in a following way
ExcelApp.xlSrcRange
ExcelApp.XlListObjectSourceType.xlSrcRange
ExcelApp.XlListObjectSourceType
this code causes error "Object doesn't support property or method ExcelApp.xlSrcRange"
New XlListObjectSourceType.xlSrcRange
new xlSrcRange
this code causes an error too (unknown variable XlListObjectSourceType and xlSrcRange)
I'm working with QTP and the script language is VB-script
A .wsf script can access the xl* constants via a Excel.Sheet reference:
type xlconst.wsf
<?xml version="1.0" standalone="yes" encoding="iso-8859-1" ?>
<package>
<job id="xlconst">
<reference object="Excel.Sheet" reference="true"/>
<script language="VBScript">
<![CDATA[
' ############################################################################
For Each arg In WScript.Arguments.Unnamed
WScript.Echo "Const " & arg & " = " & Eval(arg)
Next
' ############################################################################
]]>
</script>
</job>
</package>
output:
cscript xlconst.wsf xlNo xlYes
Const xlNo = 2
Const xlYes = 1
Plain VBScript can't. If QTP is restricted to plain VBScript, you'll have to add/define the constants manually. Perhaps the above .wsf will make this task easier.
How about using fully qualified name?
dim sourceType As Excel.XlListObjectSourceType
sourceType = Excel.XlListObjectSourceType.xlSrcRange
EDIT: (in vbscript)
dim sourceType
sourceType = 1

Resources