vb.net pass folder path as commandline argument - vb.net

I'm trying to pass a folder path via commandline argument to an application.
Problem: my folder path contains space " " in it's string. When I read the commandline arguments in the application I get my path chopped into pieces on the space " "
Sub Main()
Dim arguments As String() = System.Environment.GetCommandLineArgs()
For Each Arg As String In arguments
Console.WriteLine("Argument : " & Arg)
Next
Console.ReadLine()
End Sub
Edit: added code to build my argument
Private Sub btn_Copy_Click(sender As Object, e As EventArgs) Handles btn_Copy.Click
Dim args(3) As String
args(0) = """" & tb_CopyFromPath.Text & """"
args(1) = """" & tb_CopyToPath.Text & """"
args(2) = """" & tb_ItemTag.Text & """"
args(3) = """" & tb_Prefix.Text & """"
Dim argument As String
argument = args(0) & " " & args(1) & " " & args(2) & " " & args(3)
Process.Start("J:\VB.NET - EM AddIn\EM_Design_AddIn\CopyDesign\bin\Debug\CopyDesign.exe", argument)
End Sub
This result isn't okay. The first argument of the first path now contains a piece of the second path.
Edit: add value result from debug.
"""C:\VaultWorkspace\cadcampc\03-Vessel configurator - R2.0\Nozzles\WN_RF_ASME_B16.5\"" ""C:\VaultWorkspace\cadcampc\03-Vessel configurator - R2.0\Test Copy Design\N03"" ""N3"" ""12345-3"""

It is very simple. Just use ".
If you pass test test test parameters, you'll get 3 arguments. But if you write test "test test", you'll receive two parameters: test and test test.

