Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 10-22-2021, 11:05 AM
scienceguy scienceguy is offline Determining if an in-text table reference was created with Word's native cross reference feature Windows 10 Determining if an in-text table reference was created with Word's native cross reference feature Office 2016
Advanced Beginner
Determining if an in-text table reference was created with Word's native cross reference feature
 
Join Date: Feb 2019
Posts: 46
scienceguy is on a distinguished road
Default Determining if an in-text table reference was created with Word's native cross reference feature

Hello,

I am trying to create an application, which verifies if an in-text table (or figure, equation) reference was created using Microsoft Word's "cross reference" feature. For example, in a Word document, if there was a sentence that stated, "Please see Table 1 for further details," I want the application to verify that the author used Word's insert cross-reference feature and didn't just simply type "Table 1." Basically, is there a field code associated with "Table 1?"

Finding all of the "Table" occurrences is easy enough. However, I cannot figure out how to determine if they are associated with a table using Word's cross-reference feature. I have started my code in Excel, because I eventually want to create a spreadsheet of all of the occurrences, once I figure out the other part.

Here is my code so far. Any help would be appreciated!

Thanks,
Roy


Code:
Sub findRef()
'
Dim wdApp As Object
Dim wdDoc As Object
Dim aRng As Object
Dim strFile As String

With Application.FileDialog(msoFileDialogOpen)

  If .Show = -1 Then
    strFile = .SelectedItems(1)
  End If

End With

If strFile = "" Then
    Exit Sub
End If

On Error Resume Next
Set wdApp = GetObject(, "Word.Application")
If Err.Number <> 0 Then

    Set wdApp = CreateObject("Word.Application")
    wdApp.Visible = False

End If

Set wdDoc = wdApp.Documents.Open(Filename:=strFile, AddToRecentFiles:=False, Visible:=False)

Set aRng = wdDoc.Range
    
With aRng.Find
    .ClearFormatting
    .Text = "Table?[0-9]"
    .MatchWildcards = True
        Do While .Execute  ' Loop until Word can no longer find the search string
        
            'if aRng.text =  cross reference field code ...
        
        Loop
End With
    
wdDoc.Close False

Set aRng = Nothing
Set wdDoc = Nothing
wdApp.Quit
Set wdApp = Nothing


End Sub


Last edited by scienceguy; 10-23-2021 at 07:09 AM.
Reply With Quote
  #2  
Old 10-24-2021, 09:10 PM
Guessed's Avatar
Guessed Guessed is offline Determining if an in-text table reference was created with Word's native cross reference feature Windows 10 Determining if an in-text table reference was created with Word's native cross reference feature Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 3,932
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

Roy

If you are doing a search for 'Table #' and the document is correctly created, there will be multiple instances that your macro might find, all of which will needed to be handled/ignored by your code.
Table Captions, when done correctly are composed of two parts. The text "Table " followed by a Seq field with the series name 'Table" such that the field code starts with {Seq Table ...}
Cross references to a table caption are usually a Ref field with the name of a bookmark so the field looks something like {Ref _Ref86060324 \h} where the _Ref# is automatically 'randomly' generated when you use the Insert Cross Reference dialog.
List of Tables is also another instance where you would expect to see lots of Table #'s appear but you most likely will ignore these completely.

The following code is pulling all instances into the workbook. You would probably want to expand on this code to exclude the hits you don't want.
Code:
Sub findRef()
  Dim wdApp As Object, wdDoc As Object, aRng As Object, strFile As String
  Dim aSheet As Worksheet, iRow As Integer
  
  Set aSheet = ThisWorkbook.ActiveSheet
  aSheet.Range("A1:D1") = Split("Doc,Page,Text,FieldCode", ",")
  iRow = 1
  
  With Application.FileDialog(msoFileDialogOpen)
    If .Show = -1 Then
      strFile = .SelectedItems(1)
    End If
  End With
  
  If strFile = "" Then Exit Sub
  
  On Error Resume Next
  Set wdApp = GetObject(, "Word.Application")
  If Err.Number <> 0 Then Set wdApp = CreateObject("Word.Application")
  wdApp.Visible = False
  
  Set wdDoc = wdApp.Documents.Open(Filename:=strFile, AddToRecentFiles:=False, Visible:=False)
  Set aRng = wdDoc.Range
  With aRng.Find
    .ClearFormatting
    .Text = "Table?[0-9]{1,3}"
    .MatchWildcards = True
    .MatchCase = False
    Do While .Execute  ' Loop until Word can no longer find the search string
      iRow = iRow + 1
      aSheet.Range("A" & iRow) = strFile
      aSheet.Range("B" & iRow) = aRng.Information(3)    'wdActiveEndPageNumber = 3
      aSheet.Range("C" & iRow) = aRng.Text
      aRng.MoveStart Unit:=1, Count:=-1                 'wdCharacter = 1
      aRng.MoveEnd Unit:=1, Count:=1
      If aRng.Fields.Count > 0 Then
        aSheet.Range("D" & iRow) = aRng.Fields(1).Code.Text
      Else
        aSheet.Range("D" & iRow) = "No field"
      End If ''
      aRng.Collapse Direction:=0      'wdCollapseEnd
    Loop
  End With
      
  wdDoc.Close False
  
  Set aRng = Nothing
  Set wdDoc = Nothing
  wdApp.Visible = True
  wdApp.Quit
  Set wdApp = Nothing

End Sub
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote
  #3  
Old 10-25-2021, 04:44 AM
scienceguy scienceguy is offline Determining if an in-text table reference was created with Word's native cross reference feature Windows 10 Determining if an in-text table reference was created with Word's native cross reference feature Office 2016
Advanced Beginner
Determining if an in-text table reference was created with Word's native cross reference feature
 
Join Date: Feb 2019
Posts: 46
scienceguy is on a distinguished road
Default

Thanks, Andrew! You are awesome!!
Reply With Quote
Reply

Tags
microsoft word

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Determining if an in-text table reference was created with Word's native cross reference feature Cross Reference Text mgdawn672017 Word 3 09-17-2018 03:05 PM
Determining if an in-text table reference was created with Word's native cross reference feature extra space when I cross-reference a table or figure Toto Word 1 06-07-2014 03:51 PM
cross reference hyperlinks to text box nothing_kills Word 4 11-25-2013 09:21 AM
Determining if an in-text table reference was created with Word's native cross reference feature Reference number and cross reference for equation numbers to match the thesis format wmac Word 1 05-14-2013 08:54 PM
Cross-reference feature problems in Word 2010 bannisa Word 0 02-13-2011 03:28 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 03:32 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft