none
Using HTML Application (HTA) to create a dynamic link to a local file

    Question

  • I have no programming skills, and I wanted to create a simple launch screen to a series of files such as Word Templates.

    I used Frontpage 2003 to create a simple webpage consisting of a series of hyperlinks to  launch templates stored in the following directory:
    C:\Users\Admin\AppData\Roaming\Microsoft\Templates

    I then saved the index page as an HTML Application (HTA). 

    The issue I am having is when I come to add this to my other machines, they have different usernames so the links no longer work.

    Is it possible to build code that would recognise the links not as C:\Users\Admin\AppData\Roaming\Microsoft\Templates, but more as C:\Users\[Username]\AppData\Roaming\Microsoft\Templates and would open regardless of the username of the machine it was installed to?

    Alternatively is there another way I can launch the files?

    Many thanks for any help that someone may be able to provide.

    Thursday, October 25, 2012 12:36 PM

Answers

  • OK, I was intrigued, so I messed with this a bit.  Here is a solution that lists all of the files in the template folder and then launches the selected one in the approriate application ...

    <html>
    <head>
    <script type=text/vbs>
    Dim sTemplates, oWSHShell ' global variable
      
    sub window_onload
    Dim file, oAnchor ' local variable
      
      set oWSHShell = CreateObject("WScript.Shell")
      sTemplates = oWSHShell.expandenvironmentstrings("%userprofile%") _ 
               & "\AppData\Roaming\Microsoft\Templates\"
      
      with CreateObject("Scripting.FileSystemObject")
        for each file in .getfolder(sTemplates).Files
          set oAnchor = document.createElement("A")
          oAnchor.href = file.Path
          oAnchor.innerText = file.name & vbcrlf
          oAnchor.onclick = GetRef("Launch")
          list.appendChild(oAnchor)
        next ' files
      end with ' FileSystemObject
    end sub
    sub Launch
      with window.event
        .cancelBubble = true
        .returnvalue = false
        oWSHShell.run chr(34) & .srcElement.href & chr(34), 1, False
      end with
    end sub  
      
    </script>
    </head><body><span id=list></span></body></html>


    Tom Lavedas


    • Edited by Tom LavedasModerator Thursday, October 25, 2012 3:45 PM to fix typos
    • Marked as answer by dm75 Thursday, October 25, 2012 3:58 PM
    Thursday, October 25, 2012 3:23 PM

