word vba - remove manually typed list number - vba

I have lot of documents with list number typed manually, so my intent is to remove those manual list number and the tab or space following it.
e.g
1. Text 1
1.1 Text 2
1.1.1 Text 3
1.1.1.1 Text 4
become
Text 1
Text 2
Text 3
Text 4
I'm not sure how to do this with vba and I'd really appreciate your help.

I have figured it out by using below code:
Sub RemoveManualListNumber()
Dim strInitialPar As String
Dim intSpace As Integer, intTab As Integer
Dim strFinalPar As String
Dim iPar
For iPar = 1 To ActiveDocument.Paragraphs.Count
strInitialPar = ActiveDocument.Paragraphs(iPar).Range.Text
intSpace = InStr(1, strInitialPar, " ")
intTab = InStr(1, strInitialPar, Chr(9))
Debug.Print "Paragraph " & iPar & ": " & "Index space = " & intSpace & ", Index tab = " & intTab
If intTab > 0 And intTab < intSpace Then
strFinalPar = Right(strInitialPar, Len(strInitialPar) - intTab)
Else
strFinalPar = Right(strInitialPar, Len(strInitialPar) - intSpace)
End If
ActiveDocument.Paragraphs(iPar).Range.Text = strFinalPar
Next iPar
End sub

Related

Conditional Formatting in word vba

Greeting to all Members and Experts, I am trying to automate
the formatting process in word. The formatting is done by applying styles. But before applying styles I need to trim extra spaces between characters of serial numbers, for example, 1. a. i. and insert tabs after dot(.) and then apply the style. I have attached a sample document. Plz have a look. I have tried to get the desired result by using the following code but it doesn't get the work done
I am new here so i dont know how to attach sample files so, here is the link for sample file. https://docs.google.com/document/d/1Z1dB6tvPKVrxHlw7qV8VNyiy49c5lRZN/edit?usp=sharing&ouid=101706223056224820285&rtpof=true&sd=true
Any help or suggestion would be of great help. Thanks in advance...
Sub formatts()
Dim a As Integer
Dim i As Integer, n As Long, para As Paragraph, rng As Range, doc As Document
Set doc = ActiveDocument
With doc
For i = 1 To .Range.Paragraphs.Count
For n = 1 To doc.Paragraphs(i).Range.Characters.Count
If .Paragraphs(i).Range.Characters(n).Text = " " Or .Paragraphs(i).Range.Characters(n).Text = Chr(9) Or .Paragraphs(i).Range.Characters(n).Text = Chr(160) Then
.Paragraphs(i).Range.Characters(n).Select
'This line checks whether the first character is whitespace character or not and delete it.
doc.Paragraphs(i).Range.Characters(n).Delete
ElseIf .Paragraphs(i).Range.Characters(n).Text = "." Then
.Paragraphs(i).Range.Characters(n).InsertAfter (vbTab)
n = n + 1
a = a + 1
ElseIf .Paragraphs(i).Range.Characters(n).Text Like "[a-z]." And .Paragraphs(i).Range.Characters(n).Next.Next.Text <> "i" Then
Exit For
End If
If a >= 3 Then Exit For
Next
For n = 1 To doc.Paragraphs(i).Range.Characters.Count
If .Paragraphs(i).Range.Characters(n).Text = "i" And .Paragraphs(i).Range.Characters(n).Next.Text = "." And .Paragraphs(i).Range.Characters(n).Next.Next.Text = " " Then
doc.Range.Paragraphs(i).Style = "shh"
Exit For:
ElseIf .Paragraphs(i).Range.Characters(n).Text = "a" Or .Paragraphs(i).Range.Characters(n).Text = "b" Or .Paragraphs(i).Range.Characters(n).Text = "c" And .Paragraphs(i).Range.Characters(n).Next.Text = "." And .Paragraphs(i).Range.Characters(n).Next.Next.Text = " " Then
doc.Range.Paragraphs(i).Style = "sh"
Exit For
End If
Next
Next
End With
End Sub

How to append lines of text into a Richtextbox at specific location/index?

I have a lua script file in which I would like to add some new lines at specific locations.
So let's say we have the following text as an example:
line 1
line 2
line 3
line 8
line 9
line 10
I would like to insert some new lines after line 3 and also a few additional lines before line 8
So far I've tried to index the lines but didn't found a way to use those indexes in order to write new lines of text.
For i As Integer = 0 To textbox.Lines.Count - 1
Dim x As Integer = i + 1
Dim y As Integer = i - 1
If textbox.Lines(i).Contains("line 3") Then
textbox.Lines(x).Append("Line 4")
End If
Next
In case someone else is facing the same obstacle,I found out that the required result is obtained by using the Input/Output library and threading library Imports System.IO,Imports System.Threading, combined with the following for loop:
For i As Integer = 0 To textbox.Lines.Length - 1
Dim s As String = textbox.Lines(i)
Dim index As Integer = s.IndexOf("line 3")
If index > -1 Then
Dim length As Integer = s.Length - index
index += textbox.GetFirstCharIndexFromLine(i)
textbox.Select(index + length, 1)
Thread.Sleep(1)
SendKeys.SendWait("{ENTER}")
textbox.Text = textbox.Text.Insert(textbox.GetFirstCharIndexOfCurrentLine,
"line 4" & vbCrLf &
"line 5 " & vbCrLf &
"line 6" & vbCrLf &
"line 7" & vbCrLf)
End If
Next

PrintDialog ToPage should be less than the page count

