Word 2010 VBA macro to restart numbered lists - vba

I have a Word 2010 document with a lot of lists that were copied from another document. Unfortunately, when I copied the lists into my document they do not start at 1 anymore as they did in the orginial but are continuing from the previous lists leading up to chaos in my document.
I need my macro to run along all the numbered lists in my document and restarts each list at 1. I tried this with listformat.applylisttemplate but when calling my lists with "For Each li In ActiveDocument.Lists" I could not use the listformat function. See my code snippet below.
Set temp3 = wrdApp.ListGalleries(wdNumberGallery).ListTemplates(1).ListLevels(1)
With temp3
.StartAt = 1
End With
For Each li In ActiveDocument.Lists
li.Range.ListFormat.ApplyListTemplate ListTemplate:=temp3
Next
Obviously I would like something in the form of:
For Each li In ActiveDocument.Lists
li. => restart list count here
Next
Can anyone help? Thanks!
Addition: my current text looks like below and I'm trying to get the macro to restart each list with 1 in Word:
INPUT:
Sometext
3. TextA
4. TextB
5. TextC
6. TextD
Some other text
21. Text q
22. Text w
23. Text e
OUTPUT after macro:
1. TextA
2. TextB
3. TextC
4. TextD
Some other text
1. Text q
2. Text w
3. Text e

I don't have an example to try this on but give it a try, perhaps it will work?
Sub ListRestart()
For Each li In ListGalleries(wdNumberGallery).ListTemplates
li.ListLevels(1).StartAt = 1
Next
End Sub
Edit:
I agree, the above doesn't do the trick. The following will identify the correct paragraph where we want to reset the numbering. However, I cannot figure out how to reset the numbering from there. Perhaps that will get someone else closer to the answer?
Sub ListRestart()
' for each paragraph
' If this paragraph is a list (ListType = 3) and last paragraph was not (ListType = 0) then restart numbering
Dim n As Integer, lastListType As Integer
For Each myPara In ActiveDocument.Paragraphs
n = n + 1
'Debug.Print n & myPara.Range.Text & " ListType: " & myPara.Range.ListFormat.ListType
If myPara.Range.ListFormat.ListType = 3 Then
If lastListType = 0 Then
Debug.Print n & " " & myPara.Range.Text & " Reset numbering here"
End If
End If
lastListType = myPara.Range.ListFormat.ListType
Next
End Sub

Hi I have created a macro here that does exactly what you want.
You need to make the numbered lists as a certain style first. This is to make sure that the macro changes only the lists you need to change.
https://sites.google.com/site/anvilsoup/word/fix-numbered-items
How it works
I've chosen to use a reasonable method of determining when to restart lists: If the previous paragraph isn't a list item, then this paragraph must be a new list!
This logic is what most people use when starting a new list. It's sensible and conventional.
NOTE: If you need to have an unnumbered paragraph nestled within a list, you should consider using line breaks (SHIFT-ENTER) instead of paragraph breaks. Using line breaks will also ensure that indenting is preserved. Don't do strange things like turn off numbering for that paragraph. Technically this text is part of the same numbered item so it should be in the same paragraph.

Related

Finding Endnote number with wdRestartSection NumberingRule

I am writing a VBA script to convert endnotes to plain text. This is fairly straightforward when the endnotes have continuous numbers (copy all the end notes to the end of the text, number them using the index, and replace all the references with the indexes).
In this case, however, the endnote numbers are configured to reset every section (NumberingRule=wdRestartSection). This means the index is not the number. I've tried to get the number using endnote.Reference.Text, but this is empty. I haven't found anywhere in the object model that has the actual number for each Endnote.
Is this information available?
Is there a way to walk Endnotes per-section rather than for the entire document so that I could track the index myself?
I'm currently trying to fetch it this way:
For Each objEndnote In ActiveDocument.Endnotes
print(objEndnote.Reference.Text)
Next
This just prints empty strings.
Looks like there is no number per section - weird. So you have to count it yourself per section:
Option Explicit
Sub getAllEndnotesWithNumbers()
Dim e As Endnote, section As Long, eCounter As Long
For Each e In ThisDocument.Endnotes
If section <> endnoteSection(e) Then
section = endnoteSection(e)
eCounter = 1
Debug.Print "--- Section " & section & " ----------"
End If
Debug.Print eCounter, e.Range.Text
eCounter = eCounter + 1
Next
End Sub
Private Function endnoteSection(e As Endnote) As Long
endnoteSection = e.Range.Sections(1).Index
End Function

