#1
|
|||
|
|||
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 |
#2
|
|||
|
|||
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.
|
#3
|
||||
|
||||
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 |
#4
|
|||
|
|||
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 |
#5
|
||||
|
||||
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 |
#6
|
|||
|
|||
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 |
#7
|
||||
|
||||
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 |
#8
|
|||
|
|||
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 |
#9
|
|||
|
|||
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 |
|
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 |
Allignment of numbers with parentheses | RussBell | Mail Merge | 2 | 10-28-2016 05:51 AM |
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 |