Word VBA Macro Assistance

Steve001

Board Regular
Joined
Apr 13, 2017
Messages
91
Office Version
  1. 365
  2. 2021
  3. 2013
Platform
  1. Windows
Hello All,

I wish to modify the macro that is in this post


Sub RefColorizer()
Application.ScreenUpdating = False
Dim Fld As Field, StrTxt As String
With ActiveDocument
For Each Fld In .Fields
With Fld
If .Type = wdFieldRef Then
StrTxt = Trim(.Code.Text)
If InStr(StrTxt, "\h") > 0 Then
StrTxt = "REF " & Split(StrTxt, " ")(1) & " \* Charformat \h"
.Code.Text = StrTxt: .Code.Font.Underline = True: .Code.Font.ColorIndex = wdBlue
End If
End If
End With
Next
.Fields.Update
End With
Application.ScreenUpdating = True
End Sub

This code works fantastic, i have come across an older document that is set as follows:

Reference Type: Numbered Item
Insert Reference to: Paragraph Number

The above code changes these to

Reference Type: Heading
Insert Reference to: Heading Text

Can you please advise what to change

I am using office 363 & office 2021

Regards

Steve
 
The code you posted is incapable of changing the actual references to something else; all it can do is display the reference as underlined blue text.

PS: When posting VBA code, kindly post formatted code using the VBA code tags - on the posting menu.
 
Upvote 0
The code you posted is incapable of changing the actual references to something else; all it can do is display the reference as underlined blue text.

PS: When posting VBA code, kindly post formatted code using the VBA code tags - on the posting menu.

Hi Macropod,

first - apologies for not posing code correctly, i see the VBA Icon now.

When i run the code, i was horrified to find it did change the numbered references to headings of the numbered item.
Thank god for snapshots on the server.


is it possible to make up a macro like you did before for me, but instead of the headings, it changes the paragraph number to blue and underline if they are not set.


Regards

Steve
 
Upvote 0
Try:
VBA Code:
Sub RefColorizer()
Application.ScreenUpdating = False
Dim Fld As Field, StrTxt As String
With ActiveDocument
For Each Fld In .Fields
  With Fld
    If .Type = wdFieldRef Then
      StrTxt = Trim(Split(Split(Split(.Code.Text, "\h")(0), "\* Mergeformat")(0), "\* Charformat")(0)) & " \* Charformat \h"
      .Code.Text = StrTxt: .Code.Font.Underline = True: .Code.Font.ColorIndex = wdBlue
    End If
  End With
Next
.Fields.Update
End With
Application.ScreenUpdating = True
End Sub
It is not possible to change the character formatting of only part of the field.
 
Upvote 0
Solution

Forum statistics

Threads
1,226,771
Messages
6,192,924
Members
453,767
Latest member
922aloose

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top