VBA String pattern PinPointing

Ok so i have these 6 which are coerced during Running-time as String Values. So these lines below are String values
bdx-20131231.xml
bdx-20131231.xsd
bdx-20131231_cal.xml
bdx-20131231_def.xml
bdx-20131231_lab.xml
bdx-20131231_pre.xml
inside a macro we have a small simple loop that iterates and prints on the immediate window the string values that end with ".xml" so the xsd (second one) is not printed by the loop. Following you can see the loop
For Each el In IE.Document.getelementsbytagname("a")
If el.href Like "*.xml" Then
Debug.Print el.innertext, el.href
End If
next el
So we are left with these
bdx-20131231.xml
bdx-20131231_cal.xml
bdx-20131231_def.xml
bdx-20131231_lab.xml
bdx-20131231_pre.xml
However what i am trying to nail here is the XBRL INSTANCE DOCUMENT which has no letters at the end of it's naming it's bdx- numbers_only so it's the first one that interests us only.
However the statement... Like "*.xml" grabs and prints both 5 in the immediate window.
The first thing that came to my mind is ok split in two the bdx-20131231.xml and use the function IsNumeric on the second part after the hyphen however the value is coerced.
How can we make the loop pick only the first one bdx-20131231.xml?
For reasons of completeness you can see the whole code which the loop comes from here.
Courtesy of Tim Williams
The below should work:
Like "*[0-9].xml"

vb.net list confusion

I am looping through a list for a spellchecker in vb.net (using vs 2010). I want to go through a wrongly spelled word list. Each time the code picks the index that's one higher than the index of the last checked word.
In my version of notquiteVB/Pythonese I think it would translate something like:
(start loop)
dim i as Integer = 0
dim word as String
word = words_to_check_at_spellcheck.Item(0 + i)
i = i+1
(end loop)
But this doesn't work at all...when it gets to the last item in the list and reaches 'word = ' it throws the error of 'out of range -- must be less than the size of the collection'.
How do you get the last item in a list? Maybe lists aren't what VB uses for this kind of thing?
If you're collection of misspelled words is named mispelled:
For Each word As String In mispelled
'Do something
Next

Trim file after a blank line

I have a text file that has multiple blank lines and Im trying to return all the lines between two of them specifically
so if I have a file that looks like this:
____________________________
1########################
2##########################
3
4########################
5##########################
6#######################
7
8#########################
9##########################
10#######################
11####################
12########################
13#########################
14
15##########################
----------------------------
I would like to grab lines 8-13. Unfortunately, it might not always be 8-13 as it could be 9-20 or 7-8, but it will however always be between the 2nd and 3rd line break.
I know how to trim characters and pull out singular lines, but I have no idea how to trim entire sections.
Any help would be appreciated, even if you just point me to a tutorial.
Thanks in advance.
The basic idea here is to get the entire thing as a string, split it into groups at the double line breaks, and then reference the group you want (in your case, the third one).
Dim value As String = File.ReadAllText("C:\test.txt")
Dim breakString As String = Environment.NewLine & Environment.NewLine
Dim groups As String() = value.Split({breakString}, StringSplitOptions.None)
Dim desiredString As String = groups(2)
MsgBox(desiredString)
Edit:
In response to the question in your comment -
Environment.NewLine is a more dynamic way of specifying a line break. Assuming you're running on windows - you could use VbCrLf as well. The idea is that if you were to compile the same code on Linux, it Environment.NewLine would generate a Lf instead. You can see here for more information: http://en.wikipedia.org/wiki/Newline
The reason I used Environment.NewLine & Environment.NewLine is because you want to break your information where there are two line breaks (one at the end of the last line of a paragraph, and one for the blank line before the next paragraph)
What I ended up doing was trimming the last part and searching for what I needed in the first part (I know I didnt include the searching part in the question, but I was just trying to figure out a way to narrow down the search results as it would have had repeated results). Im posting this incase anyone else stumbles upon this looking for some answers.
Dim applist() = System.IO.File.ReadAllLines("C:\applist.txt")
Dim findICSName As String = "pid"
Dim ICSName As New Regex("\:.*?\(")
Dim x = 0
Do Until applist(x).Contains("Total PSS by OOM adjustment:")
If applist(x).Contains(findICSName) Then
app = ICSName.Match(applist(x)).Value
app = app.TrimStart(CChar(": "))
app = app.TrimEnd(CChar("("))
ListBox1.Items.Add(app)
End If
x = x + 1
Loop
End If
How this works is that it looks through each line for the regex until it reaches first word in the breakpoint "Total PSS by OOM adjustment:"

