I want to remove the line break that I have in a certain text. I check in this forum how to do it and there were several answers but no one works for me at least in powerpoint.
I saw one example with left method:
If Len(myString) <> 0 Then
If Right$(myString, 2) = vbCrLf Or Right$(myString, 2) = vbNewLine Then
myString = Left$(myString, Len(myString) - 2)
End If
End If
text = Left (text, number) gives me Type mismatch error
text = Left$ (text, number) gives me compile error: Type-declaration character does not match declared data type.
I also try to replace the line break with "" but it just did nothing. It didn't gave me an error but the line break was still there.
The line break that I am using is vbCrLf
Your problem is likely that versions of PPT since 2007 don't use VBCrLf as a paragraph-ending character. This explains which versions use what characters for line or paragraph ends:
Paragraph endings and line breaks
http://www.pptfaq.com/FAQ00992_Paragraph_endings_and_line_breaks.htm
It's from the PPT FAQ site that I maintain.
For the object in which you want to remove the vbCrLf, try using
myObj.TextFrame.TextRange.Replace vbCrLf, ""
If you want to do this to a string (not an object) you can try something like this:
Sub stripStrings()
Dim longString As String
Dim stringCopy As String
longString = "first paragraph" & vbCrLf & "second paragraph" & vbCrLf & "third paragraph" & vbCrLf
stringCopy = Replace(longString, vbCrLf, "")
MsgBox "longstring is now:" & vbCrLf & longString
MsgBox "stringcopy is:" & vbCrLf & stringCopy
End Sub
As you will see, this removes the line breaks. Adapt for your purpose...
edit As Steve Rindsberg pointed out, it may be that your version of Powerpoint is using something other than vbCrLf as the paragraph delimiting character. Here is some code to help you figure this out - for each shape with text in it, it will extract the text, showing all "control characters" (ASCII value < 32) as \xnn where nn is the value of the control character (so that vbCR will display as \x13 for example):
Sub displayControlCharacters()
Dim sh As Shape
Dim t As String
For Each sh In ActivePresentation.Slides(1).Shapes
If sh.TextFrame.HasText Then
sh.Select
t = sh.TextFrame.TextRange.Text
MsgBox "The shape contains: " & vbCrLf & escapeString(t)
End If
Next sh
End Sub
Function escapeString(t As String)
Dim ii As Integer
Dim r As String
For ii = 1 To Len(t)
If Asc(Mid(t, ii, 1)) > 31 Then
r = r + Mid(t, ii, 1)
Else
r = r + "\x" + Format(Asc(Mid(t, ii, 1)), "0")
End If
Next
escapeString = r
End Function
A simple test showed that in PowerPoint 2010 you do have just \x13 at the end of a paragraph...
Related
I am trying to remove a carriage return from a text box with formatted text:
For i = ThisShape.TextFrame2.TextRange.Runs.Count To 1 Step -1
ThisText = ThisShape.TextFrame2.TextRange.Runs(i).Text
ThisText = Replace(ThisText, Chr(13), "")
ThisShape.TextFrame2.TextRange.Runs(i).Text = ThisText
Next i
For some odd reason, the text box remains with a carriage return.
Any help would be appreciated.
David
The CLEAN function is rarely used, thus often forgotten.
CLEAN removes non printable characters (ASCII 0 through 31) from the given text. You can use it in VBA:
myStr = Application.WorksheetFunction.Clean(myStr)
...or as an Excel worksheet function:
=CLEAN(A1)
Example:
Sub cleanDemo()
Dim mystr As String
'populate string with linefeeds and carriage returns.
'note: vbLf = chr(10): vbCr = chr(13) : vbCrLf = vbCr+vbLf
mystr = "A" & vbLf & "B" & vbCr & "C" & vbCrLf & "D"
'display string and it's length
MsgBox mystr, , "myStr is " & Len(mystr) & " char long."
'remove ASCII 0 through 31
mystr = Application.WorksheetFunction.Clean(mystr)
'display string and it's length
MsgBox mystr, , "myStr is " & Len(mystr) & " char long."
End Sub
...returns:
List all characters within string:
The procedure lists the characters and their codes found in the specified string:
Sub showAllChars(strIn as String)
'Lists all ASCII character codes used in the specified string.
'Adaptable to UNICODE by changing Asc to AscW and Chr to ChrW
Dim strIn As String, c As String, p As Long
For p = 1 To Len(strIn)
c = Mid(strIn, p, 1)
Debug.Print "Pos#" & p & ": Chr(" & Asc(c) & ") =[";
Select Case Asc(c)
Case 10
Debug.Print "LF]", ;
Case 13
Debug.Print "CR]", ;
Case Else
Debug.Print c & "]", ;
End Select
If p / 5 = p \ 5 Then Debug.Print
Next p
Debug.Print "Done. (Length=" & Len(strIn) & ")"
End Sub
Example Output:
Pos#1: Chr(65) =[A] Pos#2: Chr(66) =[B] Pos#3: Chr(13) =[CR]
Pos#4: Chr(67) =[C] Pos#5: Chr(63) =[?] Pos#6: Chr(68) =[D]
Pos#7: Chr(17) =[] Pos#8: Chr(69) =[E] Pos#9: Chr(70) =[F]
Pos#10: Chr(63) =[?] Pos#11: Chr(71) =[G] Pos#12: Chr(10) =[LF]
Pos#13: Chr(72) =[H] Pos#14: Chr(73) =[I] Done. (Length=14)
More Information:
Office.com : CLEAN Function (Excel/VBA)
MSDN : WorksheetFunction.Clean Method (Interop)
ascii.cl : ASCII Codes Tables
MSDN : Chr & ChrW Functions (Interop)
MSDN : Asc & AscW Functions (Interop)
Wikpedia: ASCII
Wikpedia : Control (aka Non-Printable) Characters
I am using VBA in microstation to come up with a simple find & replace program. I keep getting a type mismatch where my & Text_Find and & Text Replace is. Any help would be greatly appreciated.
Sub Main()
Dim Find_text() As String
Dim Replace_text() As String
Find_text = Split("20.50 35.43", " ")
Replace_text = Split("12.5 43.55", " ")
' Start a command
CadInputQueue.SendKeyin "MDL KEYIN FINDREPLACETEXT,CHNGTXT CHANGE DIALOGTEXT"
For i = 0 To UBound(Find_text)
For j = 0 To UBound(Replace_text)
MsgBox Find_text
MsgBox Replace_text
CadInputQueue.SendKeyin "FIND DIALOG SEARCHSTRING" & Find_text
CadInputQueue.SendKeyin "FIND DIALOG REPLACESTRING" & Replace_text
CadInputQueue.SendKeyin "CHANGE TEXT ALLFILTERED"
Next
Next
End Sub
Find_text and Replace_text are both arrays
You will have to specify the element of the array to use
Probably Find_text(i) Replace_text(j)
I have a csv file, and I need a VBA function that adds quotes to every string value in the file, so that something like
vertical,device_name,value
mobile,Apple iPad,0
mobile,HTC,3
looks like
"vertical","device_name","value"
"mobile","Apple iPad",0
"mobile","HTC",3
What I have found until now is a macro,
Sub QuotesAroundText()
Dim c As Range
For Each c In Selection
If Not IsNumeric(c.Value) Then
c.Value = """" & c.Value & """"
End If
Next c
End Sub
that does almost exactly what I need - it add the quotes, but not to string, but to excel cells. That means, that this macro does work correctly in a xlsx file, but not in a csv file.
So I need a vba code that adds quotes not to cells, but to string, that are between commas.
By emulating a string builder I was able to process a CSV file with 59,507 Rows x 9 Columns in just over 3 seconds. This process is much faster that standard concatenation.
This function was modified from my answer to Turn Excel range into VBA string
Test
Sub TestProcessCSVFile()
Dim s As String
Dim Start: Start = Timer
ProcessCSVFile "C:\Users\Owner\Downloads\SampleCSVFile_53000kb.csv", "C:\Users\Owner\Downloads\_temp.csv"
Debug.Print "Execution Time: "; Timer - Start; "Second(s)"
End Sub
Code
Sub ProcessCSVFile(OldFile As String, NewFile As String)
Dim Data As String, text As String
Dim vCell
Dim length As Long
Open OldFile For Binary As #1
Data = Space$(LOF(1))
Get #1, , Data
Close #1
text = Space(Len(Data) * 1.5)
For Each vCell In Split(Replace(Data, vbCrLf, "," & vbCrLf & ","), ",")
If Len(vCell) + length + 5 > Len(text) Then text = text & Space(Len(Data) * 0.1)
If vCell = vbCrLf Then
Mid(text, length, 1) = vbCrLf
ElseIf IsNumeric(vCell) Then
Mid(text, length + 1, Len(vCell) + 1) = vCell & ","
length = length + Len(vCell) + 1
Else
Mid(text, length + 1, Len(vCell) + 3) = Chr(34) & vCell & Chr(34) & ","
length = length + Len(vCell) + 3
End If
Next
Open NewFile For Output As #1
Print #1, Left(text, length - 1)
Close #1
End Sub
Results
Read the text file in using line input, then taylor the following process to your needs:
Sub y()
a = "a,b,c,d"
'Split into 1d Array
b = Split(a, ",", , vbTextCompare)
'for each element in array add the quotes
For c = 0 To UBound(b)
b(c) = """" & b(c) & """"
Next c
'join the product up
d = Join(b, ",")
'Print her out
Debug.Print d
End Sub
use workbooks.opentext filename:="your csv file with path",
It will open the csv and separate them into cells,
then apply your macro and save again as csv
Interesting problem here. This line of code works through multiple iterations until it reaches a point where it throws an Run-time error 91 at me: "Object Variable or With block variable not set". This is occurring in a function designed to find a deal number. The entire program is an end of day email generation program that sends attachments to various different counter-parties. The error occurs on the ** line. For additional color, temp deal is not empty when execution is attempted. There doesn't appear to be any extraneous trailing or leading spaces either. Thanks in advance!
Function getPDFs(cFirm As Variant, iFirm As Variant, row_counter As Variant, reportsByFirm As Worksheet, trMaster As Worksheet, trSeparate As Variant, trName As Variant, reportDate As Variant) As String
dealCol = 1
Dim locationArray() As String
Dim DealArray() As String
cDes = "_vs._NY"
iDes = "_vs._IC"
filePath = "X:\Office\Confirm Drop File\"
dealNum = reportsByFirm.Cells(row_counter, dealCol)
FileType = ".pdf"
If InStr(1, dealNum, "-") > 0 Then
DealArray() = Split(dealNum, "-")
tempDeal = DealArray(LBound(DealArray))
Else
tempDeal = dealNum
End If
'Finds deal location in spread sheet for further detail to obtain file path
**trLocation = trMaster.Columns(2).Find(What:=tempDeal).Address
locationArray() = Split(trLocation, "$")
trRow = locationArray(UBound(locationArray))
'Formats client names for 20 characters and removes punctuation (".") in order to stay within convention of file naming
cFirmFormatted = Trim(Left(cFirm, 20))
iFirmFormatted = Trim(Left(iFirm, 20))
'Finds clearing method
clMethod = trMaster.Cells(trRow, 6).Value
Select Case clmethod
Case "Clport"
'Prevents naming convention issues with punctuations in the name
If InStr(1, cFirmFormatted, ".") > 0 Then
cFirmFormatted = Replace(cFirmFormatted, ".", "")
End If
getPDFs = filePath & cFirmFormatted & "\" & reportDate & "_" & dealNum & "_" & cFirmFormatted & cDes & FileType
Case "ICE"
If InStr(1, iFirmFormatted, ".") > 0 Then
iFirmFormatted = Replace(iFirmFormatted, ".", "")
End If
getPDFs = filePath & iFirmFormatted & "\" & reportDate & "_" & dealNum & "_" & iFirmFormatted & iDes & FileType
End Select
End Function
Your code assumes that trLocation is always found, if it isn't found then you will receive an error because you don't have a range to return the .Address property for.
Try testing the result first:
Dim testLocation As Excel.Range
Set testLocation = trMaster.Columns(2).Find(tempDeal)
If Not testLocation Is Nothing Then
trLocation = testLocation.Address
'// Rest of code here...
Else
MsgBox "Cannot find """ & tempDeal & """!"
Exit Function
End If
I have mails like this :
Hello,
Please note we did ... at 16h15
Actions done: Rebuilding etc
sincerely
Mr.
The actions change in every mail and what I want is to insert the action in my Excel. The problem is that I don't know how to get the "carriage return" (idk if this is the right name, this is what traduction gave me).
What I find in internet is that vbLfChr(10) is the "carriage return".
What I tried is to find the beginning :
TechnicPosition= InStr(1, .Body, "Actions done: ")
TechnicAction= Mid(.Body, TechnicPosition, 14) ' first char from the action
But I can't get the last char (first "carriage return" from TechnicAction).
I tried many things like : InStr(TechnicPosition, .Body, vbCrLf)
My question : how to get a sentence that begin from a word to a "carriage return" (the first that comes after the beginning word) ?
The carriage return in the email body is usually vbNewline
This is how I usually do it
Sub Sample()
Dim sBody As String
Dim MyAr
Dim i As Long
'
'~~> Rest of your code
'
sBody = oMail.Body
'~~> For testing purpose
'sBody = "Hello," & vbNewLine & vbNewLine
'sBody = sBody & "Please note we did ... at 16h15" & vbNewLine & vbNewLine
'sBody = sBody & "Actions done: Rebuilding etc" & vbNewLine & vbNewLine
'sBody = sBody & "Sincerely"
'~~> Split the email body on vbnewline and store it in array
MyAr = Split(sBody, vbNewLine)
'~~> Loop through array
For i = LBound(MyAr) To UBound(MyAr)
'~~> Check if the line has "Actions done:"
If InStr(1, MyAr(i), "Actions done:") Then
'~~> This would give you "Rebuilding etc"
'~~> Split the line on "Actions done:"
'~~> You will get an array. Ar(0) will have "Actions done:"
'~~> And Ar(1) will have what you are looking for so we use
'~~> Split()(1) directly to access that item
MsgBox Split(MyAr(i), "Actions done:")(1)
Exit For
End If
Next i
End Sub
Edit
ANOTHER WAY
Try this:
TechnicPosition = InStr(1, .Body, "Actions done: ")
TechnicEndPosition = InStr(TechnicPosition, .Body, Chr(10))
TechnicAction = Mid(.Body, TechnicPosition + 14, TechnicEndPosition - TechnicPosition - 14)
Loop through the body to see what the character is:
For i = 1 To Len(.Body)
If Not Mid(.Body, i, 1) Like "[A-Za-z0-9,'?!"".:]" Then
Debug.Print Asc(Mid(.Body, i, 1))
End If
Next
Once you've found the Asc() value, split the body on it and return the second index:
TechnicAction = Split(.Body, Asc(10))(1) '// Or whatever Asc() is required