Consulting

Results 1 to 15 of 15

Thread: Reference in Superscript

  1. #1
    VBAX Regular
    Joined
    Dec 2017
    Posts
    11
    Location

    Reference in Superscript

    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

  2. #2
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,335
    Location
    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
    Greg

    Visit my website: http://gregmaxey.com

  3. #3
    VBAX Regular
    Joined
    Dec 2017
    Posts
    11
    Location
    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,

  4. #4
    VBAX Regular
    Joined
    Dec 2017
    Posts
    11
    Location
    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

  5. #5
    VBAX Regular
    Joined
    Dec 2017
    Posts
    11
    Location
    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

  6. #6
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,335
    Location
    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.
    Greg

    Visit my website: http://gregmaxey.com

  7. #7
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,335
    Location
    Neither your description of the problem or you code makes any sense. What exactly are you trying to do?
    Greg

    Visit my website: http://gregmaxey.com

  8. #8
    VBAX Regular
    Joined
    Dec 2017
    Posts
    11
    Location

    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



  9. #9
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,335
    Location
    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.
    Greg

    Visit my website: http://gregmaxey.com

  10. #10
    VBAX Regular
    Joined
    Dec 2017
    Posts
    11
    Location
    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

  11. #11
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,335
    Location
    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
    Greg

    Visit my website: http://gregmaxey.com

  12. #12
    VBAX Regular
    Joined
    Dec 2017
    Posts
    11
    Location
    Hi,

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

    With Thanks & Regards,

    Sadhik

  13. #13
    VBAX Regular
    Joined
    Dec 2017
    Posts
    11
    Location
    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

  14. #14
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,335
    Location
    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
    Greg

    Visit my website: http://gregmaxey.com

  15. #15
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    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.
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •