PDA

View Full Version : Reference in Superscript



Sadhik
12-27-2017, 05:14 AM
I am working in the word file which contains lot of reference numbers (i apply character style is "sup" for reference numbers), which is in the position of superscript. I want to check there is no repetition in the reference number. I hereby attach the code which is not run. Please clarify

Sub Findref()
Dim opara As Paragraph
Dim x As Variant
Dim count As Integer
Dim i As Integer
Dim DocumentContent As Range
Set DocumentContent = ThisDocument.Content
With DocumentContent.Find
.Style = "sup"
.Format = True
.Forward = True
Do While .Execute
If .Found = True Then
x = DocumentContent.Text
For Each opara In ActiveDocument.Paragraphs
If ActiveDocument.Styles("sup") = x Then
count = count + 1
End If
Next
End If
Loop
End With


If count > 1 Then
MsgBox "Reference Numbers Repeated"
Else
MsgBox "Reference Numbers not repeated"
End If
End sub

gmaxey
12-27-2017, 07:09 AM
If you style sup is a linked style (e.g., both paragraph and character) then you may need to search on "sup Char". Regardless, one way to detect duplicates is to attempt to add values to a collection. Attempts to add a duplicate value will error. Trap the error and you have marked your dups:



Sub Findref()
Dim oRng As Range
Dim oCol As New Collection
Dim bDups As Boolean
bDups = False
Set oRng = ActiveDocument.Range
With oRng.Find
.Style = "sup Char"
.Format = True
.Forward = True
While .Execute
On Error Resume Next
oCol.Add Trim(oRng.Text), Trim(oRng.Text)
If Err.Number <> 0 Then
bDups = True
oRng.HighlightColorIndex = wdRed
End If
oRng.Collapse wdCollapseEnd
Wend
End With
If bDups Then
MsgBox "Reference Numbers Repeated"
Else
MsgBox "Reference Numbers not repeated"
End If
lbl_Exit:
Exit Sub
End Sub

Sadhik
12-27-2017, 10:04 PM
That works fine. Now i am trying to get the page number of the duplicates, I use the following code

oRng.Information(wdActiveEndPageNumber)

It is working fine for some cases. Sometimes it shows the wrong page number of the duplicate references.

Please advise me in this regard,

Sadhik
12-27-2017, 10:07 PM
The syntax of the coding is below

If bDups Then
MsgBox "Reference Numbers Repeated on Page Number " & oRng.Information(wdActiveEndPageNumber)
Else
MsgBox "Reference Numbers not repeated"
End If

Sadhik
12-28-2017, 03:54 AM
Hi,

I am working in the word file which contains lot of reference numbers in superscript. I iterate through every reference number and check any reference number is missing. I have the problem with my following code. Please advise me.

Sub Superscription()
Dim opara As Paragraph
Dim x As Integer
Dim count As Integer
Dim i As Integer
Dim DocumentContent As Range
Set DocumentContent = ThisDocument.Content
With DocumentContent.Find
.Font.Superscript = True
.Format = True
.Forward = True
Do While .Execute
If .Found = True Then
x = DocumentContent.Text
End If
Loop
MsgBox " Total Number of Referemces is " & x
End With
For i = 1 To x
For Each opara In ActiveDocument.Paragraphs
If opara.Range.Text <> i Then
MsgBox i & " Not Found"
End If
Next
Next i
End Sub

Regards,
Sadhik

gmaxey
12-28-2017, 05:24 AM
As coded, you are returning the page number of the last duplicate found. If there are multiple duplicates on different pages then that would not be reflected in your code.

gmaxey
12-28-2017, 05:29 AM
Neither your description of the problem or you code makes any sense. What exactly are you trying to do?

Sadhik
12-28-2017, 05:39 AM
Thanks for your reply.

Is this right way to find any superscript text which is not equal to the variable i (Please refer above code).

If opara.Range.Text <> i Then

If not please tell me how to search the superscript text in the paragraphs.

With Thanks
Sadhik

gmaxey
12-28-2017, 06:00 AM
That depends. If the paragraph text is say 1, 5, 9, 101 then yes that code would tell you whether the text was not equal to the variable, but what is the point of have a bunch of paragraphs in your document consisting of nothing but a number.

You are wasting my time with your incomplete description of the problem. What is the problem? Give an example. Yes, some actual sample text that illustrates what you are trying to achieve.

Sadhik
12-28-2017, 06:15 AM
I Apology for not explaining the problem. Now i am trying to explain. The following paragraph contains reference numbers ranging from 1 to 5 (which are in superscript & reference number 4 is missing). My goal is to check the missing reference number in the order.

