I am trying to replace the ascii character in a word file to its respected hexadecimal value but the problem is only uppercase characters that exist are replacing with proper values and lowercase characters are getting replaced with the uppercase entities.
I have tried this,
Dim var As String
Dim char1 As String = "!#$%&'()*+,-./0123456789:;<=>?#ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~ " & vbCrLf
Dim values As Char() = objDoc.Range.Text
For Each letter As Char In values
If char1.Contains(letter) Then
Else
var = Convert.ToString(Convert.ToInt32(letter), 16)
If var.Length = 1 Then
Dim FindObject2 As Word.Find = objDoc.Content.Find
With FindObject2
.ClearFormatting()
.Text = letter
.Replacement.ClearFormatting()
.Replacement.Text = "�" & StrConv(var, VbStrConv.None) & ";"
.Execute(Replace:=Word.WdReplace.wdReplaceAll)
End With
ElseIf var.Length = 2 Then
Dim FindObject2 As Word.Find = objDoc.Content.Find
With FindObject2
.ClearFormatting()
.Text = letter
.Replacement.ClearFormatting()
.Replacement.Text = "�" & StrConv(var, VbStrConv.None) & ";"
.Execute(Replace:=Word.WdReplace.wdReplaceAll)
End With
ElseIf var.Length = 3 Then
Dim FindObject2 As Word.Find = objDoc.Content.Find
With FindObject2
.ClearFormatting()
.Text = letter
.Replacement.ClearFormatting()
.Replacement.Text = "�" & StrConv(var, VbStrConv.None) & ";"
.Execute(Replace:=Word.WdReplace.wdReplaceAll)
End With
End If
End If
Next
Exit For
Next
Catch ex As Exception
End Try
objDoc.Save()
objDoc.Close()
objapp.Quit()
MsgBox("Process Completed")
Any help will be really appreciated.
Please, avoid the usage of past decade VB6 methods while you are programming in VB.NET, methods such as HEX and ASC can be replaced with the methods provided by the Convert Class.
var = Convert.ToString(Convert.ToInt32(letter), 16)
And place your code here:
If char1.Contains(letter) Then
' Here the instructions to do when a character is found...
Else
An Example:
Dim AscChars As Char() =
"!#$%&'()*+,-./0123456789:;<=>?#ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~ " _
& Environment.NewLine
Dim HexValue As String = String.Empty
Dim sb As New System.Text.StringBuilder
For Each c As Char In AscChars
HexValue = Convert.ToString(Convert.ToInt32(c), 16)
sb.Clear()
sb.AppendLine(String.Format("ASC: {0}", CStr(c)))
sb.AppendLine(String.Format("HEX: {0}", HexValue))
MessageBox.Show(sb.ToString, "Character conversion")
Next c
I guess you never do anything with a found char in charlist because:
If char1.Contains(letter) Then
Else
Related
For Each cell In rng
workSheetName = Format(SaturdayIsComing(), "mm-dd-yyyy") & " " & cell.Value
If WorksheetExists(workSheetName) Then
Dim localRange, localCell As Range
Set localRange = Worksheets(workSheetName).Range("D8:D19")
Dim contents As Variant
contents = ""
Dim firstLine As Boolean
firstLine = True
For Each localCell In localRange
If Len(localCell.Value) > 0 Then
If firstLine Then
contents = contents & localCell.Value & Chr(11)
Else
contents = contents & Chr(9) & Chr(9) & Chr(9) & localCell.Value & Chr(11)
End If
Else
contents = fixString(contents)
End If
If Len(contents) > 0 Then
firstLine = False
End If
Next localCell
For Each cc In wDoc.SelectContentControlsByTag(cell.Value & "Notes")
If Len(contents) > 0 Then
cc.Range.Text = fixString(contents)
Else
cc.Range.Text = "No Issues Found"
End If
Next
Else
errorCodesString = errorCodesString & cell.Value & ":"
End If
Next cell
Output to Word
Forgot to terminate the meeting
This is a test message\'s
If my cell contains a ' then I get an error saying
One of the values passwed to this method or property is incorrect
I know a ' is a comment in VBA. How do I go around this while preserving the notes that someone had added to the Excel cell?
You need to write a piece of code to search for quotes, either the single (') or double (") variety and either add a backslash before them OR double the character so '' in place of ' and "" in place of " and run this on contents before assigning it to cc.Range.Text.
This routine can also check for other instances of incorrect strings and fix them.
Something like this would do:
Function fixString(ByVal strIn As Variant) As String
Dim i As Integer
Const strIllegals = "\'"""
For i = 1 To Len(strIllegals)
strIn = Replace(strIn, Mid$(strIllegals, i, 1), "\" & Mid$(strIllegals, i, 1))
Next i
fixString = strIn
End Function
Try changing cell.Value to Replace(cell.Value, "'", "")
Or is it contents that has the apostrophe in it? A bit confusing.
Try changing contents to Replace(contents , "'", "")
I tried the code below but it only give me the first 6 digit number. How do I edit this to get multiple 6 digit numbers from the same string?
Function SixDigit(S As String, Optional index As Long = 0) As String
Dim RE As Object
Set RE = CreateObject("vbscript.regexp")
With RE
.Pattern = "(?:\b|\D)(\d{6})(?:\b|\D)"
.Global = True
SixDigit = .Execute(S)(index).submatches(0)
End With
End Function
Use the code below, source:
https://msdn.microsoft.com/en-us/library/tdte5kwf(v=vs.84)
Function RegExpTest(patrn, strng)
Dim regEx, Match, Matches, s
' Create the regular expression.
Set regEx = New RegExp
regEx.Pattern = patrn
regEx.IgnoreCase = True
regEx.Global = True
' Do the search.
Set Matches = regEx.Execute(strng)
' Iterate through the Matches collection.
s = ""
For Each Match in Matches
s = s & "Match found at position "
s = s & Match.FirstIndex & ". "
s = s & "Match Value is '"
s = s & Match.Value & "'."
s = s & vbCRLF
Next
RegExpTest = s
End Function
MsgBox(RegExpTest("is.", "IS1 is2 IS3 is4"))
I wanted to insert some texts(new line) in between existing texts in a textbox (multiline = true).
Example: (Textbox1.text's value is written below)
Name: Name of Client
DOB: 11/11/11
>>>THIS IS WHERE I WHAT TO INSERT THE VALUE OF TEXTBOX2.TEXT
Hospitalization: No
Serial Number: 12345678
Private Sub cmdTransfer_Click()
Dim SearchNote As Integer, SearchThis As String, tx2 As String
If cb9.Value = True Then
tx2 = "ADDRESS: " & vbTab & text2.Text & vbCrLf
End If
SearchThis = "Hospitalization"
SearchNote = InStr(Textbox1.Text, SearchThis)
If SearchNote Then
With textbox1
.SetFocus
.SelStart = SearchNote
.Text = .Text & .SelStart & tx2
End with
End If
End Sub
What I'm doing in my code is I'm getting the number of characters before the "Hospitalization" so that I can insert the value of Textbox2 before it. I dont know how to do that tho. Please help.
Thanks!
I believe the code you are looking for is this:
Left(SearchNote, InStr(1, SearchNote, "Hospitalization") - 1) & "new text to insert" & Mid(SearchNote, InStr(1, SearchNote, "Hospitalization"))
Left will take the first few letters up to the starting point of "Hospitalization". Then you insert the new string (possible with a new line before and after with & chr(10) &). Then you add with Mid everything after "Hospitalization".
Since I don't have a sample copy of your spreadsheet, there is a chance that one/some of my variables might be different. If you find problems with any of these, check all of the vars.
Solution #1: Create module and add this function:
Function addText(txtBox As String, addString As String)
Dim endIndex As Long
Dim SearchThis As String
Dim input1, input2, input3 As String
SearchThis = "Hospitalization"
' Get index of Hospitalization
endIndex = InStr(1, txtBox, SearchThis) - 1
If endIndex > 0 Then
input1 = Mid(txtBox, 1, endIndex)
input2 = addString & vbNewLine
input3 = Mid(txtBox, endIndex, Len(txtBox))
' Return with added text
addText = CStr(input1 & input2 & input3)
End If
End Function
then call in your button to update your text box:
Private Sub cmdTransfer_Click()
Dim tx2 As String
If cb9.Value = True Then
tx2 = "ADDRESS: " & vbTab & text2.Text & vbNewLine
Else
' Stop if there is nothing to add
End
End If
If textbox1.Value <> vbNullString Then
textbox1.Value = addText(textbox1.Value, tx2)
End If
End Sub
Solution #2: Call everything from within your button:
Private Sub cmdTransfer_Click()
Dim endIndex As Long
Dim SearchThis As String
Dim input1, input2, input3 As String
Dim txtBox As String, tx2 As String
'set tx2
If cb9.Value = True Then
tx2 = "ADDRESS: " & vbTab & text2.Text & vbNewLine
Else
' Stop if nothing to add
End
End If
If textbox1.Value <> vbNullString Then
' set txtBox variable
txtBox = textbox1.Value
Else
' Avoid Error if text box is null
End
End If
SearchThis = "Hospitalization"
' Get index of Hospitalization
endIndex = InStr(1, txtBox, SearchThis) - 1
If endIndex > 0 Then
input1 = Mid(txtBox, 1, endIndex)
input2 = tx2 & vbNewLine
input3 = Mid(txtBox, endIndex, Len(txtBox))
textbox1.Value = input1 & input2 & input3
End If
End Sub
What i would do is split text1 into an array then just add the text in the middle, mainString is text1, midStr is text2:
Dim mainStr as String, midStr as String, ArreStr() as String
mainStr=text1.text:midStr=text2.text
ArreStr=Split(mainStr,VBNewLine)
text1.text=ArreStr(0) & vbnewline & midStr & vbnewline & ArreStr(1)
I am writing a macro that recognizes the RGB value of a cell and then passes it as an argument to conditional formatting. The only issue is that using below:
RGBcolor1 = "RGB(" & CInt("&H" & Right(HEXcolor1, 2)) & _
", " & CInt("&H" & Mid(HEXcolor1, 3, 2)) & _
", " & CInt("&H" & Left(HEXcolor1, 2)) & ")"
where:
HEXcolor1 = Right("000000" & Hex(Sheet1.[LowColour].Interior.Color), 6)
The RGB value is a string, whereas in order to pass it as .Color, I need it to be a Long (Color = rgb(255, 0, 0)).
I am aware solutions exist where using Debug window is recommended to retrieve ?rgb(255,0,0), however, I would like to automate the process. I tried Clng() as well as .Evaluate() but they did not work.
Any help greatly appreciated!
You'll have to parse the string. You could use a regex or just make some simple replacements to isolate just the digits. For example:
strColor = "RGB(123, 0, 234)"
strColor = Replace(strColor, "RGB", "")
strColor = Replace(strColor, "(", "")
strColor = Replace(strColor, ")", "")
strColor = Replace(strColor, " ", "")
Dim a As Variant, c As Long
a = Split(strColor, ",")
c = a(0) * &H10000 + a(1) * &H100 + a(2)
Range("A1").Interior.Color = c
Or, with a regex (you'll have to add a reference to the Microsoft VBScript Regular Expressions 5.5 library):
With New RegExp
.Global = True
.Pattern = "[^\d,]" ' Remove anything that's not a digit or comma
Dim a As Variant, c As Long
a = Split(.Replace(strColor, ""), ",")
c = a(0) * &H10000 + a(1) * &H100 + a(2)
End If
Range("A1").Interior.Color = c
Edit:
Here's a quick but hacky way, using Eval() from the Microsoft Script Control:
With CreateObject("MSScriptControl.ScriptControl")
.Language = "VBScript"
Range("A1").Interior.Color = .Eval(strColor)
End With
You can convert it by using the val() function
Dim l as long
dim str as string
str = "111111"
l = val(str)
or
CLng(Val(str))
I had a project to do with looping a text into vertical and horizontal
suppose to look like this
http://pastebin.com/UJ6LZybU
Please help me :(
well this are the code i tried
but it only showed the end of the words
Dim input As String = "HELLOWORLD"
Dim i As Integer = 0
For i = 0 To input.Length - 1
Dim words As Char = input(i)
TextBox1.Text = ("HELLOWORLD" & vbNewLine & words)
Next
this project can be answered in any type of loop
doloop
forloop
Set TextBox1.Multiline = True and resize
Dim input As String = "HELLOWORLD"
TextBox1.Text = ("HELLOWORLD" & Environment.NewLine)
For i As Integer = 1 To input.Length - 1
TextBox1.Text &= (input(i) & Environment.NewLine)
Next
This should do it:
Dim i As Integer
Dim input As String = "HELLOWORLD"
TextBox1.Text = input
For i = 1 To input.Length - 1
TextBox1.Text &= vbNewLine & input(i)
Next