locked
Cross-reference annoyances - How to make default lowercase first letter? How to get only the number? RRS feed

  • Question

  • 1. Almost every time I add a cross-reference I need to change it by clicking on edit / lowercase. How can I make lowercase the defult option? Or how can I make a macro to do that!

    2. How can I insert only the reference number, ex: See figure 1 and 2. Using corss-reference it will be: See figure 1 and figure 2. To remove the text "figure" I found only one HARD way:

    insert \* Arabi into the code. Ex:

    normal: { REF _Ref154458335 \h }
    only number: { REF _Ref154458335 \* Arabic \h }

    How can I macro that since I can not right click when recording a macro (Im noob with macros).

     

    Tuesday, June 28, 2011 4:47 PM

Answers

  • Hi Pedro,

    That makes the code much more complex, because Word provides no direct way of determining whether the selection is in a field:

    Sub MakeLowRef()
    Application.ScreenUpdating = False
    Dim StrExt As String, Rng As Range
    With Selection
      Set Rng = .Range
    FoundField:
      If .Fields.Count > 0 Then
        With .Fields(1)
          If .Type = wdFieldRef Then
            If InStr(.Code.Text, "\* lower") > 0 Then Exit Sub
            If InStr(.Code.Text, "\h") > 0 Then StrExt = " \h"
            .Code.Text = Split(.Code.Text, "\h")(0) & "\* lower" & StrExt
          End If
          .Update
        End With
      Else
        Call GetField
        If .Fields.Count > 0 Then GoTo FoundField
      End If
    End With
    Rng.Select
    Set Rng = Nothing
    Application.ScreenUpdating = True
    End Sub

    Sub MakeNumRef()
    Application.ScreenUpdating = False
    Dim StrExt As String, Rng As Range
    With Selection
      Set Rng = .Range
    FoundField:
      If .Fields.Count > 0 Then
        With .Fields(1)
          If .Type = wdFieldRef Then
            If InStr(.Code.Text, "\#") > 0 Then Exit Sub
            If InStr(.Code.Text, "\h") > 0 Then StrExt = " \h"
            .Code.Text = Split(.Code.Text, "\h")(0) & "\# 0" & StrExt
          End If
          .Update
        End With
      Else
        Call GetField
        If .Fields.Count > 0 Then GoTo FoundField
      End If
    End With
    Application.ScreenUpdating = True
    End Sub

    Private Sub GetField()
    Dim lPos As Long
    ActiveDocument.ActiveWindow.View.ShowFieldCodes = False
    With Selection
      lPos = .Start
      'Use ToggleShowCodes to move the selection if we're not already at the start of a field
      .Fields.ToggleShowCodes
      .Fields.ToggleShowCodes
      'Retry one character later, in case the selection was already at the start of the field
      If lPos = .Start Then
        .Start = .Start + 1
        lPos = .Start
        .Fields.ToggleShowCodes
        .Fields.ToggleShowCodes
      End If
      'If the selection has moved, we're in a field
      If lPos <> .Start Then
        .Fields.ToggleShowCodes
        'Find the end of the field
        .MoveEndUntil cset:=Chr(21), Count:=256
        .Fields.ToggleShowCodes
      End If
    End With
    End Sub

     


    Cheers
    Paul Edstein
    [MS MVP - Word]
    • Marked as answer by Nick Wan Monday, July 4, 2011 1:17 AM
    Saturday, July 2, 2011 2:05 AM

