How to apply a style to a substring of text in Indesign? - vb.net

How can I select a substring of text from my Cell, so that I can apply a character style?
This works fine when I'm only selecting a single character:
If IsNumeric(para.Contents.ToString.Substring(0, 1))
doc.Selection = para.Characters.ItemByRange(1, 1)
doc.Selection.Item(1).appliedCharacterStyle = mycharstyle
End If
but it fails if I try to select a range of characters
doc.Selection = myCell.Characters.ItemByRange(72, 76)
The error is
Invalid valid for set property 'Selection. Expected Array of Objects, Object or idNothingEnum enumerator, but received (Character, Character, Character, Character, Character)
I think there must be some entirely different technique to apply my CharacterStyle to a substring of text in my Cell, but after searching high-and-low I've yet to discover it.

got it working with GREP instead.
app.FindGrepPreferences = idNothingEnum.idNothing
app.ChangeGrepPreferences = idNothingEnum.idNothing
app.FindChangeGrepOptions.IncludeFootnotes = False
app.FindChangeGrepOptions.IncludeHiddenLayers = False
app.FindChangeGrepOptions.IncludeLockedLayersForFind = False
app.FindChangeGrepOptions.IncludeLockedStoriesForFind = False
app.FindChangeGrepOptions.IncludeMasterPages = False
mycharstyle = doc.CharacterStyles("SK-number1")
app.ChangeGrepPreferences.AppliedCharacterStyle = mycharstyle
app.FindGrepPreferences.FindWhat = "(?<=\t)(\d{4}.*~b)"
doc.ChangeGrep()

Related

VBA - Why IsNumeric is not evaluating correctly

I have following code in VBA to evaluate a variable lngPNumber to send the "correct" value to a WorksheetFunction.vLookup function:
Dim lngPNumber As Variant
lngPNumber = ActiveSheet.Cells(objInitialCell.Row, INT_ACCP_COL_PNUMBER).Value
If IsNumeric(lngPNumber) = False Or CDbl(lngPNumber) <> Round(CDbl(lngPNumber)) Then
lngPNumber = CStr(ActiveSheet.Cells(objInitialCell.Row, INT_ACCP_COL_PNUMBER).Text)
End If
lngPNumbercan be:
an integer value (f.i. 4111)
a float value (41.111111)
or just a
string value (normally "xxxx" but can also be filled with strings
like "asdasd", "dasdsadasd", etc...)
In the last both cases, I want to send the Cell Text and not the Cell Value where lngPNumberis obtained.
However, I get a Type Missmatch error if the value is a string like in the last example in the list. Any help?
The If will try to resolve both sides of the Or regardless if the first is false or true, so CDbl(lngPNumber) will error if lngPNumber is text.
So split them into two.
Dim lngPNumber As Variant
lngPNumber = ActiveSheet.Cells(objInitialCell.Row, INT_ACCP_COL_PNUMBER).Value
If Not IsNumeric(lngPNumber) Then
lngPNumber = CStr(ActiveSheet.Cells(objInitialCell.Row, INT_ACCP_COL_PNUMBER).Text)
ElseIF CDbl(lngPNumber) <> Round(CDbl(lngPNumber)) Then
lngPNumber = CStr(ActiveSheet.Cells(objInitialCell.Row, INT_ACCP_COL_PNUMBER).Text)
End If
Now the second will fire only if lngPNumber is a number.

VBA code lower case to upper case using Mid string

I've gotten stuck with this VBA code. Any help would be greatly appreciated. I'm trying to change the first letters of 2 words from lower case to upper case. Also, how should I take the space in between these two words into consideration in the code?
I haven't been able to execute the code as I keep getting this compile error: "Argument not optional".
Function Properword(Text)
Dim rText
rText = Len(rText)
If rText(Mid(1, 1)) = LCase(Str) Then
rText = UCase(Str)
If rText(Mid(6, 1)) = LCase(Str) Then
rText = UCase
End If
End Function
Cheers!
First of all, you don't have to use UDF. Simply use inbuilt WorksheetFunction.Proper function to achieve ProperCase.
If you still want to create UDF, an example would be
Function Properword(strTxt)
Dim arrTxt
arrTxt = Split(strTxt, " ")
For i = LBound(arrTxt) To UBound(arrTxt)
arrTxt(i) = UCase(Left(arrTxt(i), 1)) & Mid(arrTxt(i), 2)
Next
Properword = Join(arrTxt, " ")
End Function
Finally, issues with your code
rText = Len(rText) ~~ this means rText will contain a numeric value because Len returns the lenght of the string
If rText(Mid(1, 1)) = LCase(Str) Then ~~ Mid takes the string as first argument followed by start point and then end point (optional).
not sure what you were trying to do in the following lines.
rText = UCase(Str)
If rText(Mid(6, 1)) = LCase(Str) Then
rText = UCase
In addition to the Excel function PROPER
str = WorksheetFunction.Proper("UPPER lower") ' "Upper Lower"
There is also the VBA.StrConv function:
str = StrConv("UPPER lower", vbProperCase) ' "Upper Lower"
To convert only parts of the string to uppercase, you can use RegEx, or the Mid statement:
Mid(str, 1, 1) = UCase(Mid(str, 1, 1)) ' makes the first letter uppercase

