Write multiple lines with quotes in a text file vb.net - vb.net

Hi Is there any easier way to write multiple lines in a file which has things like quotes in and other things like that or is the only way to do it like this
Dim objwriter As New System.IO.StreamWriter(AppsDir & "EthIPChanger.bat")
objwriter.WriteLine("#echo off")
objwriter.WriteLine("netsh interface ip set address name=""" & "Local Area Connection""" & " static " & TB_EthIPAddress.Text & " " & TB_EthSubnetMask.Text & " " & TB_EthDefaultGateway.Text & " 1")
objwriter.WriteLine("netsh interface ip set dns """ & "Local Area Connection""" & " static " & TB_EthDNS1.Text)
objwriter.WriteLine("ipconfig /all > """ & AppsDir & "NetworkInfo.txt""")
objwriter.WriteLine("echo hi > """ & AppsDir & "CheckLen.txt""")
objwriter.Close()
I know if you are using python you can do """ and then do anything inside that and end it with """
Does anything like that exist in vb.net?
thanks

If you use objwriter.Write - then you can supply the vbcrlf yourself - and then you can put multiple 'lines' in one write statement.
For example:
Dim str2write As string
str2write = "firstline" and Chr(34) & Chr(34) & vbcrlf
str2write &= Chr(34) & "second line" and Chr(34) & vbcrlf & vbcrlf
objwriter.write(str2write)
objwriter.close()

You can try with StringBuilder:
Dim objwriter As New System.IO.StreamWriter(AppsDir & "EthIPChanger.bat")
Dim textToWrite As New System.Text.StringBuilder
With textToWrite
.Append("#echo off")
.AppendFormat("netsh interface ip set address name={0}Local Area Connection{0} static {1} {2} {3} 1", Chr(34), TB_EthIPAddress.Text, TB_EthSubnetMask.Text, TB_EthDefaultGateway.Text)
.AppendFormat("netsh interface ip set dns {0}Local Area Connection{0} static {1}", Chr(34), TB_EthDNS1.Text)
.AppendFormat("ipconfig /all > {0}{1}{2}{0}", Chr(34), AppsDir, TB_EthDNS1.Text)
.AppendFormat("echo hi > {0}{1}{2}{0}", Chr(34), AppsDir, CheckLen.Text)
End With
objwriter.WriteLine(textToWrite.ToString)
objwriter.Close()

Related

Start CMD Window Minimized?

