Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Show a popup/message box from txt file content using mshta

Status
Not open for further replies.

amein

Technical User
Jul 11, 2019
4
MY
Using example below, i want my code that will open a msgbox that contain string from txt file.

Code:
Option Explicit
Dim wsh : Set wsh = CreateObject("Wscript.Shell")

'Message boxes that don't wait for a return to continue. No return values.
MsgBlank "tetttedghdhbcccc", "title"

'Functions for simple no wait message boxes without return values.
Function MsgBlank(m, t)
    wsh.Run "mshta.exe vbscript:Execute(MsgBox(""" & m _
        & """, vbOkOnly, """ & t & """)(window.close))"
End Function

I try something like this, not working. Someone can help fixing it?

Code:
Option Explicit
Dim wsh : Set wsh = CreateObject("Wscript.Shell")

'Message boxes that don't wait for a return to continue. No return values.
Dim fso
Dim file
Dim content
Set fso = CreateObject("Scripting.FileSystemObject")
Set file = fso.OpenTextFile ("c:\List.txt", 1)
content = file.ReadAll
file.Close
MsgInformation content, "title"
'Functions for simple no wait message boxes without return values.

Function MsgInformation(m, t)
    wsh.Run "mshta.exe vbscript:Execute(MsgBox("""&m&""",vbInformation,"""&t&""")(window.close))"
End Function
 
You don't need to use mshta to pop up a message box. You can do it directly in vbscript

Code:
dim fso, file, content
set fso = CreateObject("Scripting.FileSystemObject")
set file = fso.OpenTextFile(".\List.txt", 1)
content = file.ReadAll
file.close
MsgBox content, vbInformation or vbOKOnly, "Readall"
 
> You can do it directly in vbscript

I think the idea of the OPs code is to throw up a messagebox on a separate thread so it doesn't block - as messageboxes are modal. The mshta technique works for that.

And the reason it isn't working is

[tt]&m&[/tt] and [tt]&t&[/tt]

should really be

[tt]& m &[/tt] and [tt]& m &[/tt]
 
> You don't need to use mshta to pop up a message box. You can do it directly in vbscript

as per @strongm explained..

> & m & and & m &

I try this from my side. Nothing come out too.. Im using windows 10. Any setting should I look for?

But when I try run using templates code, its working.

2CRh44Q_1_i4ueor.gif
 
Afraid I can't help - it works fine here on my W10 box when launching the same way as your GIF shows. Can't replicate your problem.
 
Hi amein,
First of all try at the command line how you create the message box using MSHTA.
This works
Code:
mshta.exe vbscript:Execute("MsgBox ""tetttedghdhbcccc"", vbOkOnly, ""title""")(window.close)
mshta_fdfbbg.jpg
 
