Slang's Anti-Singularity
  • Slanghome
  • Slangblog
  • Slangpuzzles
    • CurrentPuzzles
    • ScholarPuzzle
    • PastPuzzles

VBA, ClickOnce Applications and Parameters - oh my!

5/18/2012

0 Comments

 
I'm a big fan of the KISS principle: "Keep It Simple Slang." I tried following the instructions for passing arguments to a clickonce application, and though I am not stupid, they seemed, I don't know, complex. I went for a much simpler route that works well in my environment.

Let me back up and give the situation. I have a simple clickonce application call AlphaWave that allows users to select from a couple of drop down lists, and enter some information into a text box and maybe add an image to provide feedback to me. Pretty simple stuff. What I wanted to do is to pre-populate one of the dropdowns based on the application that was calling the AlphaWave app so the user wouldn't have to. Again, pretty simple stuff. Or so I thought. Searching through the morass of blogs, MSDN articles, and other assorted goop, I found a couple of good articles that discuss it. The discussion was in-depth and complex. But I am lazy; I don't want to work that hard. So I bypassed all the passing parameter stuff in the usual way. What I did, is I create a text file with the parameters in it before calling AlphaWave through a Process.Start method in my Visual Studio 2010 VB application. In this particular case, the call was on a button click event. The WriteAllText line is what creates a text file and populates it with a string (the second parameter). In this particular case, the string "Sisyphus" indicating the application that is calling AlphaWave.

Const cAlphaFile As String = "C:\alphacall.txt"
Const cAlphaApp As String = "C:\ProgramData\Microsoft\Windows\Start Menu\LCB Applications\AlphaWave.appref-ms"
'What we are writing is the parameter string to be read later. In this case, it's a single line
My.Computer.FileSystem.WriteAllText(cAlphaFile, "Sisyphus", True)
Try
Process.Start(cAlphaApp)
Catch ex As Exception
MsgBox("Your application not found error message here.", MsgBoxStyle.Information)
End Try

The Process.Start line fires up the AlphaWave application itself, so from the perspective of the calling application, the job is done. A parameter has been "passed" by writing to the text file, and the AlphaWave application started.
In AlphaWave, I needed to set the startup object to be Sub Main and have that look for the text file with the parameters in it before displaying the form itself. If the parameter text file exists, I read its contents as the equivalent of a passed parameter, kill the file, then continue processing the AlphaWave form startup. If the text file doesn't exist, the code bypasses it all and I just don't have the parameters loaded into strParameters for launching AlphaWave. Simple and fast enough.


The additional code dealing with the cAppVersion is how I display the version number of the app. If it's deployed as a ClickOnce, it reads the data from the Deployment object. If it isn't, I am working in my own dev environment, and I display that.

Public strParamaters as string
Dim cAlphaCall as string = "C:\alphacall.txt"
Try
strParameters = My.Computer.FileSystem.ReadAllText( cAlphaCall )
Kill( cAlphaCall )
Catch ex As Exception
End Try
' Identify the version in the app
If Deployment.Application.ApplicationDeployment.IsNetworkDeployed Then
cAppVersion = "v. " & Deployment.Application.ApplicationDeployment.CurrentDeployment.CurrentVersion.ToString
Else
cAppVersion = "Development version"
End If
theForm.lblVersion.Text = cAppVersion
Application.Run(theForm)

Calling a ClickOnce Application from VBA
The second problem I had to solve was calling the app from inside VBA code. AlphaWave's raison d'être is to serve as a feedback process and – in addition to stand-alone applications – there is a lot of custom code in MS Word for which I need feedback.

The "Process.Start" method is not available in VBA, and the Shell command only works against .com, .bat or .exe files. A conundrum in that it appears there isn't a way to launch a clickOnce application from within a MS Office application. Why? A ClickOnce application is a completely different animal with an extension of appref.ms. Another search across the web pretty much resulted in nothing.

After thinking a bit, I cleverly (I think) came up with a clickOnce Application Launcher... application. It is a console app, taking as a passed argument the path to the clickOnce application I want to start. Since it can easily take in an argument, AND it can also launch appref.ms files, the problem was solved. Below is the ENTIRE contents of the clickLauncher application:

Module Module1
Sub Main()
Main(Environment.GetCommandLineArgs())
End Sub

