How to add quotation marks to 2 strings and variable in the middle - vb.net

I'm trying to add quotations to a piece of string that is broken in 2 pieces with a variable in the middle.
I have tried many, many ways...many ways and failed.
Example:
Dim path as String = "C:\Users\" & CurrentUser & "\folder\path to something\"
I need the whole result to be in quotations to pass it to a command that requires the path with spaces in quotations.
"C:\Users\Nemo\Folder\Path to something\"
Any help is appreciated.

You should just be able to use "" inside the string assignment that you have. So with your example it would look like this:
Dim path As String = """C:\Users\" & CurrentUser & "\folder\path to something\"""

IMHO it is a good idea to use CHR(34) instead of actual quotes when concatenating strings. Your code should look like this:
Dim current user as string="Nemo"
Dim path as String = CHR(34) & "C:\Users\" & CurrentUser & "\folder\path to something\" & CHR(34)
The result will be:
"C:\Users\Nemo\folder\path to something\"

Related

VBA Run String as Code

I want to read strings of code from a database. A string should include pieces of text and one/two variables which are set later in the project. The result should look like this:
"Greetings, Tom."
Usually I would do something like this:
Sub Show()
Dim strName as String
strName = "Tom"
Debug.Print "Greetings, " & strName & "."
...
But in this case the part
"Greetings, " & strName & "."
is on my database. During runtime I set the value of strName to multiple names (eg in one case to "Tom" and in another case to "Marco")
How is it possible to let VBA know that strName is a variable and not a string to be shown? In the end my call should look like this:
strDbText = <String from Database>
strName = "Tom"
Debug.Print strDbText '--> Greetings, Tom.
Can anyone help me out one more time?
Thanks a lot!
edit:
This code will not be an executable. So there is no need to go for the VBIDE-Objects to create a new module and execute inserted code. It is a string which will be written in a Word Document.
Solution:
String Substitution with the Replace() function
I would assign strName first and replace it with the string in the Database like this...
strName = "Tom"
strDbText = <String from Database>
strDbText = Replace(strDbText, "strName", strName) 'This distinguishes the string from the variable
Debug.Print strDbText '--> Greetings, Tom.
However, I would suggest using another variable in your database in place of strName because it may become confusing to look at.
Absolutely agreeing #ShanayL answer and strongly recommending its usage, I couldn't avoid pointing an unsafe alternative to solve your question, using Eval. Eval is potentially dangerous, creating a code injection vulnerability in your application!!!
strDbText = """Greetings, "" & strName"
strName = "Tom"
Debug.Print Eval(strDbText) 'Greetings, Tom
Notice: Eval is potentially dangerous and its usage is strongly disencouraged!

Vba to call a powershell script or .bat file