Here is the procedure:
Code:
[COLOR=#804040][b]dim[/b][/color] wsh
[COLOR=#804040][b]set[/b][/color] wsh [COLOR=#804040][b]=[/b][/color] [COLOR=#008080]CreateObject[/color][COLOR=#804040][b]([/b][/color][COLOR=#ff00ff]"Wscript.Shell"[/color][COLOR=#804040][b])[/b][/color]

MsgBlank_01 [COLOR=#ff00ff]"foo"[/color][COLOR=#804040][b],[/b][/color] [COLOR=#ff00ff]"message 01"[/color]
MsgBlank_02 [COLOR=#ff00ff]"bar"[/color][COLOR=#804040][b],[/b][/color] [COLOR=#ff00ff]"message 02"[/color]


[COLOR=#804040][b]Function[/b][/color] MsgBlank_01[COLOR=#804040][b]([/b][/color]m[COLOR=#804040][b],[/b][/color] t[COLOR=#804040][b])[/b][/color]
 [COLOR=#0000ff] 'create command string[/color]
  my_command [COLOR=#804040][b]=[/b][/color] [COLOR=#ff00ff]"mshta.exe vbscript:Execute(""MsgBox """""[/color] [COLOR=#804040][b]&[/b][/color] m _ 
     [COLOR=#804040][b]&[/b][/color] [COLOR=#ff00ff]""""", vbOkOnly, """""[/color] [COLOR=#804040][b]&[/b][/color] t [COLOR=#804040][b]&[/b][/color] [COLOR=#ff00ff]""""""")(window.close)"[/color]
  wscript[COLOR=#804040][b].[/b][/color]echo [COLOR=#ff00ff]"my_command = '"[/color] [COLOR=#804040][b]&[/b][/color] my_command [COLOR=#804040][b]&[/b][/color] [COLOR=#ff00ff]"'"[/color]
 [COLOR=#0000ff] ' execute command string[/color]
  wsh[COLOR=#804040][b].[/b][/color]run my_command  
[COLOR=#804040][b]End[/b][/color] [COLOR=#804040][b]Function[/b][/color] 

[COLOR=#804040][b]function[/b][/color] MsgBlank_02[COLOR=#804040][b]([/b][/color]m[COLOR=#804040][b],[/b][/color] t[COLOR=#804040][b])[/b][/color]
  apost [COLOR=#804040][b]=[/b][/color] [COLOR=#ff00ff]""""[/color]
 [COLOR=#0000ff] ' double apostrophe input strings[/color]
  m [COLOR=#804040][b]=[/b][/color] apost [COLOR=#804040][b]&[/b][/color] apost [COLOR=#804040][b]&[/b][/color] m [COLOR=#804040][b]&[/b][/color] apost [COLOR=#804040][b]&[/b][/color] apost
  t [COLOR=#804040][b]=[/b][/color] apost [COLOR=#804040][b]&[/b][/color] apost [COLOR=#804040][b]&[/b][/color] t [COLOR=#804040][b]&[/b][/color] apost [COLOR=#804040][b]&[/b][/color] apost
 [COLOR=#0000ff] 'create command string[/color]
  my_command [COLOR=#804040][b]=[/b][/color] [COLOR=#ff00ff]"mshta.exe vbscript:Execute(""MsgBox "[/color] [COLOR=#804040][b]&[/b][/color] m[COLOR=#804040][b] _[/b][/color]
     [COLOR=#804040][b]&[/b][/color] [COLOR=#ff00ff]", VbOkOnly, "[/color] [COLOR=#804040][b]&[/b][/color] t [COLOR=#804040][b]&[/b][/color] [COLOR=#ff00ff]""")(window.close)"[/color]
  wscript[COLOR=#804040][b].[/b][/color]echo [COLOR=#ff00ff]"my_command = '"[/color] [COLOR=#804040][b]&[/b][/color] my_command [COLOR=#804040][b]&[/b][/color] [COLOR=#ff00ff]"'"[/color]

 [COLOR=#0000ff] ' execute command string[/color]
  wsh[COLOR=#804040][b].[/b][/color]run my_command
[COLOR=#804040][b]end[/b][/color] [COLOR=#804040][b]function[/b][/color]

MsgBlank_01 contains too much apostrophes in one string, so I would prefer MsgBlank_02

amein_cieytk.jpg
 
Hi microm,

Code below worked at my pc

Code:
mshta.exe vbscript:Execute("MsgBox ""tetttedghdhbcccc"", vbOkOnly, ""title""")(window.close)

KmKLp9x_1_mq61td.png


and the second code working too... Possible to use that to read string from text file then view at the msg box?
 
Hi amein ,
First I looked at your function MsgBlank(m, t). It didn't work for me, so I posted the answer.
Now I'm trying your second function MsgInformation(m, t) but this works for me on windows10 well.

amein_1_zpervt.jpg
 
Hi Microm,

Thank you for your reply. Seems like something wrong with my PC then because im using win10 too..
 
Hi amein,
try it to run at command line like me, maybe you see any error message.
Maybe you could have some problem with permision ... Are you admin on your PC? For example on my W10 i had no permision to copy the file list.txt and the VbScript file to the root directory of the drive c: (it worked only when I done it as administrator).
 
>to the root directory of the drive c:

Users really are not supposed to write to tbe root of the C drive
 
strongm (MIS)
>to the root directory of the drive c:

Users really are not supposed to write to tbe root of the C drive
Usually I never do that either.
But I wanted to try the script of amein and here there is the text file in the root directory
Code:
...
Set file = fso.OpenTextFile ("c:\List.txt", 1)
...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top