Private Sub Main(ByVal args() As String)
If UBound(args) = 0 Then
MsgBox("This application is designed to be launched from VBA Code with an argument of a path to a clickonce application as follows:" & vbCrLf & vbCrLf & _
" RetVal = ShellExecute(0, ""open"", chr(34) & & chr(34), Chr(34) & & Chr(34), """", 0)" & vbCrLf & vbCrLf & _
"Double clicking it without passing an argument of the clickonce app path will do nothing other than get you this message box.")
Exit Sub

End If
Try
Process.Start(args(1))

Catch ex As Exception
MsgBox("The ClickOnce application that you are trying to launch (" & args(1).ToString & ") was not found in the expected location." & vbCrLf & _
"Please ensure it has been installed locally, and the shortcut properly located in the LCB Applications folder.", MsgBoxStyle.Information)

End Try
End Sub
End Module

Here is the VBA code that is used to call the app launcher to instantiate a ClickOnce app. You will need a reference to the ShellExecute function at the top of your module:

Private Declare Function ShellExecute Lib "Shell32.dll" Alias _
"ShellExecuteA" (ByVal hwnd As Long, ByVal lpOperation As String, _
ByVal lpFile As String, ByVal lpParameters As String, _
ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long

Sub VBACallToLaunchClickOnceApp()
Dim strLauncherPath as string
Dim strAppPath as string
Dim RetValue as Long
strLauncherPath = ""'this is the path to the clickOnceLauncher Application
strAppPath = ""'this is the path to the appref.ms shortcut you want to start up
RetVal = ShellExecute(0, ""open"", chr(34) & strLauncherPath & chr(34), Chr(34) & strAppPath & Chr(34), """", 0)
End Sub

That's it. I hope someone else can find the results of my investigations useful. Have a great weekend.
0 Comments



Leave a Reply.

    RSS Feed

    Author

    Just a guy out exploring the world. Former world-class never-was endurance runner.

    ​Hit me up, and we'll catch a beer or coffee in your town.


    Follow @slang4201

    Archives

    September 2021
    August 2021
    May 2021
    October 2020
    September 2020
    December 2019
    November 2019
    October 2019
    July 2019
    October 2018
    September 2018
    May 2018
    April 2018
    November 2017
    October 2017
    July 2017
    June 2017
    June 2015
    August 2014
    January 2014
    October 2013
    September 2013
    June 2013
    May 2013
    April 2013
    March 2013
    February 2013
    January 2013
    December 2012
    November 2012
    October 2012
    September 2012
    August 2012
    July 2012
    June 2012
    May 2012
    April 2012
    March 2012
    February 2012
    January 2012
    December 2011
    November 2011
    October 2011
    August 2011
    March 2011
    October 2010
    July 2010
    January 2009
    December 2008
    October 2008

    Categories

    All
    2011
    Alternate Energy
    Android
    Angela Sullivan
    Animals
    Antiques
    Apple
    Ash Canyon
    Astronomy
    AT&T
    Bailout
    Battery
    Bicycling
    Biometrics
    Books
    Carrier Iq
    Carson City
    Centennial
    C Hill
    C-Hill
    Christmas
    Climate
    Clothing
    Coding
    Colorado
    Columbus
    Cramps
    Curiosity
    Dad
    Dardanelles Lake
    Dell
    Dick's Lake
    Dilbert
    Eagle Lake
    Earworms
    Eating Problems
    Eclipse
    Economy
    Education
    Eldorado Canyon
    Energy
    Errors
    Espionage
    Europe 2019
    Evi
    Fallon
    Family
    Fontanillis Lake
    Food
    Garmin
    Geocaching
    Goals
    Google
    Google Earth
    Grouse Lake
    Hiking
    Inov8
    Investing
    Ipad
    Iphone
    Iron Mountain
    Legislatures
    Mac
    Market
    Market Drop
    Mars
    Mctarnahan
    Medicine
    Microsoft Word
    Motivation
    Mountain Biking
    Moving Minutes
    Music
    Nevada Day
    Nfc
    Ohio
    Olympics
    Openoffice
    Opportunity
    Panama 2018
    Paper Airplane
    People
    Politics
    Prison Hill
    Privacy
    Puts
    Puzzles
    Race
    Rant
    Reno
    Retrospective
    Roosevelt
    Running
    Running Dynamics
    Saddest Cities
    Safe & Sober
    Science
    Shoes
    Shopping
    Sierra
    Sierra Canyon
    Slangsploration
    Snl
    Soccer
    Software
    Spasms
    Spirit
    Sullivan Canyon
    Svn
    Tahoe
    Tahoe Rim Trail
    Taxes
    Technology
    Transit
    Travel
    Trees
    Vba
    Velma Lakes
    Venus
    Verizon
    Violin
    Watches
    Weather
    Wolframalpha
    Words
    Wrestling
    Writing
    Xkcd
    Yawbe
    Yoga

This is ALL MINE, I tell you! copyright 2010-2021