Question

I am new to posting to Stack but have been using this site to solve a lot of coding issues so I know some of the basics. I have tried for hours (that's a lot for me on one issue) to get past this problem. I am creating a local tool (HTA) to eventually manage CSV files however I am stuck on this first part.

I am creating an explorer type selection tool where you pick the root folder it will load each subfolder as a button, it works great except if a subfolder has a space it spits out "Unterminated String Constant". I have worked around most the issues but since I want it to call back using this name I can not simply take out the space or replace it because clicking it's button will not work.

Any help would be VERY appreciated!

P.S. It's unfinished and I am so sorry there are no comments or descriptions... P.S.S. Please let me know any noob things you see too... whether it be site etiquette, or coding. Thanks!!! Stackoverflow has been a huge help for me!!!

Issue is on line:

strHtml = strHtml & "<td><input type=button value='" & strFolderName & "' name=btn_'" & strFolderName & "' onClick=btnCall('" & objFolder.Name & "')></td><Br>"

Full Code is below:

<html>
    <head>
    <title>CSV Menu Selector</title>
    <hta:application
         scroll="no"
         singleinstance="no"
         windowstate="normal"
    >
    </head>

<script type="text/vbscript">
Dim objPath
Dim Master
Dim Master1
Dim g_date_input ' globally saved input object

Function GetDate(obj)
  div1.style.visibility="visible"
  set g_date_input = obj
End Function

Sub cal1_click()
  g_date_input.value = cal1
  div1.style.visibility="hidden"
End Sub


''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'''''''''''''''''''''''''''''''BrowseFolders'''''''''''''''''''''''''''''
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Sub BrowseSub
  Set objShell = CreateObject("Shell.Application")
  Set objFolder = objShell.BrowseForFolder (0, "Select The Folder To Enumerate :", (0),17)

  If objFolder Is Nothing Then
    Exit Sub
  Else
    Set objFolderItem = objFolder.Self
    objPath = objFolderItem.Path
  End If
  ShowData
End Sub

Sub txtFile_OnkeyPress
  If window.event.Keycode = 13 Then
    objPath = txtFile.value
    ShowData
  End If
End Sub

Sub ShowData
  If objPath = "::{20D04FE0-3AEA-1069-A2D8-08002B30309D}" Then
    objPath = "C:\"
  End If

  Set fso = CreateObject("Scripting.FileSystemObject")

  If fso.FolderExists(objPath) Then
    txtFile.value = objPath
  Else
    MsgBox "Unable to use this path:" & vbCrLf & objPath
    Exit Sub
  End If

  Set objFso = CreateObject("Scripting.FileSystemObject")
  Set objFolder = objFso.GetFolder(objPath)
  'For each objFolder in objFolder.Subfolders
  'If objFolder.Subfolders.Count > 0 Then

  strHtml = "<html><body><table>"
//msgbox objFolder.Subfolders.Count
  For each objFolder in objFolder.Subfolders
    Set objOption = Document.createElement("OPTION")
    objOption.text = objFolder.Subfolders
    objOption.value = objFolder.Subfolders
    //msgbox "objFolder: " & objFolder

  strFolderName = Replace(objFolder.Name," ","_")
  strFolderName2 = Replace(objFolder.Name," ","&nbsp")
  //msgbox "NoBlanks: " & strFolderName

  Window.Document.Title = "Information For " & objPath
  strHtml = strHtml & "<td><input type=button value='" & strFolderName & "' name=btn_'" & strFolderName & "' onClick=btnCall('" & objFolder.Name & "')></td><Br>"
  //Msgbox strHtml
  'End If
  Next
  strHtml = strHtml & "</table></body></html>"
  Msgbox strHtml
  DataArea.InnerHtml = strHtml

End Sub

Sub btnCall(strBtnName)
    objPath = objPath & "\" & strBtnName
    msgbox "objPath: " & objPath
    ShowData
End Sub

Sub CheckMaster
  Master = txtFile.value
  If txtFile.value ="" Then
    msgbox "Please Enter Database Name"
  Else
    msgBox "Master is: " & Master
    TrimMaster
  End If
End Sub

Sub TrimMaster
    Master1 = Mid(txtFile.value, 1+InStrRev(txtFile.value,"\"))
    msgBox "Master1 is: " & Master1
End Sub

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

</script>

<body>

<div>

  <!-- Begin Browse for Folder -->
  <input type="text" name="txtFile" size="50" />
  <input id="btnBrowse" type="button" value="Browse..." onClick="BrowseSub" />
  <input type="button" value="Submit" name="run_button" onClick="CheckMaster"><br></td>
  <!-- End Browse for Folder -->

  <!-- Begin Browse for Folder-->
  <input Type="Button" Value="Reset" onClick="location.reload()" /><p></td>
  <!-- <input Type="Button" Value="Browse For Folder" Name="Run_Button" onClick="BrowseSub"><p></td> -->
  <Span Id = "DataArea"></Span><Div Align = "Center">
  <!-- <select style="background-color:#ffb7d6" size="8" onActivate=LoadDropDown name="Scanners" onChange="TestSub"> -->
  <!-- End Browse for Folder -->

  <!-- Begin Get Dates -->
  <!-- <input id="ddate1" type="text" value="click here" onclick="GetDate(me)"> -->
  <!-- <input id="ddate2" type="text" value="click here" onclick="GetDate(me)"> -->
  <div id="div1" style="visibility:hidden;">
    <object id="cal1" classid="clsid:8E27C92B-1264-101C-8A2F-040224009C02"></object>
  </div>
  <!-- End Get Dates -->

</div>

</body>
</html>
Was it helpful?

Solution

Wrong quote placement. Splitted to make it more visible

Dim td
    td = Array( _ 
            "<td>" _ 
            , "<input type='button'" _ 
            , " value='", objFolder.Name, "'" _ 
            , " name='btn_" , strFolderName, "'" _ 
            , " onClick='btnCall(""" , objFolder.Name , """)'" _ 
            , "></td><br>" _ 
        )

    strHtml = strHtml & Join(td,"")

OTHER TIPS

Try using this helpful Function DblQuote(Str) to add the double quotes into a variable

Function DblQuote(Str)
    DblQuote = Chr(34) & Str & Chr(34)
End Function

So your code become something like that :

<html>
    <head>
    <title>CSV Menu Selector</title>
    <hta:application
         scroll="no"
         singleinstance="no"
         windowstate="normal"
    >
    </head>

<script type="text/vbscript">
Dim objPath
Dim Master
Dim Master1
Dim g_date_input ' globally saved input object

Function GetDate(obj)
  div1.style.visibility="visible"
  set g_date_input = obj
End Function

Sub cal1_click()
  g_date_input.value = cal1
  div1.style.visibility="hidden"
End Sub

Function DblQuote(Str)
    DblQuote = Chr(34) & Str & Chr(34)
End Function

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'''''''''''''''''''''''''''''''BrowseFolders'''''''''''''''''''''''''''''
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Sub BrowseSub
  Set objShell = CreateObject("Shell.Application")
  Set objFolder = objShell.BrowseForFolder (0, "Select The Folder To Enumerate :", (0),17)

  If objFolder Is Nothing Then
    Exit Sub
  Else
    Set objFolderItem = objFolder.Self
    objPath = objFolderItem.Path
  End If
  ShowData
End Sub

Sub txtFile_OnkeyPress
  If window.event.Keycode = 13 Then
    objPath = txtFile.value
    ShowData
  End If
End Sub

Sub ShowData
  If objPath = "::{20D04FE0-3AEA-1069-A2D8-08002B30309D}" Then
    objPath = "C:\"
  End If

  Set fso = CreateObject("Scripting.FileSystemObject")

  If fso.FolderExists(objPath) Then
    txtFile.value = objPath
  Else
    MsgBox "Unable to use this path:" & vbCrLf & objPath
    Exit Sub
  End If

  Set objFso = CreateObject("Scripting.FileSystemObject")
  Set objFolder = objFso.GetFolder(objPath)
  'For each objFolder in objFolder.Subfolders
  'If objFolder.Subfolders.Count > 0 Then

  strHtml = "<html><body><table>"
//msgbox objFolder.Subfolders.Count
  For each objFolder in objFolder.Subfolders
    Set objOption = Document.createElement("OPTION")
    objOption.text = objFolder.Subfolders
    objOption.value = objFolder.Subfolders
    //msgbox "objFolder: " & objFolder

  strFolderName = Replace(objFolder.Name," ","_")
  strFolderName2 = Replace(objFolder.Name," ","&nbsp")
  //msgbox "NoBlanks: " & strFolderName

  Window.Document.Title = "Information For " & DblQuote(objPath)
  'strHtml = strHtml & "<td><input type=button value='" & strFolderName & "' name=btn_'" & strFolderName & "' onClick=btnCall(" & objFolder.Name & ")></td><Br>"
  Dim td
    td = Array( _ 
            "<td>" _ 
            , "<input type='button'" _ 
            , " value='", objFolder.Name, "'" _ 
            , " name='btn_" , strFolderName, "'" _ 
            , " onClick='btnCall(" , DblQuote(objFolder.Name) , ")'" _ 
            , "></td><br>" _ 
        )

    strHtml = strHtml & Join(td,"")
  //Msgbox strHtml
  'End If
  Next
  strHtml = strHtml & "</table></body></html>"
  Msgbox strHtml
  DataArea.InnerHtml = strHtml

End Sub

Sub btnCall(strBtnName)
    objPath = objPath & "\" & strBtnName
    msgbox "objPath: " & DblQuote(objPath)
    ShowData
End Sub

Sub CheckMaster
  Master = txtFile.value
  If txtFile.value ="" Then
    msgbox "Please Enter Database Name"
  Else
    msgBox "Master is: " & Master
    TrimMaster
  End If
End Sub

Sub TrimMaster
    Master1 = Mid(txtFile.value, 1+InStrRev(txtFile.value,"\"))
    msgBox "Master1 is: " & Master1
End Sub

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

</script>

<body>

<div>

  <!-- Begin Browse for Folder -->
  <input type="text" name="txtFile" size="50" />
  <input id="btnBrowse" type="button" value="Browse..." onClick="BrowseSub" />
  <input type="button" value="Submit" name="run_button" onClick="CheckMaster"><br></td>
  <!-- End Browse for Folder -->

  <!-- Begin Browse for Folder-->
  <input Type="Button" Value="Reset" onClick="location.reload()" /><p></td>
  <!-- <input Type="Button" Value="Browse For Folder" Name="Run_Button" onClick="BrowseSub"><p></td> -->
  <Span Id = "DataArea"></Span><Div Align = "Center">
  <!-- <select style="background-color:#ffb7d6" size="8" onActivate=LoadDropDown name="Scanners" onChange="TestSub"> -->
  <!-- End Browse for Folder -->

  <!-- Begin Get Dates -->
  <!-- <input id="ddate1" type="text" value="click here" onclick="GetDate(me)"> -->
  <!-- <input id="ddate2" type="text" value="click here" onclick="GetDate(me)"> -->
  <div id="div1" style="visibility:hidden;">
    <object id="cal1" classid="clsid:8E27C92B-1264-101C-8A2F-040224009C02"></object>
  </div>
  <!-- End Get Dates -->

</div>

</body>
</html>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top