Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 09-15-2021, 09:01 AM
Shelley Lou Shelley Lou is offline VBA Formatting Fields within parentheses Windows 10 VBA Formatting Fields within parentheses Office 2016
Competent Performer
VBA Formatting Fields within parentheses
 
Join Date: Dec 2020
Posts: 163
Shelley Lou is on a distinguished road
Default VBA Formatting Fields within parentheses

Hi, I have created the code below to format fields within brackets that appear directly after references to clauses (the fields contain the heading to the clause). The format for these fields should be italics and not bold. The code works but when run it also removes the bold formatting from the numbering fields for Schedule and Part (see page 2). Can anyone help me tweak the code below to not change the schedule/part numbering fields and only change the fields within the brackets. By way of reference the text fields after clauses are inserted via Cross Reference Paragraph Text. Thanks

Field Test macro.docx



Code:
Sub DPU_Banking_HeadingsInBrackets()
Application.ScreenUpdating = False
Dim Para As Paragraph, oRng As Range
ActiveWindow.View.ShowFieldCodes = Not ActiveWindow.View.ShowFieldCodes
Set oRng = ActiveDocument.Range
With oRng.Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = " \* MERGEFORMAT "
.Replacement.Text = ""
.Execute Replace:=wdReplaceAll
End With
With oRng.Find
.Replacement.Font.Bold = False
.Text = "^d"
.Replacement.Text = "^&"
.Format = True
.Execute Replace:=wdReplaceAll
End With
With oRng.Find
.Replacement.Font.Italic = True
.Text = "(^d"
.Replacement.Text = "^&"
.Wrap = wdFindContinue
.Format = True
.Execute Replace:=wdReplaceAll
End With
With oRng.Find.Replacement.Font
.Bold = False
.Italic = False
End With
With Selection.Find
.Text = " \h "
.Replacement.Text = "^& \* Charformat "
.Format = True
.Execute Replace:=wdReplaceAll
End With
With oRng.Find
.Text = "("
.Replacement.Text = "("
.Format = True
.Execute Replace:=wdReplaceAll
End With
ActiveWindow.View.ShowFieldCodes = Not ActiveWindow.View.ShowFieldCodes
Application.ScreenUpdating = True
End Sub
Reply With Quote
  #2  
Old 09-15-2021, 10:54 AM
Charles Kenyon Charles Kenyon is offline VBA Formatting Fields within parentheses Windows 10 VBA Formatting Fields within parentheses Office 2019
Moderator
 
Join Date: Mar 2012
Location: Sun Prairie, Wisconsin
Posts: 9,081
Charles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant future
Default

No help with the vba, I'm afraid, but you really, really, really, should be using styles for this. Then you would not need a macro but simply modify the style. Now that you have it in direct formatting, you should probably use your macro to apply a character style once you have the macro working.
Reply With Quote
  #3  
Old 09-15-2021, 08:36 PM
Guessed's Avatar
Guessed Guessed is offline VBA Formatting Fields within parentheses Windows 10 VBA Formatting Fields within parentheses 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

I wouldn't do this with search and replace. Try this code as an alternative
Code:
Sub SetFieldFormat()
  Dim aFld As Field, sCode As String
  For Each aFld In ActiveDocument.Fields
    sCode = LCase(aFld.Code)
    If sCode Like "* ref *" Then
      Debug.Print sCode
      If Not sCode Like "*\w*" Then
        aFld.Result.Font.Italic = True
        aFld.Result.Font.Bold = False
        If sCode Like "*mergeformat*" Then
          sCode = Replace(sCode, "merge", "char")
        ElseIf Not sCode Like "*charformat*" Then
          sCode = sCode & " \* charformat "
          aFld.Code.Text = sCode
        End If
        aFld.Update
      End If
    End If
  Next aFld
End Sub
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote
  #4  
Old 09-16-2021, 02:31 AM
Shelley Lou Shelley Lou is offline VBA Formatting Fields within parentheses Windows 10 VBA Formatting Fields within parentheses Office 2016
Competent Performer
VBA Formatting Fields within parentheses
 
Join Date: Dec 2020
Posts: 163
Shelley Lou is on a distinguished road
Default VBA Formatting Fields within parentheses

Hi Andrew, thank you so much for your reply and the code. I've run the code on my 200 page document and the following happens:

If fields that are already formatted unbold and italics remain correctly formatted, the code does not change these which is brilliant.

Adding new fields within brackets just formats them unbold and not italics so this needs to change to italics.

I'm not sure what to change to correct this.

Shelley
Reply With Quote
  #5  
Old 09-16-2021, 04:09 AM
Guessed's Avatar
Guessed Guessed is offline VBA Formatting Fields within parentheses Windows 10 VBA Formatting Fields within parentheses 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

