Morning Everyone,
using interop.word to do a find/replace in the header. It works but word, in it's great wisdom, adds spaces: 1 space to the beginning of each line after the first, 2-3 spaces at the end of each line.
So it looks like this but it should be completely left:
Sept 2, 2015
first name
last name
address
I am looking at the text of the header after replacement using headerfooter.range.text and there are no extra spaces so it has to be an auto-formatting issue that word is obsessed with.
Thanks for any help!
What I have tried with no luck:
paragraphformat.duplicate before and reset after
range.paragraphformat.spacebefore = 0
range.paragraphformat.spaceafter = 0
Dim info As String = Now.ToLongDateString & vbCrLf & Trim(PersonInformation.FullName) & vbCrLf & Trim(PersonInformation.Address)
info &= vbCrLf & Trim(PersonInformation.City & ", " & PersonInformation.State & " " & PersonInformation.Zip)
hf.Range.Find.Execute(TagToReplace, , , , , , , , , info)
UPDATED
Found the answer. It is how word/interop interprets vbcrlf when generating the doc. Using just vbcr did the trick. vblf also caused the inserted space. Incidentally, vbNewLine also caused the extra space to appear.
Found the answer. It is how word/interop interprets vbcrlf when generating the doc. Using just vbcr did the trick. vblf also caused the inserted space. Incidentally, vbNewLine also caused the extra space to appear.
Related
I'm adding a Title to a chart but I keep getting an extra space (white space) between the two strings when I use vbNewLine.
myChart.ChartTitle.Text = "Distance to Default for" & vbNewLine & compName
note compName is another string I previously define.
Most probably it's a text format issue. Try: vbCrLf, vbCr, vbLf or (digged up the doc) Environment.NewLine. One of the four should be OK.
"Cr" is "carriage return" while "Lf" is "Line Feed" (these terms come from old typewriter times). Some OS/text format/system/whatever uses both "driver characters", some use just one of these - the white space you saw was an unnecessary "cr" or "lf" if that makes sense
Try vbCrLf:
myChart.ChartTitle.Text = "Distance to Default for" & vbCrLf & compName
EDIT: Try vbCr since vbCrLf looks to be identical to vbNewline
myChart.ChartTitle.Text = "Distance to Default for" & vbCr & compName
helpful post. I have the same issue. "text" & vbnewline & "text" is two carriage returns instead of one...
vbVerticalTab worked well for me.
I don't understand what end result you're aiming for, but using vbNewLine will insert a carriage return.
If you're just trying to insert a space between the text and the value then one of the following would work:
myChart.ChartTitle.Text = "Distance to Default for " & compName
myChart.ChartTitle.Text = "Distance to Default for" & Chr(32) & compName
So here's my solution. Use chr(10) instead of vbNewLine and then do this:
compName = Replace(compName, vbCrLf, Chr(10))
It worked for me. Don't know if it'll work for everyone. Cheers.
You can also use vbVerticalTab or Chr(11). This is the same as hitting shift + enter if you were typing a Word document.
Interestingly, in the Official documentation, VBA Miscellaneous constants, it says this in the description:
Not useful in Microsoft Windows or on the Macintosh
Of which I disagree. The documentation says the same thing about vbFormFeed / Chr(12), but you can use that to easily insert page breaks.
I am currently trying to make a simple VBA program that would multiply two numbers together and then prompt a message box that says "The value of (variable1) and (variable 2) is (The answer).
Here is my current attempt that isn't perfect:
MsgBox prompt:=intmlt, Title:="The Value of " & intFirstNum & intSecondNum
The two big issues I have is how to you put a space inbetween the intFirstNum and intSecondNum? and How do you add an is to the end of that prompt?
It should be
"The Value of " & intFirstNum & " and " & intSecondNum & " is "
Per #ScottHoltzman
I used constants like vbLf , vbCrLf & vbCr in a MsgBox; it produces same output in a MsgBox (Text "Hai" appears in a first paragraph and a word "Welcome" appears in a next Paragraph )
MsgBox("Hai" & vbLf & "Welcome")
MsgBox ("Hai" & vbCrLf & "Welcome")
MsgBox("Hai" & vbCr & "Welcome")
I know vbLf , vbCrLf & vbCr are used for print and display functions.
I want to know the Difference between the vbLf , vbCrLf & vbCr constants.
Constant Value Description
----------------------------------------------------------------
vbCr Chr(13) Carriage return
vbCrLf Chr(13) & Chr(10) Carriage return–linefeed combination
vbLf Chr(10) Line feed
vbCr : - return to line beginning
Represents a carriage-return character for print and display functions.
vbCrLf : - similar to pressing Enter
Represents a carriage-return character combined with a linefeed character for print and display
functions.
vbLf : - go to next line
Represents a linefeed character for print and display functions.
Read More from Constants Class
The three constants have similar functions nowadays, but different historical origins, and very occasionally you may be required to use one or the other.
You need to think back to the days of old manual typewriters to get the origins of this. There are two distinct actions needed to start a new line of text:
move the typing head back to the left. In practice in a typewriter this is done by moving the roll which carries the paper (the "carriage") all the way back to the right -- the typing head is fixed. This is a carriage return.
move the paper up by the width of one line. This is a line feed.
In computers, these two actions are represented by two different characters - carriage return is CR, ASCII character 13, vbCr; line feed is LF, ASCII character 10, vbLf. In the old days of teletypes and line printers, the printer needed to be sent these two characters -- traditionally in the sequence CRLF -- to start a new line, and so the CRLF combination -- vbCrLf -- became a traditional line ending sequence, in some computing environments.
The problem was, of course, that it made just as much sense to only use one character to mark the line ending, and have the terminal or printer perform both the carriage return and line feed actions automatically. And so before you knew it, we had 3 different valid line endings: LF alone (used in Unix and Macintoshes), CR alone (apparently used in older Mac OSes) and the CRLF combination (used in DOS, and hence in Windows). This in turn led to the complications of DOS / Windows programs having the option of opening files in text mode, where any CRLF pair read from the file was converted to a single CR (and vice versa when writing).
So - to cut a (much too) long story short - there are historical reasons for the existence of the three separate line separators, which are now often irrelevant: and perhaps the best course of action in .NET is to use Environment.NewLine which means someone else has decided for you which to use, and future portability issues should be reduced.
Okay, so what i"m doing is creating a word file with my application with my labels and textboxes to create a receipt. In my word file, I am trying to write what is in a first name label beside what is in a last name label.
Here is the code I have, but I just seem to be missing something, because the word file keeps putting a huge gap between the first name and last name. I'm sure it's a very simple fix, but Google doesn't seem to be helping with what I am trying to describe.
printFile.WriteLine("Name: " & vbTab & FirstNameTextBox.Text + LastNameTextBox.Text)
I assume it must be the vbTab keyword.
which is fixed and cannot be changed.
That said you can manage a workaround as follows:
Public Const MyTab = " "
and in your code
printFile.WriteLine("Name: " & MyTab & FirstNameTextBox.Text + LastNameTextBox.Text)
I am using following code to read text from a .txt file. But I don't know how to
perform search within file and how to read a specific line in the text file, based on search.
Dim vrDisplay = My.Computer.FileSystem.ReadAllText(CurDir() & "\keys.txt")
MsgBox(vrDisplay)
For an example,
if I want to read the line that contains the word "Start", how to do that
Thanks.
Instead of reading all text, for efficiency's sake,
Open a FileStream for the file in question.
Create a StreamReader.
Loop through, calling ReadLine until either you find the end of the file, or the string contains "Start".
Edit: even if it's required that you keep the entire file in memory, you can still do the above by using a MemoryStream().
It isn't easy to tell from your post if it's the best possible solution, but one solution would be to use regular expressions to find all lines containing the word Start:
^.*\bStart\b.*$
matches an entire line that contains the complete word Start anywhere. It rejects Start as a part of a word, for example Starting won't be matched (that's what the \b word boundary anchors are for).
To use this in VB.NET:
Dim RegexObj As New Regex(
"^ # Start of line" & chr(10) & _
".* # Any number of characters (anything except newlines)" & chr(10) & _
"\b # Word boundary" & chr(10) & _
"Start # ""Start""" & chr(10) & _
"\b # Word boundary" & chr(10) & _
".* # Any number of characters (anything except newlines)" & chr(10) & _
"$ # End of line",
RegexOptions.Multiline Or RegexOptions.IgnorePatternWhitespace)
AllMatchResults = RegexObj.Matches(vrDisplay)
If AllMatchResults.Count > 0 Then
' Access individual matches using AllMatchResults.Item[]
Else
' Match attempt failed
End If