Writing out a quoted variable - vba

I"m struggling with some code that writes out a VBS file, for a particular line that writes a file path which holds a variable. Struggling with getting the double-quotes right and placing variable correctly.
This line currently works, but only if "C:\tmp" dir is already created:
Print #PayLoadFile, "HTTPDownload ""http://host.example.net/test1.exe"", ""C:\tmp"" "
Instead of C:\tmp, I would like to write to user profile directory. But I can't get the part to write out correctly in VBS file. I'm trying this:
Print #PayLoadFile, "HTTPDownload ""http://host.example.net/test1.exe"", ""Replace(myFile)"" "
The 'myFile' variable holds a string of the user profile directory ("c:\Users\John Doe")
It should print out to the vbs file as:
HTTPDownload "http://host.example.net/test1.exe", "C:\Users\John Doe"
But instead it looks like this:
HTTPDownload "http://host.example.net/test1.exe", "Replace(myFile)"

You need to concatenate the value of myFile into the string:
Print #PayLoadFile, "HTTPDownload ""http://host.example.net/test1.exe"", """ & myFile & """"

Related

How to indicate a directory path using variables?

I have to implement the use of a certain .exe file in VBA. The .exe takes as input a specific type of file and outputs a .txt file.
When I write the whole directory of both the input and output files, the code works. When I split the directory and store the parts in variables, it doesn't.
I need to split it because I am going to use this .exe with different directories so the user could choose the wanted directory.
Sub convert()
Dim directory As String
Dim Filename As String
directory = "C:\Users\user1\Desktop\reporting\201703161224"
Filename = "\input.set"
Shell "cmd /c""C:\Users\user1\Desktop\reporting\appli.exe
C:\Users\user1\Desktop\reporting\201703161224\input.set>
C:\Users\user1\Desktop\reporting\201703161224\output.txt"
'this works well
file = directory & Filename
Shell "cmd /c""C:\Users\user1\Desktop\reporting\appli.exe file>
C:\Users\user1\Desktop\reporting\201703161224\output.txt"
'this doesn't work
End Sub
You need to break out of the quotes and Concatenate to use your file string variable
Shell = "Hard_Coded_String_1" & file & "Hard_Coded_String_2"

How to concatenate files by calling Windows Shell with VBA

I am trying to write some VBA in Excel that will concatenate .csv files by executing the copy and more commands in the Windows Shell.
This code successfully creates an empty file concat_target.txt (into which the others will be concatenated):
Shell ("cmd.exe copy /b NUL " & """\\path\to\network\folder\concat_target.txt""")
However, this code does not have the intended effect of saving file_1.txt as file_1.concat_temp (skipping the header):
Shell ("cmd.exe more +1 " & """\\path\to\network\folder\file_1.txt""" & " > " & """\\path\to\network\folder\file_1.concat_temp""")
In fact, every variation of the call to more that I have tried just pops up a console (that stays open) whereas the call to copy does not. The UNC network path has cursed spaces in it.
UPDATE:
It turns out that adding the /c flag to the more command does the trick (although it seems to have no effect on the call to copy). So this code is having the desired effect:
Shell ("cmd.exe /c more +1 " & """\\path\to\network\folder\file_1.txt""" & " > " & """\\path\to\network\folder\file_1.concat_temp""")
I will post the full solution for posterity once it is finished.
I don´t know why you are using 'more', but I would use other methods:
Shell ("cmd.exe /c type ""\\path\to\network\folder\file_1.txt"" >> ""\\path\to\network\folder\file_1.concat_temp""")
Notes:
redirecting with '>' will overwrite the file, losing previous contents
redirecting with '>>' will append the file (concatenate)
no need to separate every element in your string with " & " if they are all text (but I guess you already knew this)
no need to create an empty file first.
UPDATE:
OP was using 'more' to exclude the .csv table headers, as I hadn´t understood before.
So the 'type' command won´t work in this case.

VBA MkDir silently removes caron

I am using VBA to create folders automatically. One of the folder names that I need to create contains the character č (c with caron). When I use MkDir in VBA the folder is created with a "c" instead of a "č".
Sample code:
root_folder = "C:\customers\"
folder_name = "háček" 'I do not think you can enter this into the VBA editor, but I am getting the folder_name from an external source
full_folder_path = root_folder & folder_name & "\"
MkDir full_folder_path
attachment.SaveAs full_folder_path & attachment.filename
This will create a folder called "C:\customers\hacek\" instead of "C:\customers\háček\", which then causes the save operation to fail, because it tries to save in "C:\customers\háček\, which of course does not exist. VBA seems to be able to read and handle the characters correctly, because I can read it from my data source and save it to a text file without issues. The problem just seems to exist when it comes to creating folders.
Is there a way to make VBA create the folder with the name that I have actually specified?
edit: formatting
If you use FileSystemObject, you are able to create the Folder with the proper name:
Dim fs As Object
Set fs = CreateObject("Scripting.FileSystemObject")
fs.CreateFolder "C:\customers\hac" & ChrW(269) & "ek" 'ChrW(269) prints č

Why does ftp not work with concatenated strings?

I have been scratching my head for hours on this one. I have a VBA macro that generates a text file containing a list of ftp commands that I will use with "ftp -s:". So far so good. However ftp does something very weird and splits a concatenated string an place it at the beginning of the line. I will give an example to better illustrate this. The following is the structure of the text file:
myUserName
myPassword
get myFile.zip
close
The characters in bold indicate where the concatenation occurred using Print #1, "get " & fileName & ".zip"
So when I run ftp -s:Commands.txt myftp.me.com i am able to login fine, except when ftp gets to third line with the get command. The ".zip" is split and placed in the beginning of the command line and basically executes .zip get myFile
I do not see this when I manually type the commands into a text file.
Any ideas on what might be happening? Thanks in advance.
I do not know, why this should not work, but what you may try, is to add quotes " " around the file names. normally this should work without quotes if you have no spaces in the filename, but I would give it a try.
Print #1, "get """ & fileName & ".zip"""

Pass Variable from Excel Macro to .vbs file

I have an excel button with a macro. The code simply stores the path of the workbook in a vaiable names 'MyCompletePath' and then runs a .vbs file. The code is:
MyCompletePath = ActiveWorkbook.FullName
'Run VBS file
Shell "wscript C:\Users\name\Desktop\vb.vbs", vbNormalFocus
I want to pass the variable 'MyCompletePath' from the excel file to the file which is executed using the last line.
I made some searches but didn't fully understand what their solutions do. Maybe someone can tell me how to do it.
UPDATE/EDIT: I'm now having a problem with the filepath after Shell "wscript. It has spaces in the folder name. How can I get it to work with spaces in the name?
Thank you.
You can include command line arguments after the path to your VBS file. For example:
Shell "wscript C:\Users\name\Desktop\vb.vbs BlahBlahBlah", vbNormalFocus
Then inside your VBS script, you can access them using the WScript.Arguments collection. For example:
MsgBox WScript.Arguments(0)
would pop up a message box displaying "BlahBlahBlah".
For a file path, as you indicated, which might include spaces and thus would be treated as multiple arguments to the script, I would include the argument in quotes, like this:
Shell "wscript C:\Users\name\Desktop\vb.vbs ""this has multiple words""", vbNormalFocus
based on passing argument from vba to vbs
When I write that within the vba:
Sub Macro1()
MyCompletePath = "toto"
Shell "wscript D:\\vb.vbs " & MyCompletePath
End Sub
and this in vb.vbs
MsgBox("Hello " & WScript.Arguments(0))
I do get "Hello toto"