Trimming begining of string in VB forms application

Here is an example of the string / variable i want to trim:
type="game" music-file="my-file.mp3" gts="false"
I would like to find the beginning / end of the following code and trim them off.
I do not know the string before / after, so i am not able to use replace("String", "") functions.
How would i trim the string to make it only show: music-file="my-file.mp3?
I would like the string to go into a text box.
I would like to trim the beginning and end if possible.
I would NOT like to use external libraries (DLLS)
I assume your keyword is music-file="
use InStr() to find your keyword example:
int keyWordPos = InStr(<your string>,<your keyword>) 'keyWordPos = 13
you will get the first position of keyword. Next, use it as start position to find the end of value(I mean the second " of music-file="my-file.mp3"). But, don't forget to add keyword length to skip the first ". Like this.
int endWordPos = Instr(keyWordPos + len(<your keyword>),<your string>,<your keyword>) 'Start at 25 and endWordPos = 36
After that, use SubString() to get your value.
string strVal = <String Variable>.SubString(keyWordPos,endWordPos - keyWordPos + 1)
Here the answer of mine.
string strText = "type='game' music-file='my-file.mp3' gts='false'"
string keyWord = "music-file='"
int keyWordPos = Instr(strText,keyWord) 'Should be 13
int endWordPos = Instr(keyWordPos + len(keyWord),strText,"""") 'Should be 36
string resultText = strText.SubString(keyWordPos,endWordPos - keyWordPos +1)
Dim text As String = "type='game' music-file='my-file.mp3' gts='false'"
text = ((text.Substring(text.IndexOf("music-file"))).Substring(0, (text.Substring(text.IndexOf("music-file"))).IndexOf(" gts="))).Trim

How to resolve Index and length must refer to a location within the string. Parameter name:length"?

Dim amountorhour As String = "A4"
If amountorhour.Substring(0, 1) = "A" Then
amount = amountorhour.Substring(1, amountorhour.Length)--**error comes here**
HrsWorked = 0
Else
HrsWorked = amountorhour.Substring(1, amountorhour.Length - 1)
amount = 0
I know this error shows up when I am asking for length of string which is out of range but in this scenario, I don't see anything like that. Please help me in figuring out the problem
The second parameter is the length of the substring from the index that you have specified. So passing amountorhour.Length works only if you pass 0 as first parameter which would return the original string.
It seems that you want to take all but the first character, you can use the overload with one parameter:
amount = amountorhour.Substring(1)
This is the same as
amount = amountorhour.Substring(1, amountorhour.Length - 1)
As an aside, you can use
If amountorhour.StartsWith("A") Then
instead of
If amountorhour.Substring(0, 1) = "A" Then

How to delete a string from a RichTextBox?

I am trying to parse junk and narrow down a bunch of text. How do I delete the current line if a does not match? I would like to remove the line entirely:
For i As Integer = 0 To RichTextBox1.Lines.Length - 1
Dim a As String = RichTextBox1.Lines(i).ToString
If Not a = "SaveThisLine" Then
'delete the active line
End If
Next
Also how would I match partially? Such as if not a = "SaveThisLine" & * (to use a wildcard).
I would not touch original text and rather save valid lines into a StringBuilder, so if line is valid, AppendLine to it. In the end dump back into RichTextBox1.Text using StringBuilder.ToString.
For partial match in VB.NET you can use a native Like operator:
"aaa" Like "a*"
Returns True.
Or use regular expressions:
System.Text.RegularExpressions.Regex.Match("aaa", "^a").Success
Also returns True.
You can do it in this way to:
For i As Integer = 0 To RichTextBox1.Lines.Length - 1
If RichTextBox1.Lines(i) = "2" Then
RichTextBox1.Text = Replace(RichTextBox1.Text, RichTextBox1.Lines(i), "", , 1)
End If
Next