All replies

  • Another user, Ferrety2011, has replied to a thread you have subscribed to in the Word IT Pro Discussions Forum.

    Thread Title Cross-reference annoyances - How to make default lowercase first letter? How to get only the number?
    Started by: PedroDD

    Reply:

    You can record a new macro that will insert that information for you doing this:

    1. Click on one of the cells that need to be changed.

    2. Start recording macro, give it a short cut key if you like, for ease of using later.

    3. Use only the formula bar, do not click on any cells during the macro recording. Type in the new line of information into the Formula bar.

    4. Click on the Check Park next to the formula bar to finalize the change to the cell.

    5. Stop recording.

    Now, using your keyboard short cut you assigned, navigate to one of the cells that needs the exact same information and use the short cut. The macro you recorded only sees to use the active cell and put the new string of info in. Then it knows to finalize the active cell. Because you don't click on any cells during the macro, it does not identify any specific location that it has to be in so it becomes a universal tool.

    An example of what the code would look like after you're done recording:

    Sub Exmaple_1()
    '
    ' Exmaple_1 Macro
    '
    ' Keyboard Shortcut: Ctrl+u
    '
        ActiveCell.FormulaR1C1 = "{ REF _Ref154458335 \h }"
    End Sub

    The downside is that it will only put in the info that you record with. So if each cell with it's reference that you mention above will be different, this method won't work. BUT, if you need it to always insert either { REF _Ref154458335 \h }, or { REF _Ref154458335 \* Arabic \h }, exactly, then this will work well for you.


    The Microsoft Developer Network
    Wednesday, June 29, 2011 5:10 PM
  • This macro is for excel..
    Wednesday, June 29, 2011 5:14 PM
  • You can't really change how the Insert|Cross-Reference dialogue works. However, you could expedite the lower-case & number-only conversions with macros.

    For #1, you could select the cross-ref and use a macro like:

    Sub MakeLowRef()
    Dim StrExt As String
    With Selection
      If .Fields.Count > 0 Then
        With .Fields(1)
          If .Type = wdFieldRef Then
            If InStr(.Code.Text, "\* lower") > 0 Then Exit Sub
            If InStr(.Code.Text, "\h") > 0 Then StrExt = " \h"
            .Code.Text = Split(.Code.Text, "\h")(0) & "\* lower" & StrExt
          End If
          .Update
        End With
      End If
    End With
    End Sub

    For #2, you could select the cross-ref and use a macro like:

    Sub MakeNumRef()
    Dim StrExt As String
    With Selection
      If .Fields.Count > 0 Then
        With .Fields(1)
          If .Type = wdFieldRef Then
            If InStr(.Code.Text, "\#") > 0 Then Exit Sub
            If InStr(.Code.Text, "\h") > 0 Then StrExt = " \h"
            .Code.Text = Split(.Code.Text, "\h")(0) & "\# 0" & StrExt
          End If
          .Update
        End With
      End If
    End With
    End Sub


    Cheers
    Paul Edstein
    [MS MVP - Word]
    Friday, July 1, 2011 6:06 AM
  • Thank you!

    Using your macro I need to select all the cross-reference to make it work.

    Is there a away that if the text cursor is over the reference is enought? So I dont need to select all the reference text by myself.

    Friday, July 1, 2011 12:02 PM
  • Hi Pedro,

    That makes the code much more complex, because Word provides no direct way of determining whether the selection is in a field:

    Sub MakeLowRef()
    Application.ScreenUpdating = False
    Dim StrExt As String, Rng As Range
    With Selection
      Set Rng = .Range
    FoundField:
      If .Fields.Count > 0 Then
        With .Fields(1)
          If .Type = wdFieldRef Then
            If InStr(.Code.Text, "\* lower") > 0 Then Exit Sub
            If InStr(.Code.Text, "\h") > 0 Then StrExt = " \h"
            .Code.Text = Split(.Code.Text, "\h")(0) & "\* lower" & StrExt
          End If
          .Update
        End With
      Else
        Call GetField
        If .Fields.Count > 0 Then GoTo FoundField
      End If
    End With
    Rng.Select
    Set Rng = Nothing
    Application.ScreenUpdating = True
    End Sub

    Sub MakeNumRef()
    Application.ScreenUpdating = False
    Dim StrExt As String, Rng As Range
    With Selection
      Set Rng = .Range
    FoundField:
      If .Fields.Count > 0 Then
        With .Fields(1)
          If .Type = wdFieldRef Then
            If InStr(.Code.Text, "\#") > 0 Then Exit Sub
            If InStr(.Code.Text, "\h") > 0 Then StrExt = " \h"
            .Code.Text = Split(.Code.Text, "\h")(0) & "\# 0" & StrExt
          End If
          .Update
        End With
      Else
        Call GetField
        If .Fields.Count > 0 Then GoTo FoundField
      End If
    End With
    Application.ScreenUpdating = True
    End Sub

    Private Sub GetField()
    Dim lPos As Long
    ActiveDocument.ActiveWindow.View.ShowFieldCodes = False
    With Selection
      lPos = .Start
      'Use ToggleShowCodes to move the selection if we're not already at the start of a field
      .Fields.ToggleShowCodes
      .Fields.ToggleShowCodes
      'Retry one character later, in case the selection was already at the start of the field
      If lPos = .Start Then
        .Start = .Start + 1
        lPos = .Start
        .Fields.ToggleShowCodes
        .Fields.ToggleShowCodes
      End If
      'If the selection has moved, we're in a field
      If lPos <> .Start Then
        .Fields.ToggleShowCodes
        'Find the end of the field
        .MoveEndUntil cset:=Chr(21), Count:=256
        .Fields.ToggleShowCodes
      End If
    End With
    End Sub

     


    Cheers
    Paul Edstein
    [MS MVP - Word]
    • Marked as answer by Nick Wan Monday, July 4, 2011 1:17 AM
    Saturday, July 2, 2011 2:05 AM
  • Your code for displaying the number only works.  However, I have my figures numbered using the chapter number and the number of figures under that chapter heading e.g. 5-3 for chapter 5, figure 3.  Is there any way to have the number only be displayed in this format?  Using your method I get a number like 9 instead of 5-3.

    Also is there a way to do this just with a field code?  

    I found a method that does this using bookmarks but it seems messy and I would have to remember the name of the bookmark for each figure since part of the reason for using captioned numbered figures is that the cross-liked figures will auto-update if figures are added or removed.

    Thanks in advance!

    Saturday, March 17, 2012 12:36 AM
  • AFAIK, the only practical way is to bookmark the SEQ field that produces the '5-3', then cross-reference the bookmark. The same applies to captions that use letters instead of numbers.

    Cheers
    Paul Edstein
    [MS MVP - Word]

    Sunday, March 18, 2012 4:52 AM