I have a line to call a powershell script and it works but the powershell windows gets closed without it working.
the code is, any ideas?
Additionally I would like it to be able to run .bat files (I tried Shell ( & pathcrnt & "\GUI-getuserpropertiesV2.10.ps1" & pathcrnt) bit I get the same error.
Sub CallBatch()
Dim pathcrnt As String
pathcrnt = ActiveWorkbook.Path
Shell ("PowerShell " & pathcrnt & "\GUI-getuserpropertiesV2.10.ps1" & pathcrnt)
End Sub
Please see the link below and make sure you have permissions for the machine.
http://www.tek-tips.com/viewthread.cfm?qid=1585510
I don't know about your setup or security, so if you encounter an error, search for 'PS permissions', or something along those lines. The VBA script will definitely work!
If you insert a space after .ps1 it should work:
Sub CallBatch()
Dim pathcrnt As String
pathcrnt = ActiveWorkbook.Path
Shell "PowerShell " & pathcrnt & "\GUI-getuserpropertiesV2.10.ps1 " & pathcrnt
End Sub
Imagine the resulting string; for pathcrnt C:\Windows it would be this: "PowerShell C:\Windows\GUI-getuserpropertiesV2.10.ps1C:\Windows"
You also might want to enclose the path elements with double quotes to prevent spaces messing with your parameters, like this:
Shell "PowerShell """ & pathcrnt & "\GUI-getuserpropertiesV2.10.ps1"" """ & pathcrnt & """"
Since double quotes also are string delimiters in VBA you have to escape them with another double quote.

Use combobox in savepath

first of all: Sorry for the not so clear title. I didn't know a better way to descripe my question.
I'm building a application that has to save user-specified data to a sdcard on a plc.
I already found out how to connect to that plc but am still working on the saving part.
For the testing i just used:
ds.WriteXml("C:\" & DateTimePicker1.Text & ".xml")
I think it's possible to change it to \192.168.2.16\SDcard\filename but that's not very flexible.
What i would like to have is the ability to take the value from a combobox and use that as the ip adress.
What is the best way to do this? as i don't think it's a simpe thing like making the savepad
(\" & comboIP.selectedvalue & "\Sdcard\" & DateTimePicker1.Text & ".xml") Unfortunately, the SD card is still on it's way so i can't test it yet..
Thanks in advance!
ds.WriteXml("C:\" & comboIP.Text & "\SDCard\" & DateTimePicker1.Text & ".xml")
That works just fine.
You don't really need the SDCard in hand to test this out.
You can simply create temporary variables before the WriteXML function call, set a breakpoint on them, and ensure that they are the correct values beforehand.
e.g.:
Dim sSelectedIP As String = comboIP.Text
Dim sDateTimePicker As String = DateTimePicker1.Text
Dim sCompleteDirectory As String = "C:\" & sSelectedIP & "\SDCard"
If My.Computer.FileSystem.DirectoryExists(sCompleteDirectory) = False Then
My.Computer.FileSystem.CreateDirectory(sCompleteDirectory)
End If
ds.WriteXml(sCompleteDirectory & "\" & sDateTimePicker & ".xml")

Concatenating a complex string in VB. NET

I am using START-PROCESS to call MSTEST with multiple arguments that define the container and test settings, however I think it's choking on the way I'm concatenating this. Should I use some other method of constructing this string before putting it into START-PROCESS?
Dim rwSettings As String = "\\PerfvsCtlr2\LoadtestSettings\PerfVSCtlr2forRemote.testsettings"
Dim rwContainer As String = "\\PerfvsCtlr2\LoadTest\LoadTestDefs\Heifer_Interactive_Peak_Workload.loadtest"
Dim rwResults As String = Workload.txtRwResults.Text
System.Diagnostics.Process.Start(Environment.GetEnvironmentVariable("VS110COMNTOOLS") & "..\Ide\MSTEST.EXE", "/Testsettings:""" & rwSettings & "" & " /Testcontainer:""" & rwContainer & "" & " /Resultsfile:""" & rwResults & "")
The problem is unknown currently, because process.start opens and closes the window far too quickly for me to catch any sort of error message. So my question is two-fold:
Does the above concatenation look correct? Is there a way I can get more information on either the final execution string Process.Start is putting together or the error message it's returning?
You can use Path.Combine to build paths and String.Format to build the arguments for Process.Start:
Dim rwSettings As String = "\\PerfvsCtlr2\LoadtestSettings\PerfVSCtlr2forRemote.testsettings"
Dim rwContainer As String = "\\PerfvsCtlr2\LoadTest\LoadTestDefs\Heifer_Interactive_Peak_Workload.loadtest"
Dim rwResults As String = "Workload.txtRwResults.Text"
Dim fileName = System.IO.Path.Combine(Environment.GetEnvironmentVariable("VS110COMNTOOLS"), "Ide\MSTEST.EXE")
Dim args = String.Format("/Testsettings:{0} /Testcontainer:{1} /Resultsfile:{2}", rwSettings, rwContainer, rwResults)
System.Diagnostics.Process.Start(fileName, args)
However, i must admit thar i'm not sure if this yields the desired result. It might give you an idea anyway.
I suspect that your problem is that you are not closing your quotation marks, for instance:
" /Testcontainer:""" & rwContainer & ""
Should be:
" /Testcontainer:""" & rwContainer & """"
Notice that the double-quotation mark at the end needs to be a quadruple quotation mark. Simply saying "" means an empty string.
Should you use something else? Probably. It would be more readable and efficient if you used StringBuilder or String.Format, but even so, you'll still have to fix the closing quotes issue.

quotes in vb.net links

I want to make a link from what the user filepath that is given from an openfiledialog. But I can't get all the quotes in the right places
DOCTextBox.Text = "" & TitleTextBox.Text & ""
In order to put a " in a VB.Net string literal, you need to write "".
For example:
"<a href=""" & HttpUtility.HtmlAttributeEncode(OpenFileDialog1.FileName) _
& """target=_""blank"">" & HttpUtility.HtmlEncode(TitleTextBox.Text) & "</a>"
DOCTextBox.Text = "" & TitleTextBox.Text & ""
It sounds like you're trying to add quotes into the final string produced from the expression. In order to do that use a pair of double quotes "". For example
""
You can use triple quotes such as """