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

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.

Related

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

How to enter multiple string variables into a text box

I have a workbook with an userform that captures user input into string and single variables and I want to display a text consisting those variables into a text box on the same userform using new line and tab.
Example:
Dim dog as String 'rex
Dim years as Single '5
Dim owner As String 'Joe
Dim address as String '123 Sample Street
Dim value as Single '300.00
I would like to have a textbox on my form, that would display:
The dog's name is rex. He is 5 years old.
Owner: Joe
Address: 123 Sample Street
Treatment value: 300.00
I used
textbox.value = "The dog's name is " & dog & vbNewLine & vbNewLine & "Owner: " & owner & vbNewLine & "Address: " & address & vbNewLine & vbNewLine & "Treatment value: " & value
But after some time i will not be able to add another character to this line and I have plenty more variables mixed with text to come.
Can you suggest how this can be done?
Update: Resolved
Many thanks for your help.
Try
textbox.value = "The dog's name is " & dog & vbNewLine & vbNewLine
textbox.value = textbox.value & "Owner: " & owner & vbNewLine
textbox.value = textbox.value & "Treatment value: " & value & vbNewLine
Continue with other fields!
Try like this:
textbox.value = "The dog's name is " & dog & vbNewLine & vbNewLine & _
"Owner: " & owner & vbNewLine & _
"Address: " & address & vbNewLine & vbNewLine _
& "Treatment value: " & value
The " _" signs help you break the code into more lines. Note, that we have 2 signs - space and underscore there.
A small info - it is possibly good to consider passing the text as a .Text and not as .Value. Here is something good to read - Distinction between using .text and .value in VBA Access

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)

vba add more than one space in vba email body?

I am using vba in outlook to generate an email. I would like to find a way to move some text over to the right, about 100 pixels.
since I don't believe there is a way to incorporate css or styles in vba I am looking for a way to add more than one space to get the text moved over. however when I try space() function and " ", even if I repeat these codes a number of times it only ever gives me one space.
can someone please help me and show me what I need to do, thanks
"<br><br><br>" & "3PL & HAULAGE SUPPLIERS: " & " " & "<font size=""4.5"" face=""calibri"" color=""red"">" & "<b>" & EmailCount & "</b></font>" & vbNewLine & _
This will add 10 white spaces just before 3PL. You may need to adjust as the pixel distance will be relative to the font
TRIED AND TESTED
Sub test()
Dim WS As String
WS = "&nbsp"
For i = 1 To 10
WS = WS & "&nbsp"
Next i
Debug.Print "<br><br><br>" & WS & "3PL & HAULAGE SUPPLIERS: " & " " & "<font size=""4.5"" face=""calibri"" color=""red"">" & "<b>" & EmailCount & "</b></font>" & vbNewLine
End Sub

Specifying the location of an inlineshape in MS Word (VBA)

I've been trying to adapt the method shown here: http://support.microsoft.com/kb/246299 so that I can create a command button in word which will save the document and remove itself when clicked. I've been unable to figure out how to change the position of the button from the default of the top left of the first page however. Ideally I'd like the button to be generated at the end of the document and be centre aligned, or otherwise placed at the cursor position.
Any advice would be very much appreciated :)
Thank You.
My VB.NET project code so far:
Dim shp As Word.InlineShape
shp = wrdDoc.Content.InlineShapes.AddOLEControl(ClassType:="Forms.CommandButton.1")
shp.OLEFormat.Object.Caption = "Save To Disk"
shp.Width = "100"
'Add a procedure for the click event of the inlineshape
Dim sCode As String
sCode = "Private Sub " & shp.OLEFormat.Object.Name & "_Click()" & vbCrLf & _
"ActiveDocument.SaveAs(""" & sOutFile & """)" & vbCrLf & _
"On Error GoTo NoSave" & vbCrLf & _
"MsgBox ""Document Saved Successfully""" & vbCrLf & _
"Dim o As Object" & vbCrLf & _
"For Each o In ActiveDocument.InlineShapes" & vbCrLf & _
"If o.OLEFormat.Object.Name = ""CommandButton1"" Then" & vbCrLf & _
"o.Delete" & vbCrLf & _
"End If" & vbCrLf & _
"Next" & vbCrLf & _
"Exit Sub" & vbCrLf & _
"NoSave:" & vbCrLf & _
"MsgBox ""Document Failed To Save""" & vbCrLf & _
"End Sub"
wrdDoc.VBProject.VBComponents("ThisDocument").CodeModule.AddFromString(sCode)
wrdApp.Visible = True
As long as everything else is working just set the shp.left = 250 and shp.top = 1200
etc etc.
Just like in VB when you place a button. For more details on the exact calls you should reference this page: http://msdn.microsoft.com/en-us/library/office/hh965406%28v=office.14%29.aspx
But say to center a button you can set left to be the (doc.width - shape.width)
But word buttons allow for much more complex styling and setup.