I recently upgraded my computer to Windows 10, and now one of the programs is acting weird since the upgrade.
Trying to print a page range out of a PDF, and when I print pages 1 to 100 (out of 477 pages) I get an error saying ToPage should be less than the page count even though 100 is less than 477.
If I skip the page range part and just have it print all of the pages, it works fine.
Sub PrintToPaperSync(ByVal InputfilePath As String, Optional ByVal DeleteAfter As Boolean = False)
On Error GoTo sError
Dim tError As String = ""
Dim toPage As Integer = 0
Console.WriteLine("PrintToPaper " & InputfilePath)
Dim File As String = Split(InputfilePath, "\")(Split(InputfilePath, "\").Length - 1)
Dim viewer As New Syncfusion.Windows.Forms.PdfViewer.PdfDocumentView
viewer.Load(InputfilePath)
Dim print As New System.Windows.Forms.PrintDialog()
print.Document = viewer.PrintDocument
print.Document.DocumentName = File
'print 100 pages at a time
Do While toPage < viewer.PageCount
print.Document.PrinterSettings.PrintRange = Drawing.Printing.PrintRange.SomePages
print.Document.PrinterSettings.FromPage = toPage + 1
toPage += 100
print.Document.PrinterSettings.ToPage = IIf(toPage < viewer.PageCount, toPage, viewer.PageCount)
tError = "From: " & print.Document.PrinterSettings.FromPage & " | To: " & print.Document.PrinterSettings.ToPage & " | PageCount: " & viewer.PageCount & " - "
print.Document.Print()
Application.DoEvents()
Loop
viewer.Unload()
viewer.Dispose()
Console.WriteLine("Printing: " & InputfilePath)
Exit Sub
sError:
Dim ErrorStr As String = ErrorToString()
WriteLine("PrintToPaper " & tError & " " & InputfilePath & " - " & ErrorStr)
End Sub
Full text of the error:
PrintToPaper From: 1 | To: 100 | PageCount: 477 - F:\Process\LogTag-10-30-15-104122.pdf - ToPage should be less than the page count
We want to only print 100 pages at a time, because the printer begins to slow down after 100 pages for some reason.
This printing problem is caused due to the issue with Syncfusion Control. You can contact the Syncfusion Software Support team to get the issue resolved. Please follow the below link to contact Syncfusion Support Team
Click Here

If a listbox contains an item LIKE

Is there anywhere to check whether a listbox contains an item which is similar to a string?
I have tried to use a Like statement but this doesn't work.
I have tried:
For i As Integer = 0 To TopicListBox.Items.Count - 1
If (TopicListBox.Items(i).ToString.Contains(Record.Item("Keyphrase2"))) Then
Dim Item As String = TopicListBox.Items(i).ToString
If Item Like "Questions related to:" & Record.Item("Keyphrase2") & ": |*| Qs" Then
Dim ItemSplit() As String = Item.Split(New Char() {"|"c})
Dim AmountOfQuestionsAfter As Integer = AmountOfQuestions + CInt(ItemSplit(1))
Item = ItemSplit(0) & AmountOfQuestionsAfter & ItemSplit(1)
Else
TopicListBox.Items.Add("Questions related to:" & Record.Item("Keyphrase2") & ": |" & AmountOfQuestions & "| Qs")
Exit For
End If
End If
Next
I don't really understand what you are trying to accomplish, but here is a LINQ function to return all the items that contain a certain string.
Dim lb As New ListBox
lb.Items.Add("asdf")
lb.Items.Add("asdfasdf")
lb.Items.Add("aifisasdf")
lb.Items.Add("adf")
'' find all items in the ListBox that contain the string "asdf"
Dim found = lb.Items.Cast(Of String).Where(Function(f) f.Contains("asdf"))
For Each s In found
'' do something with those items
Next
Can't you just do
If Item.Contains("Questions related to:" & Record.Item("Keyphrase2")) Then
...
End If
or
If Item.StartsWith("Questions related to:" & Record.Item("Keyphrase2")) Then
...
End If
?
Dim Found = (From Result In lb.Items Where Result.ToString = "Value" Select Result)
If (Found.Count > 0) Then
' your code
End If

Text.remove for phone number formatting in vb.net

I have a textbox for a phone number that formats the phone number to look like this:
(123) 456-7891
but I want it to change back to just numbers when the user is finished with the data entry:
1234567891
Here's my code for formatting the number:
Dim newNumber As String
If txtContactNumberNew.Text.Length = 0 Then
txtContactNumberNew.Text = "N/A"
ElseIf txtContactNumberNew.Text.Length = 10 Then
newNumber = "(" & txtContactNumberNew.Text.Substring(0, 3) & ") " & txtContactNumberNew.Text.Substring(3, 3) & "-" & txtContactNumberNew.Text.Substring(6, 4)
txtContactNumberNew.Text = newNumber
ElseIf txtContactNumberNew.Text.Length > 10 Then
newNumber = "(" & txtContactNumberNew.Text.Substring(0, 3) & ") " & txtContactNumberNew.Text.Substring(3, 3) & "-" & txtContactNumberNew.Text.Substring(6, 4) & " x " & txtContactNumberNew.Text.Substring(10)
txtContactNumberNew.Text = newNumber
ElseIf txtContactNumberNew.Text.Length < 10 Then
MsgBox("Not enough Numbers! Please retype the Phone number", vbExclamation, "Not enough Numbers")
Return
End If
Since I added the parenthesis would this change my character count? I just want the formatting removed when the form is completed.
Consider using the trim method to remove unwanted characters. You could then get the character count of the result.
'List of characters to remove
Dim charsToTrim() As Char = { "("c, ")"c, "-"c}
Dim result As String = txtContactNumberNew.Text.Trim(charsToTrim)
taken from http://msdn.microsoft.com/en-us/library/d4tt83f9.aspx
if you just want the () removed can't you just use replace from regex
System.Text.RegularExpressions.Regex.Replace(str,"[\(|\)|\-|\s]","")