How about we identify the fields in a different way and use the Character style that makes it italic.
Code:
Sub SetFieldFormat()
  Dim aFld As Field, sCode As String
  For Each aFld In ActiveDocument.Fields
    sCode = LCase(aFld.Code)
    If sCode Like "* ref *" Then
      Debug.Print sCode
      If aFld.Result.Words.First.Previous = "(" Then
        Debug.Print aFld.Result.Words.First.Previous
        If sCode Like "*mergeformat*" Then
          sCode = Replace(sCode, "mergefield", "charfield")
        ElseIf Not sCode Like "*charformat*" Then
          sCode = sCode & " \* charformat "
          aFld.Code.Text = sCode
        End If
        aFld.Result.Style = "Emphasis"
        aFld.Update
      End If
    End If
  Next aFld
End Sub
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote
  #6  
Old 09-16-2021, 09:33 AM
Shelley Lou Shelley Lou is offline VBA Formatting Fields within parentheses Windows 10 VBA Formatting Fields within parentheses Office 2016
Competent Performer
VBA Formatting Fields within parentheses
 
Join Date: Dec 2020
Posts: 163
Shelley Lou is on a distinguished road
Default VBA Formatting Fields within parentheses

Hi Andrew, unfortunately that didn't work, on one document the code wouldn't even run. I've checked the toggle codes on 3 documents, there seems to be both \r and \w
field codes.PNG

When inserting the style Emphasis I get a debug and it changes the clause cross reference number to italics
debug.PNG

I will keep trying
Reply With Quote
  #7  
Old 09-16-2021, 03:52 PM
Guessed's Avatar
Guessed Guessed is offline VBA Formatting Fields within parentheses Windows 10 VBA Formatting Fields within parentheses 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

It looks like you are mix and matching code from both macros I posted. If you want to do that then you MUST look at the differences and work out what the differences mean.

The changes I made to the second macro don't rely on the presence of the \w.

When you say the code wouldn't even run, can you cast any light on what line was failing.
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote
  #8  
Old 09-17-2021, 12:52 AM
Shelley Lou Shelley Lou is offline VBA Formatting Fields within parentheses Windows 10 VBA Formatting Fields within parentheses Office 2016
Competent Performer
VBA Formatting Fields within parentheses
 
Join Date: Dec 2020
Posts: 163
Shelley Lou is on a distinguished road
Default VBA Formatting Fields within parentheses

Hi Andrew, many thanks for your reply. The documents themselves come from external clients so they are not in-house templates unfortunately.

Your second macro when run debugs as per the image below. It does remove the bold but doesn't change them to italics.

I went back to trying the original code on one particular document and it debugs as per the image in my previous post and changes the cross ref digits to italics which is incorrect, only the cross ref headings in brackets should be in italics. So I changed the \w to \r and that put the cross ref digits back to how they should be but still didn't change the headings in brackets to italics. Hope that helps.

debug.PNG
Reply With Quote
  #9  
Old 09-17-2021, 09:36 AM
Shelley Lou Shelley Lou is offline VBA Formatting Fields within parentheses Windows 10 VBA Formatting Fields within parentheses Office 2016
Competent Performer
VBA Formatting Fields within parentheses
 
Join Date: Dec 2020
Posts: 163
Shelley Lou is on a distinguished road
Default VBA Formatting Fields within parentheses

Andrew, I think I've fixed the problem with the italics, it still debugs on one of my testing documents but I can live with that for that particular document, I just added aFld.Code.Italic = True and it now works

Code:
Sub SetFieldFormat()
  Dim aFld As Field, sCode As String
  For Each aFld In ActiveDocument.Fields
    sCode = LCase(aFld.Code)
    If sCode Like "* ref *" Then
      Debug.Print sCode
      If aFld.Result.Words.First.Previous = "(" Then
        Debug.Print aFld.Result.Words.First.Previous
        If sCode Like "*mergeformat*" Then
          sCode = Replace(sCode, "mergefield", "charfield")
        ElseIf Not sCode Like "*charformat*" Then
          sCode = sCode & " \* charformat "
          aFld.Code.Text = sCode
          aFld.Code.Italic = True
        End If
        aFld.Update
      End If
    End If
  Next aFld
End Sub
Reply With Quote
Reply

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Extract numbers from parentheses and add them up pokeyarw Excel 9 05-06-2019 09:50 PM
add parentheses to a letter steve8778 Word 6 11-17-2016 07:50 AM
VBA Formatting Fields within parentheses Allignment of numbers with parentheses RussBell Mail Merge 2 10-28-2016 05:51 AM
VBA Formatting Fields within parentheses Parentheses Changing on Document Subject1157 Word 2 06-16-2011 08:46 PM
Problem With Formatting Mailmerge Fields revans611 Mail Merge 0 05-31-2009 06:20 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 07:02 AM.


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