GetCrossReferenceItems in msword and VBA showing only limited content

I want to make a special list of figures with use of VBA and here I am using the function
myFigures = ActiveDocument.GetCrossReferenceItems(Referencetype:="Figure")
In my word document there are 20 figures, but myFigures only contains the first 10 figures (see my code below.).
I search the internet and found that others had the same problem, but I have not found any solutions.
My word is 2003 version
Please help me ....
Sub List()
Dim i As Long
Dim LowerValFig, UpperValFig As Integer
Dim myTables, myFigures as Variant
If ActiveDocument.Bookmarks.Count >= 1 Then
myFigures = ActiveDocument.GetCrossReferenceItems(Referencetype:="Figure")
' Test size...
LowerValFig = LBound(myFigures) 'Get the lower boundry number.
UpperValFig = UBound(myFigures) 'Get the upper boundry number
' Do something ....
For i = LBound(myFigures) To UBound(myFigures) ‘ should be 1…20, but is onlu 1…10
'Do something ....
Next i
End If
MsgBox ("Done ....")
End Sub*
Definitely something flaky with that. If I run the following code on a document that contains 32 Figure captions, the message boxes both display 32. However, if I uncomment the For Next loop, they only display 12 and the iteration ceases after the 12th item.
Dim i As Long
Dim myFigures As Variant
myFigures = ActiveDocument.GetCrossReferenceItems("Figure")
MsgBox myFigures(UBound(myFigures))
MsgBox UBound(myFigures)
'For i = 1 To UBound(myFigures)
' MsgBox myFigures(i)
'Next i
I had the same problem with my custom cross-refference dialog and solved it by invoking the dialog after each command ActiveDocument.GetCrossReferenceItems(YourCaptionName).
So you type:
varRefItemsFigure1 = ActiveDocument.GetCrossReferenceItems(g_strCaptionLabelFigure1)
For k = 1 To UBound(varRefItemsFigure1)
frmBwtRefDialog.ListBoxFigures.AddItem varRefItemsFigure1(k)
Next
and then:
frmBwtRefDialog.Show vbModeless
Thus the dialog invoked several times instead of one, but it works fast and don't do any trouble. I used this for one year and didn't see any errors.
Enjoy!
Frankly I feel bad about calling this an "answer", but here's what I did in the same situation. It would appear that entering the debugger and stepping through the GetCrossReferenceItems always returns the correct value. Inspired by this I tried various ways of giving control back to Word (DoEvents; running next segment using Application.OnTime) but to no avail. Eventually the only thing I found that worked was to invoke the debugger between assignments, so I have:
availRefs =
ActiveDocument.GetCrossReferenceItems(wdRefTypeNumberedItem):Stop
availTables =
ActiveDocument.GetCrossReferenceItems(wdCaptionTable):Stop
availFigures = ActiveDocument.GetCrossReferenceItems(wdCaptionFigure)
It's not pretty but, as I'm the only person who'll be running this, it kind of works for my purposes.