On the Insert tab1, the galleries include items that are designed to coordinate with the overall look of your document.2 You can use these galleries to insert tables, headers, footers, lists, cover pages, and other document building blocks.3 When you create pictures, charts, or diagrams, they also coordinate with your current document look. You can easily change the formatting of selected text in the document text by choosing a look for the selected text from the Quick Styles gallery on the Home tab. You can also format text directly by using the other controls on the Home tab.5

I iterate through every superscripted reference numbers and check the missing reference number (4) in the order. How can i iterate through the every reference numbers and find the missing reference number.

With Thanks & Regards
Sadhik

gmaxey
12-28-2017, 06:25 AM
Well obviously you can't find something that isn't there :-). The following, will flag the 5 as a reference number that is out of order.


Sub ScratchMacro()
'A basic Word macro coded by Greg Maxey, http://gregmaxey.com/word_tips.html, 12/28/2017
Dim oRng As Word.Range
Dim lngRef As Long
lngRef = 0
Set oRng = ActiveDocument.Range
With oRng.Find
.Font.Superscript = True
While .Execute
If IsNumeric(oRng) Then
lngRef = lngRef + 1
If CLng(oRng) = lngRef Then
'Do nothing. This is the next sequential number
Else
oRng.HighlightColorIndex = wdRed
End If
End If
oRng.Collapse wdCollapseEnd
Wend
End With
lbl_Exit:
Exit Sub
End Sub

Sadhik
12-28-2017, 10:06 PM
Hi,

This code works. Thanks a lot for giving me a solution

With Thanks & Regards,

Sadhik

Sadhik
01-04-2018, 12:02 AM
Hi,

I am working on the file with lot of reference numbers (Please refer to my earlier posts.). I have to find the duplicates occurences in the reference numbers. My following code just find the missing references. I want to find duplicate references.

Say for Example

1On the Insert tab, the galleries include items that are designed to coordinate with the overall look of your document. You can use these galleries to insert tables, headers, footers, lists, cover pages, and other document building blocks. When you create pictures, charts, or diagrams, they also coordinate with your current document look.4 You can easily change the formatting of selected3 text in the document text by choosing a look for the selected text from the Quick Styles gallery on the Home tab. You can also format text directly by using the other controls on the Home tab.

In above paragraph my script only finds the missing references numbers (2,3). Editor may fill up the missing references. Again the reference number 3 is following in the paragraph, which is already filled by editor so that it has to be duplicate reference. I want to highlight the missing elements (3) where ever is occured in this file.

My code is as follows

Sub Macro1()
Dim oRng As Word.Range
Dim x As Integer
Dim lngRef As Long
Dim counter As Long
Dim missingElements As String
Dim lastValue As Integer
lngRef = 0
Set oRng = ActiveDocument.Range
With oRng.Find
.Font.Superscript = True
'.Forward = False
While .Execute
x = oRng.Text
counter = counter + 1
If (lastValue <> x) Then
If (counter - CLng(oRng) < 0) Then
For i = 1 To (oRng - (lastValue + 1))
If (Len(missingElements) > 0) Then
missingElements = missingElements & ", "
End If
lastValue = lastValue + 1
counter = counter + 1

missingElements = missingElements & lastValue

Next i

End If
lastValue = counter
End If


Wend
MsgBox "Reference numbers are missing in the following pages " & missingElements
End With
lbl_Exit:
Exit Sub
End Sub

With Thanks & Regards,

Sadhik

gmaxey
01-06-2018, 06:21 AM
Sadhik

From your example, it appears that the reference number 3 is not missing or duplicated, but out of order. 2 is missing and 4 has no place in the text. You can't highlight something that isn't there!!!!

What is you ULTIMATE goal? If it is correctly sequenced reference numbers then perhaps:


Sub Macro1()
Dim oRng As Word.Range
Dim lngIndex As Long
Set oRng = ActiveDocument.Range
With oRng.Find
.Text = "[0-9]{1,}"
.Font.Superscript = True
.MatchWildcards = True
While .Execute
lngIndex = lngIndex + 1
If lngIndex <> CLng(oRng.Text) Then
oRng.Select
oRng.Text = InputBox("The selected reference number is does not match the expected value." & vbCr + vbCr _
& "Either the reference numbers are out of order or one or more" _
& " reference numbers are missing from the sequence." & vbCr + vbCr _
& "The expected number is " & lngIndex, "SEQUENCE ERROR", lngIndex)
End If
oRng.Collapse wdCollapseEnd
Wend
End With
lbl_Exit:
Exit Sub
End Sub

macropod
01-06-2018, 01:57 PM
If you want to have sequential superscripted #s, why not use any of:
• footnotes;
• endnotes; or
• superscripted SEQ fields.
The first two will auto-update immediately you add/delete a footnote or endnote and the last will auto-update when you use Ctrl-A, F9 after you add/delete a superscripted SEQ field. Any of those is far more flexible than trying to manage superscripts the way you're trying to.