I use the following code to start a process and then wait for it to exit. It works, but the CMD window is full size. I'd rather have it start at least minimized or possibly hidden. Thanks in advance!
Edit: If I am doing this in a method does not allow me to change the method in which to specify either hidden or minimized, how do I go about this?
mProcessParameters = New StringBuilder
mProcessParameters.Append(" -input:" & Chr(34) & "\\server\waybill\Waybill files\")
mProcessParameters.Append(txt_Year.Text & " Data\Interim Data Processing\")
mProcessParameters.Append(mBPGM_FileNames(mLooper2) & mInFileNameSuffix & Chr(34) & " ")
mProcessParameters.Append(" -config:" & Chr(34) & "\\server\waybill\Waybill files\")
mProcessParameters.Append(txt_Year.Text & " Data\Interim Data Processing\")
mProcessParameters.Append(mBPGM_FileTypes(mLooper2) & mCfgFileNameSuffix & Chr(34))
mProcess.Start(My.Settings.BatchProg, mProcessParameters.ToString).WaitForExit()

Microsoft.Office.Interop - ReplaceText (with) *NewLine* in Word.Table

I am having an application that uses Microsoft Word 2016 / the Interop interface to call the replace function. This way the application replaces some texts like 'Customername' to the actual name of the customer. Surely this is not the best way to do so, but thats how the application was build back in the days. Now I have to do some adjustments to the formatting facing a problem with the named function.
One of the Texts getting replaced is "[TELNR]" , beeing replaced with all known numbers of the customer, one number per line. Inserting New Lines as vbCr works great on a blank page as you can see top of the screenshot. The same called function will not put a new lines into a table (second page on the left) and won't even touch a textbox (second page on the right).
I have tried passing Line breaks as ^p, \n\r, \n, vbClRf / vbNewLine, vbCr. None of these will work.
I haven't found anything usefull on google or SO. Do you have ideas or a reason what might cause or even solve the problem?
#Region "Sample Strings"
Dim sample1 As String = "t:022220000000" & vbNewLine & "h:022221111111" & vbNewLine & ":0222222222222" & vbNewLine & "f:022223333333"
Dim sample2 As String = "t:022220000000" & vbCr & "h:022221111111" & vbCr & ":0222222222222" & vbCr & "f:022223333333"
Dim sample3 As String = "t:022220000000" & "^p" & "h:022221111111" & "^p" & ":0222222222222" & "^p" & "f:022223333333"
Dim sample4 As String = "t:022220000000" & "\n" & "h:022221111111" & "\n" & ":0222222222222" & "\n" & "f:022223333333"
Dim sample5 As String = "t:022220000000" & "\n\r" & "h:022221111111" & "\n\r" & ":0222222222222" & "\n\r" & "f:022223333333"
Dim sample6 As String = "t:022220000000" & vbCrLf & "h:022221111111" & vbCrLf & ":0222222222222" & vbCrLf & "f:022223333333"
#End Region
Private Sub replaceText(file As Microsoft.Office.Interop.Word.Document, find As String, replaceWith As String)
// First line was commented out for the ^p try
If replaceWith.Contains("^") Then replaceWith = replaceWith.Replace("^", "")
file.Content.Find.Execute(FindText:=find, ReplaceWith:=replaceWith, Replace:=Word.WdReplace.wdReplaceAll)
End Sub
The following was function was used to insert the textbox.

Creating data entry form using button VBA

I am trying to create a basic data entry form, however, it is turning into more trouble than I anticipated.. I have the form created, now I am just trying to INSERT the data into the DB (TEST). I am receiving an "Object Required" error. Any suggestions? All of the txt boxes are verified to be correct. This is all being done through Access VBA
Private Sub Command28_Click()
Dim sSQL As String
Set sSQL = "INSERT INTO TEST (Carrier_Ent_ID, Row_Insert_TS, Row_Update_TS,
Row_Insert_User_ID, Row_Update_User_ID, Carrier_Ent_Name, Active)
VALUES (" & Me.txtENTID & "," & Me.txtDate & "," & Me.txtDate & "," &
Me.cmboUserID & "," & Me.cmboUserID & "," & Me.txtENTNAME & ","
& Me.Txtactive & "); "
DoCmd.RunSQL.sSQL
End Sub
From what I can see there's a couple of mistakes in the code.
You only use SET when setting a reference to an object - sSQL is a text string.
DoCmd.RunSQL shouldn't have a full-stop after RunSQL- just the text string.
If Me.txtENTNAME is a text string it should have an apostrophe before and after it.
Private Sub Command28_Click()
Dim sSQL As String
'No Set & ' before and after Me.txtENTNAME (assuming it's text).
sSQL = "INSERT INTO TEST (Carrier_Ent_ID, Row_Insert_TS, Row_Update_TS, " & _
"Row_Insert_User_ID, Row_Update_User_ID, Carrier_Ent_Name, Active) " & _
"VALUES (" & Me.txtENTID & "," & Me.txtDate & "," & Me.txtDate & "," & _
Me.cmboUserID & "," & Me.cmboUserID & ",'" & Me.txtENTNAME & "'," & Me.txtActive & "); "
'No full stop after RunSQL.
DoCmd.RunSQL sSQL
End Sub

String sets its Value to "False"

So, I'm currently working on a program that should create Lua-Scripts for the Unitale-Engine. I added a "New Project" button which should create a new project file when you click it. I want this file to contain the default project-file-text. So i did this:
Dim File1 As String = "monster{" & vbNewLine & "COMMENTS=Ribbit, ribbit.;Croak, croak.;Hop, hop.;Meow." & vbNewLine & "COMMANDS=Compliment;Threaten" & vbNewLine & "DEFAULT_SPRITE=None" & vbNewLine & "NAME=Froggit" & vbNewLine & "HP=10" & vbNewLine & "ATK=1" & vbNewLine = "DEF=0" & vbNewLine & "CHECK_MESSAGE=Life is difficult for this enemy." & vbNewLine & "HandleAttack=" & vbNewLine & "OnDeath=" & vbNewLine & "HandleCustomCommand=" & vbNewLine & "}"
Dim File2 As String = vbNewLine & "encounter{" & vbNewLine & "DEFAULT_MUSIC=Fight" & vbNewLine & "ENCOUNTER_TEXT=Froggit hops close." & vbNewLine & "NEXTWAVES=1" & vbNewLine & "WAVETIMER=6" & vbNewLine & "ARENASIZE=155;130" & vbNewLine & "ENEMIES=Froggit" & vbNewLine & "ENEMYPOSITIONS=0;0" & "EncounterStarting=" & vbNewLine & "EnemyDialogueStarting=" & vbNewLine & "EnemyDialogueEnding=" & vbNewLine & "DefenseEnding=" & vbNewLine & "HandleItem=" & vbNewLine & "HandleSpare=" & vbNewLine & "EnteringState=" & vbNewLine & "Update=" & vbNewLine & "}"
Dim File3 As String = vbNewLine & "waves{" & vbNewLine & "1=Do Nothing" & vbNewLine & "}" & vbNewLine & "end"
My.Computer.FileSystem.WriteAllText(My.Computer.FileSystem.SpecialDirectories.MyDocuments & "\UnitaleMaker\Projects\" & PJNBox.Text & ".umproj", File1 & File2 & File3, False)
Yes, that is a lot of text. The problem is, that File1 always sets its value to "False" when it is declared. I thought that is because the string is too long, so I split it to 3 parts, but it doesn't work.
Any help would be apprechiated!
~ Mika // OneByte_
Starting with VB 14 (Visual Studio 2015), you can span String literals across multiple lines. VB 14 also adds String interpolation, which makes for much cleaner code. You could write your code like the following which would have kept you from having made your mistake of & vbNewLine = "DEF=0" &and would overall make the code easier to read.
I am also assuming you forgot a newline in "ENEMYPOSITIONS=0;0" & "EncounterStarting=" and I have indented the parts between the {}. I combined File1, File2, and File3 back into a single string since you stated you broke them up in the question, I assumed you'd rather have it all in one string.
Dim File1 As String = "monster{
COMMENTS = Ribbit, ribbit.;Croak, croak.;Hop, hop.;Meow.
COMMANDS=Compliment;Threaten
DEFAULT_SPRITE=None
NAME=Froggit
HP=10
ATK=1
DEF=0
CHECK_MESSAGE=Life Is difficult For this enemy.
HandleAttack=
OnDeath=
HandleCustomCommand=
}
encounter{
DEFAULT_MUSIC = Fight
ENCOUNTER_TEXT =Froggit hops close.
NEXTWAVES=1
WAVETIMER=6
ARENASIZE=155;130
ENEMIES=Froggit
ENEMYPOSITIONS=0;0
EncounterStarting=
EnemyDialogueStarting=
EnemyDialogueEnding=
DefenseEnding=
HandleItem=
HandleSpare=
EnteringState=
Update=
}
waves{
1 =Do Nothing
}
End"
My.Computer.FileSystem.WriteAllText($"{My.Computer.FileSystem.SpecialDirectories.MyDocuments}\UnitaleMaker\Projects\{PJNBox.Text}.umproj", File1, False)
Please learn to use string.format.
Also I changed & to new line in file2:
"ENEMYPOSITIONS=0;0" & "EncounterStarting="
I just assumed you forgot to add new line there.
Alss Environment.NewLine is the best option for new lines as it is universal.
Dim File1 As String = String.Format("monster{{{0}COMMENTS=Ribbit, ribbit.;Croak, croak.;Hop, hop.;Meow.{0}COMMANDS=Compliment;Threaten{0}DEFAULT_SPRITE=None{0}NAME=Froggit{0}HP=10{0}ATK=1{0}DEF=0{0}CHECK_MESSAGE=Life is difficult for this enemy.{0}HandleAttack={0}OnDeath={0}HandleCustomCommand={0}}}", Environment.NewLine)
Dim File2 As String = String.Format("{0}encounter{{{0}DEFAULT_MUSIC=Fight{0}ENCOUNTER_TEXT=Froggit hops close.{0}NEXTWAVES=1{0}WAVETIMER=6{0}ARENASIZE=155;130{0}ENEMIES=Froggit{0}ENEMYPOSITIONS=0;0{0}EncounterStarting={0}EnemyDialogueStarting={0}EnemyDialogueEnding={0}DefenseEnding={0}HandleItem={0}HandleSpare={0}EnteringState={0}Update={0}}}", Environment.NewLine)
Dim File3 As String = String.Format("{0}waves{{{0}1=Do Nothing{0}}}{0}end", Environment.NewLine)
My.Computer.FileSystem.WriteAllText(My.Computer.FileSystem.SpecialDirectories.MyDocuments & "\UnitaleMaker\Projects\" & PJNBox.Text & ".umproj", File1 & File2 & File3, False)

In VBA OutLook, how do I string together Namespace property specifiers? I'm trying to filter inbox by subject line for both unread and read emails.

I'm trying to parse through my inbox by subject line for both unread and read emails.
However, when I parse through subject line only, the filter returns only read emails.
'Filters inbox by subject line (only returns READ emails)
strFilter = "#SQL=" & Chr(34) & "urn:schemas:httpmail:subject" & Chr(34) & " LIKE '%M4 Vitals Grade%'"
'Returns Entire Inbox (READ AND UNREAD emails)
strFilter2 = "#SQL=" & Chr(34) & "urn:schemas:httpmail:read" & Chr(34) & "=True"
'Does not work...
strFilter3 = "#SQL=" & Chr(34) & "urn:schemas:httpmail:subject" & Chr(34) & " LIKE '%M4 Vitals Grade%' WHERE " & Chr(34) & "urn:schemas:httpmail:read" & Chr(34) & " = true"
Set filteredItems = objFolder.Items.Restrict(strFilter)
Please let me know if you need more information! Thank you
Try the following one:
strFilter =" & Chr(34) & "urn:schemas:httpmail:subject" & Chr(34) & " LIKE '%M4 Vitals Grade%' AND [UnRead] = True"