All replies

  • Here is one way I can think of (there are several others) ...

    <html>
    <head>
    <script type=text/vbs>
    Dim sProfile
      
    sub window_onload
      sProfile = CreateObject("WScript.Shell").expandenvironmentstrings("%userprofile%")
      anchor1.href = sProfile & "\AppData\Roaming\Microsoft\Templates"
    end sub
      
    </script>
    </head>
    <body>
    <a id=anchor1>Templates</a>
    </body>
    </html>


    Tom Lavedas

    Thursday, October 25, 2012 1:11 PM
  • Thanks for your code Tom

    I added the above code to the head and body section of the webpage that the link resides.

    How would I write the HTML links to individual files within the main body now?

    Right now I have written it like below:

    <a href="../../../../Admin/AppData/Roaming/Microsoft/Templates/test_template.docx">Test Template</a>

    The links don't see to work.

    How should the new links be written now?

    Regards

    Chris

    Thursday, October 25, 2012 1:22 PM
  • You could try:

    <a href="%USERPROFILE%\AppData\Roaming\Microsoft\Templates">Link</a>

    Thursday, October 25, 2012 1:45 PM
  • You need to add new '<a id=anchorN>Descriptive Text</a>' lines (without the quotes) in the<body> section of the document, one for document to be referenced.  The N at the end of the ID is a unique number for each anchor.  Then add a line of code in the Window_onload subroutine to assign the respective HRef text for each file, something like this, ...

    <html>
    <head>
    <script type=text/vbs>
    Dim sProfile
      
    sub window_onload
      sProfile = CreateObject("WScript.Shell").expandenvironmentstrings("%userprofile%")
      anchor1.href = sProfile & "\AppData\Roaming\Microsoft\Templates\test_template.docx"
      anchor2.href = sProfile & "\AppData\Roaming\Microsoft\Templates\Another_test_template.docx"
      anchor3.href = sProfile & "\AppData\Roaming\Microsoft\Templates\test_template_No_3.docx"
    end sub
      
    </script>
    </head>
    <body>
    <a id=anchor1>Templates</a>
    <a id=anchor2>Templates</a>
    <a id=anchor3>Templates</a>
    </body>
    </html>

    Or a slightly simpler approach uses an array of anchors ...

    <html>
    <head>
    <script type=text/vbs>
    Dim sProfile
      
    sub window_onload
      sProfile = CreateObject("WScript.Shell").expandenvironmentstrings("%userprofile%") _ 
               & "\AppData\Roaming\Microsoft\Templates\"
      anchor(0).href = sProfile & "test_template.docx"
      anchor(1).href = sProfile & "Another_test_template.docx"
      anchor(2).href = sProfile & "test_template_No_3.docx"
    end sub
      
    </script>
    </head>
    <body>
    <a id=anchor>Templates</a>
    <a id=anchor>Templates</a>
    <a id=anchor>Templates</a>
    </body>
    </html>

    This one moved the common part of the path to be part of the 'sProfile' variable.  It makes the resulting code a little bit less cluttered, so it esier to maintain, I feel.

    Tom Lavedas

    Thursday, October 25, 2012 1:53 PM
  • Thanks for the replies.  Tom, your latest code works, with just one issue.

    The template now opens in Word, but it is contained within the HTML Application (HTA), rather than in MS Word itself.

    Any way to make it launch into the Word application?

    This seems to be a general problem in linking to a Word Template from the HTML Application (HTA).

    In the past I have got round it by creating a shortcut to the template and then linking to that, but if you have a better solution, I would rather use that than litter my Word Templates folder with shortcuts to templates!

    Update:

    If there is no way to resolve this, my rudimentary / non programming solution to create a shortcut to the template does work.  I can prevent the templates folder from becoming too cluttered by collating all of the shortcuts to templates into a single folder.

    Thus I have used the following code that does work:

    <script type=text/vbs>
    Dim sProfile
    sub window_onload
      sProfile = CreateObject("WScript.Shell").expandenvironmentstrings("%userprofile%")
      anchor1.href = sProfile & "\AppData\Roaming\Microsoft\Templates\Shortcuts\test_template - Shortcut.lnk"
    end sub
     
    </script>

    If you think this is the only solution, I will mark this as solved! 

    Thanks once again,

    Chris


    • Edited by dm75 Thursday, October 25, 2012 2:57 PM
    Thursday, October 25, 2012 2:23 PM
  • That can't work because the percent sign has a different meaning in HTML and the DOM doesn't 'know' anything about MS environment variables.

    Tom Lavedas

    Thursday, October 25, 2012 3:02 PM
  • I have switched to using your less cluttered code:

    <script type=text/vbs>
    Dim sProfile
     
    sub window_onload
      sProfile = CreateObject("WScript.Shell").expandenvironmentstrings("%userprofile%") _
               & "\AppData\Roaming\Microsoft\Templates\Shortcuts"
      anchor1.href = sProfile & "Test_template - Shortcut.lnk"
    end sub
     
    </script>

    It works fine. 

    I wasn't sure what you meant by "That can't work because the percent sign has a different meaning in HTML and the DOM doesn't 'know' anything about MS environment variables."  Are you referring to "%userprofile%"?


    • Edited by dm75 Thursday, October 25, 2012 3:19 PM
    Thursday, October 25, 2012 3:18 PM
  • OK, I was intrigued, so I messed with this a bit.  Here is a solution that lists all of the files in the template folder and then launches the selected one in the approriate application ...

    <html>
    <head>
    <script type=text/vbs>
    Dim sTemplates, oWSHShell ' global variable
      
    sub window_onload
    Dim file, oAnchor ' local variable
      
      set oWSHShell = CreateObject("WScript.Shell")
      sTemplates = oWSHShell.expandenvironmentstrings("%userprofile%") _ 
               & "\AppData\Roaming\Microsoft\Templates\"
      
      with CreateObject("Scripting.FileSystemObject")
        for each file in .getfolder(sTemplates).Files
          set oAnchor = document.createElement("A")
          oAnchor.href = file.Path
          oAnchor.innerText = file.name & vbcrlf
          oAnchor.onclick = GetRef("Launch")
          list.appendChild(oAnchor)
        next ' files
      end with ' FileSystemObject
    end sub
    sub Launch
      with window.event
        .cancelBubble = true
        .returnvalue = false
        oWSHShell.run chr(34) & .srcElement.href & chr(34), 1, False
      end with
    end sub  
      
    </script>
    </head><body><span id=list></span></body></html>


    Tom Lavedas


    • Edited by Tom LavedasModerator Thursday, October 25, 2012 3:45 PM to fix typos
    • Marked as answer by dm75 Thursday, October 25, 2012 3:58 PM
    Thursday, October 25, 2012 3:23 PM
  • Yes, I was referring to the attempt to use the environment variable, %UserProfile%, in the HTML tag to name the file.  It won't work there.  It works up in the <script> section because we are using the WScript.Shell object's method to implement the needed expansion.


    Tom Lavedas

    Thursday, October 25, 2012 3:27 PM
  • Wow, that is clever, thanks for that!

    It also seems to stop the problem with the template opening Word inside the HTML Application (HTA) shell.

    This will save a lot of time. 

    I will mark above post as answer!





    • Edited by dm75 Thursday, October 25, 2012 3:59 PM
    Thursday, October 25, 2012 3:34 PM