Just pass it with double-quotes around it.
I.e:
app.exe "C:\Sub folder 1\Sub folder 2"
If you do it in code:
Process.Start("app.exe", """" & path & """")
The quotes specifies the start and end of an argument.
EDIT:
In your case you could do this instead:
argument = """" & args(0) & """ """ & args(1) & """ """ & args(2) & """ """ & args(3) & """"

Use CHR(34) to delimiter a string with spaces within.
See my answer here:
vb.net How to pass a string with spaces to the command line

Related

Ms Access Get filename with wildcards or loop

I am using MS Access Forms and I am trying to open a file but don't know how to open the file based knowing only part of the name. Example below works
Private Sub Open_Email_Click()
On Error GoTo Err_cmdExplore_Click
Dim x As Long
Dim strFileName As String
strFileName = "C:\data\office\policy num\20180926 S Sales 112.32.msg"
strApp = """C:\Program Files\Microsoft Office\Office15\Outlook.exe"""
If InStr(strFileName, " ") > 0 Then strFileName = """" & strFileName & """"
x = Shell(strApp & " /f " & strFileName)
Exit_cmdExplore_Click:
Exit Sub
Err_cmdExplore_Click:
MsgBox Err.Description
Resume Exit_cmdExplore_Click
End Sub
If I change the strFilename to being
strFileName = "C:\data\" & Me.Office & "\" & Me.nm & " " & Me.pol & "\" & "*"& " S Sales " & Me.amt & "*" & ".msg"
It includes the * rather than using it as a wildcard, the date/numbers can be anything or in another format but always eight numbers. I tried using a while loop on the numbers but I am not sure the best way of doing this sorry.
You can use the Dir function to iterate over all files that match a string pattern.
strApp = """C:\Program Files\Microsoft Office\Office15\Outlook.exe"""
Dim strFilePattern As String
strFilePattern ="C:\data\" & Me.Office & "\" & Me.nm & " " & Me.pol & "\" & "*"& " S Sales " & Me.amt & "*" & ".msg"
Dim strFileName As String
strFileName = Dir(strFilePattern)
Do While Not strFileName = vbNullString
If InStr(strFileName, " ") > 0 Then strFileName = """" & strFileName & """"
x = Shell(strApp & " /f " & strFileName)
strFileName = Dir
Loop
The first call to Dir with the pattern as a parameter will find the first file that matches the pattern supplied. All subsequent calls without the pattern will return the next file that matches the pattern.
So, lets rebuild the question a bit. Imagine that you are having the following 5 files in a given folder:
A:\peter.msg
A:\bstack.msg
A:\coverflow.msg
A:\heter.msg
A:\beter.msg
and you need to find the files, that correspond to "A:\*eter.msg" and print them.
For this, you need to use the keyword Like:
Sub TestMe()
Dim someNames As Variant
someNames = Array("A:\peter.msg", "A:\bstack.msg", _
"A:\coverflow.msg", "A:\heter.msg", "A:\beter.msg")
Dim cnt As Long
For cnt = LBound(someNames) To UBound(someNames)
If someNames(cnt) Like "A:\*eter.msg" Then
Debug.Print someNames(cnt)
End If
Next
End Sub
Loop through files in a folder using VBA?

How to assign a macro in a Word Document to open a PDF to specific page

I am trying to assign a macro with commandbutton in a Word document which when clicked should open a PDF document of page 9. I trying with the below Code but not successful in solving my Problem.
I am adding the below Code in 'Module 1'.
Private Sub CommandButton1_Click()
App_Path = "C:\Program Files\Adobe\Reader 10.0\Reader\AcroRd32.exe"
File_Path = "C:\Users\Desktop\USER MANUAL.pdf"
Page_Num = 4
Shell App_Path & " /A Page=" & Page_Num & "" & File_Path, vbMaximizedFocus
End Sub
Because your paths contain spaces both paths need to be enclosed in quotes ""
App_Path = """C:\Program Files\Adobe\Reader 10.0\Reader\AcroRd32.exe"""
File_Path = """C:\Users\Desktop\USER MANUAL.pdf"""
Also there needs to be a space between Page_Num and File_Path so replace & "" & with & " " &.
Shell App_Path & " /A Page=" & Page_Num & " " & File_Path, vbMaximizedFocus

Keep Process.Start() From running at the same time

I am having a problem, i was doing files copy and then combine them into pdf file using "pdftk" from cmd.
i put the copy process into a nest loop to get the files i wanted. However, the combine process start at the same time as the copy process run which combines any file already copied. It should wait until the copy done then start the combine. Please help me with this
Private Sub PDF_Print(ProcessDir As String)
Dim i As Integer = 0
For i = 1 To CInt(txtNumofSet.Text)
Dim Labelcopy As String = "/c S: & cd ""S:\User Files\Shipping & Receiving\NewlyWeds labels\process"" & for /l %x in (1, 1, " & txtCopies.Text & ")" &
"do (copy """ & SourceFile & """ workarea\" & i & "S" & "%x.pdf)"
Dim Run = Process.Start("cmd.exe", Labelcopy)
Dim Blankcopy As String = "/c S: & cd ""S:\User Files\Shipping & Receiving\NewlyWeds labels\process"" & for /l %x in (1, 1, " & txtNumofblanks.Text & ")" &
"do (copy """ & ProcessDir & """ workarea\" & i & "SB" & "%x.pdf)"
Dim Run1 = Process.Start("cmd.exe", Blankcopy)
Next
Process.Start("S:\User Files\Shipping & Receiving\NewlyWeds labels\process\Label.cmd")
' Me.Close()
End Sub
Is there any ways to tell the last the Process.start to start after the first two finish running. Thank you.
You should use the .NET framework to copy the files, e.g. File.Copy
One can use the Process.HasExited Property to see if a process has completed. Here is a simple example:
Dim objProcess As Process = Process.Start("cmd.exe", Labelcopy)
Do Until objProcess.HasExited
System.Threading.Thread.Sleep(100)
Loop
Process.Start("S:\User Files\Shipping & Receiving\NewlyWeds labels\process\Label.cmd")

Excel VBA Shell "file not found"

For the life of me, I can't get this shell procedure to work with variables. I'm trying to execute POSTRESULTS6 which then processes the csv file. The "DISPLAY" argument tells POSTRESULTS6 to show a progress window as it processes the csv.
Dim MyAppID As Variant
Dim stExecute As String: stExecute = """C:\Program Files (x86)\PerkinElmer\LABWORKS64\POSTRESULTS6.exe"""
Dim stFile As String: stFile = "INFILE:C:\PostResult6\Multi-Component_Data_Import.csv"
Dim stFull As String: stFull = "stExecute & "" "" & stFile"
MyAppID = Shell("stFull" & "" "" & DISPLAY, vbNormalFocus)
AppActivate (MyAppID)
The procedure works as expected when I type it all out in one single line like this:
MyAppID = Shell("""C:\Program Files (x86)\PerkinElmer\LABWORKS64\POSTRESULTS6.exe"" & "" "" & INFILE:C:\PostResult6\Multi-Component_Data_Import.csv" & "" "" & DISPLAY, vbNormalFocus)
The reason that I want to use variables is to shorten the lines of code and possibly make the procedure dynamic for the csv file name.
I'm guessing that it has something to do with spaces but I can't figure out where my mistake is. I also can't figure out how to continue the long string of code to the next line. The space-underscore doesn't seem to work.
As an aside, when my shell procedure was very simple, I didn't have to declare the MyAppID variable. As it got more complex, I started getting errors about MyAppID not being defined. Does anyone know why this is?
Any help is greatly appreciated.
Edit: The Error that I get when I run the code with variables is "Run-time error '53': File not found"
I know that the file paths assigned to the variables are correct. I tested them in windows explorer.
Your problem is, for example: stFull = "stExecute & "" "" & stFile"
You have stExecute within quotes which makes it a literal not a variable.
I think this is correct:
Dim MyAppID As Variant
Dim stExecute As String: stExecute = """C:\Program Files (x86)\PerkinElmer\LABWORKS64\POSTRESULTS6.exe"""
Dim stFile As String: stFile = "INFILE:C:\PostResult6\Multi-Component_Data_Import.csv"
Dim stFull As String: stFull = stExecute & " " & stFile
MyAppID = Shell(stFull & " DISPLAY", vbNormalFocus)
You've got messed up quotes. stFull in your last line is being evaluated as a literal not as a string. Here's my attempt to solve your quotes debacle.
Dim stExecute As String: stExecute = "C:\Program Files (x86)\PerkinElmer\LABWORKS64\POSTRESULTS6.exe"
Dim stFile As String: stFile = "INFILE:C:\PostResult6\Multi-Component_Data_Import.csv"
Dim StFull As String
StFull = """" & """" & """" & stExecute & """" & """" & " & """" """" & " & stFile & """"
MyAppID = Shell(stFull & "" "" & DISPLAY, vbNormalFocus)
AppActivate (MyAppID)
Outputting StFull:
My perspective for using variables in Shell:
MyAppID as variant
Dim Program as string
Dim File as string
Program = """Your path to program"""
File = """Your path to file"""
MyAppID = Shell(Program & " " & File, vbNormalFocus)
Additional comments:
Single quotes can be used (e.g. Program = "Your path to program") but then VBA may have problem to compile properly if you have spaces in folders names and would return an error.

Writing one file with two functions in vba

This is the header of my main function to write excel cells to an XML file. I want this to call another function, which can do its own set of writing.
Public Sub WriteXML()
Dim Sheet As Worksheet
Dim Cell As Range
Dim xmlFile
xmlFile = ThisWorkbook.Path & "\" & 'Test1' & ".xml"
Set Sheet = ActiveWorkbook.Worksheets("Sht1")
Open xmlFile For Output As #1
Print #1, "<?xml version=" & Chr(34) & "1.0" & Chr(34) & _
" encoding=" & Chr(34) & "UTF-8" & Chr(34) & "?>"
Call WriteCustomer(xmlFile)
This is the start of the second function, though I'm getting an 'object not found' sort of error.
Sub WriteCustomer(x As Variant)
Print x, " <Customer>"
Print x, " <First>" & 'Bill' & "</First>"
Print x, " <Last>" & 'Johnson' & "</Last>"
Print x, " </Customer>"
Print x, ""
End Sub
How do I need to construct the call and/or variable to pass the open file as an object to the second function?
You can request, store and pass around a handle as follows:
Dim handle As Integer
handle = FreeFile()
Open xmlFile For Output As #handle
Print #handle, "<?xml version=" & Chr(34) & "1.0" & Chr(34) & _
...
Call WriteCustomer(handle)
And
Sub WriteCustomer(handle As Integer)
Print #handle, " <Customer>"
Since you have opened the file in the first function with the line
Open xmlFile For Output As #1
Any code that references #1 while it's open will write to the same file. Thus you can simply rewrite your second function as
Sub WriteCustomer()
Print #1, " <Customer>"
Print #1, " <First>" & 'Bill' & "</First>"
Print #1, " <Last>" & 'Johnson' & "</Last>"
Print #1, " </Customer>"
Print #1, ""
End Sub