Windows Shell Run Method from OpenInsight

Here is a useful code example showing how to call the Windows shell run method to open any document that is registered with an application. If the file isn't registered whit an application Windows will prompt to select the application using the same dialog that would normally appear when you double click an unrecognized file.

Function CS_LAUNCH_FILE_SNIPPET(void)

*Common to hold our control between runs so it isn't re-initialized on each call
common /wscript_launch_control/ wsLauncher, wsLauncherAssigned

If Assigned(wsLauncherAssigned) Else wsLauncherAssigned = ''
	
If wsLauncherAssigned Else
	*Control not initialized, do it now
	
	*VBScript to run
	sc = ''
	sc<-1> = 'sub launch(File)'
	sc<-1> = 'CreateObject("WScript.Shell").Run(File)'
	sc<-1> = 'end sub'
	
	*Setup the control
	wsLauncher = OLECreateInstance("MSScriptControl.ScriptControl")
	wsLauncher->Language = "VBScript"
	Swap @FM With \0D0A\ In sc
	
	*Add the VBScript to the control
	x = wsLauncher->AddCode(sc)
	sc = ''
	
	wsLauncherAssigned = 1 ;*Mark the launcher has been setup
End 

*Path of the file to open
FilePath = "C:\temp\test.csv"

*Run the control's VBScript to launch the file.
*Protect the FilePath in quotes in case of spaces in path
x =  wsLauncher->Run('launch', '"' : FilePath : '"')

Return

It also demonstrates how to initialize and re-use a script object using a named common. This isn't required for simple examples but for repeated usage it's best practice to re-use the script control. If this technique interests you more examples are available

Share this post

Comments (1)

  • anon

    Jared,

    I have used a number of the snippets that you have posted and they work great! Thanks very much for the effort and for sharing. 

    Jul 29, 2016

Leave a comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.

About Us

Congruity Service is a technology solutions company bringing the best technology solutions to OpenInsight projects, Drupal sites, servers, networks, and your technology needs.