![]() |
|
#1
|
|||
|
|||
![]() I am writing a macro to change the gender in a Will from the husband to the wife where they make complementary Wills. I have the following in a module. When I tried to insert the selected it was capitalised but not bold. Any clues? Code:
Sub WillGenderChange21April() ' ' WillGenderChange21April Macro Dim SavedTexts As Object Dim rng As Range Set rng = Selection.Range Selection.HomeKey Unit:=wdStory Selection.GoTo What:=wdGoToBookmark, Name:="Husband" SelectNameForWillSubSub 'This calls sub SelectNameForWillSubSub ' Move to a new location Selection.GoTo What:=wdGoToBookmark, Name:="Wife" If SavedTexts.Exists(Husband) Then Selection.FormattedText = SavedTexts(Husband).FormattedText 'SelectNameForWillSubSub 'This calls sub ' Insert the stored text at the new location Selection.TypeText Husband 'UserSelection 'It works to here to select the full name. Now to assign 'the selection to a variable and then delete it End Sub Sub SelectNameForWillSubSub() '' SelectNameForWillSubSub Macro ' Dim rng As Range Set rng = Selection.Range Dim selectedText As String 'SetSavedTexts = CreateObject("Scripting.Dictionary") With rng.Find .ClearFormatting .Font.Bold = True .MatchCase = True .Text = "[A-Z]{1,}" .MatchWildcards = True .Execute End With ' If bold uppercase text is found, extend selection If rng.Find.Found Then Do While rng.Characters.Last.Font.Bold = True And _ rng.Characters.Last.Text Like "[A-Z ]" ' Allow spaces rng.MoveEnd wdCharacter, 1 Loop End If ' Retract selection if a lowercase character was picked up If rng.Characters.Last.Text Like "[a-z]" Then rng.MoveEnd wdCharacter, -1 End If rng.Select End Sub Last edited by macropod; 04-21-2025 at 12:16 AM. Reason: Added code tags for code formatting |
#2
|
||||
|
||||
![]()
It is not clear what your macro is actually doing. What is contained originally in the bookmarked ranges 'Husband' and 'Wife'?
If the aim is to simply transpose the contents of the two bookmarked ranges then the following will do that: Code:
Sub WillGenderChange21April() Dim sHusband As String, sWife As String If ActiveDocument.Bookmarks.Exists("Husband") = True Then sHusband = ActiveDocument.Bookmarks("Husband").Range.Text End If If ActiveDocument.Bookmarks.Exists("Wife") = True Then sWife = ActiveDocument.Bookmarks("Wife").Range.Text End If FillBM "Wife", sHusband FillBM "Husband", sWife End Sub Private Sub FillBM(strbmName As String, strValue As String) 'Graham Mayor - https://www.gmayor.com Dim oRng As Range With ActiveDocument On Error GoTo lbl_Exit If .Bookmarks.Exists(strbmName) = True Then Set oRng = .Bookmarks(strbmName).Range oRng.Text = strValue oRng.Bookmarks.Add strbmName End If End With lbl_Exit: Set oRng = Nothing Exit Sub End Sub
__________________
Graham Mayor - MS MVP (Word) (2002-2019) Visit my web site for more programming tips and ready made processes www.gmayor.com |
#3
|
||||
|
||||
![]()
If that's the case, the simplest way would be to use two custom document properties - SpouseA and SpouseB, with corresponding DOCPROPERTY fields at the relevant locations and simply swap the document property contents, then do a print preview to update the display. The same could be done for variable his/her, him/her, he/she & husband/wife references, too. Indeed, for the his/her, him/her, he/she & husband/wife references, a single custom document property could be used, holding just the sex (m/f) and the custom document property fields could be combined with IF field tests to conditionally output the his/her, him/her, he/she & husband/wife content, as appropriate. basically, just three custom document properties for all the variations you need to deal with - two for the names & one for the sex.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#4
|
|||
|
|||
![]()
I'm sorry if this is a repeat because I was typing a reply that seemed to disappear.
Where a husband and wife want to make wills that are mirror images of each other I prepare the husband's will and then substitute their names in relevant places (and change words such as "she" to "he" etc , which I assume would be simple). The husband's name is substituted once and the wife's name twice. The name of the spouse to be selected must be first name, surname, and however many middle names they have. I created Sub SelectNameForWillSubSub() which selects all bold uppercase text after the bookmark "Husband" until the first lowercase character. However, when I try to insert it elsewhere it has only saved in uppercase format and not bold. I thought that by saving each name is a variable it would then be simple to insert it in the relevant places. Subject to your guidance, my problem is to be able to save each name as a variable in bold uppercase so that I can then insert them where needed. |
#5
|
||||
|
||||
![]()
As I said, this would be far simpler if you used custom document properties and DOCPROPERTY fields.
String variables are just that - strings. As such, they have no formatting attributes.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#6
|
|||
|
|||
![]()
Thanks, Paul. As I am a neophyte I have no idea of DOCPROPERTY fields. Where do I go to explore them?
|
#7
|
||||
|
||||
![]()
All the work is in the setup. Application once that's done is trivial.
Here's how you go about the setup: 1. Go to File|Info|Properties>Advanced Properties>Custom 2. Create 3 Custom properties - SpouseA, SpouseB, Sex, giving them the values 'Husband', 'Wife' and 'M', repsectively. 4. In your document, at the first location where you want the husband's name to appear in a husband's will, press Ctrl-F9 to create a pair of field braces (i.e. { }') and type 'DOCPROPERTY SpouseA' between them, so that you get {DOCPROPERTY SpouseA}. 5. Press F9 to update the display, then copy the field, which should now say 'Husband', to wherever else you want it to appear. 6. Repeat steps 4 & 5 for the SpouseB property, for wherever the wife's name is to appear. You should now have a basic setup, with 'Husband' and 'Wife' showing in all the right places. Now go back to the custom document properties dialog and swap 'Husband' and 'Wife', then switch back to the document and either press Ctrl-A, F9 or do a print preview, and you should see all the 'Husband' and 'Wife' references have swapped over in the document. It's that simple. Undo those changes for now. Continuing with the Sex document property: 1. Select a location where the text should say he/she. 2. Press Ctrl-F9 twice to create a nested pair of field braces (i.e. { { } }') 3. Fill in the field braces so that you get {IF{DOCPROPERTY Sex}= "M" "he" "she"}, making sure you have all the spaces indicated. 4. Press F9 to update the display, then copy the field, which should now say 'he', to wherever else you want he/she to appear. 5. Repeat steps 3 & 4 for the 'him/her', 'his/her' & 'husband/wife' variable content. 6. Do the same for where the M/F outputs should be inverted, as in, for example, {IF{DOCPROPERTY Sex}= "M" "she" "he"} Now go back to the custom document properties dialog and swap 'Husband' and 'Wife', and change the Sex property to 'F', then switch back to the document and either press Ctrl-A, F9 or do a print preview, and you should see all the 'Husband' and 'Wife' and he/she, etc. references have swapped over in the document. Simple as. Now for some simple tweaks with the SpouseA & SpouseB properties. So far, the field outputs will be exactly the same as the property values, without regard to formatting. You apparently want some of the outputs to be All-Caps. For that, you can simply apply the \* Upper switch to the applicable DOCPROPERTY field, as in: {DOCPROPERTY SpouseA \* Upper} Of course, if you only ever want the spouse names to be in Upper case, you could just input them into the SpouseA & SpouseB properties that way and skip this switch. You also apparently want some of the outputs to be Bold. For that, you can simply apply the \* CHARFORMAT switch to the applicable DOCPROPERTY field and apply bold formatting to at least the 'D' of 'DOCPROPERTY', as in: {DOCPROPERTY SpouseA \* CHARFORMAT} You can even combine that with the \* Upper switch, as in: {DOCPROPERTY SpouseA \* Upper \* CHARFORMAT} That should take care of the setup. From now on, all you need do is update the three custom document properties, then refresh the document's display. And that could be automated with a macro as simple as: Code:
Sub WillSetup() With ActiveDocument .CustomDocumentProperties("SpouseA").Value = Trim(InputBox("Testator:")) .CustomDocumentProperties("Sex").Value = UCase(Left(Trim(InputBox("Testator's Sex:")),1)) .CustomDocumentProperties("SpouseB").Value = Trim(InputBox("Beneficiary:")) .Fields.Update End With End Sub Code:
Sub WillSwap() With ActiveDocument Dim StrTmp As String: StrTmp = .CustomDocumentProperties("SpouseA").Value .CustomDocumentProperties("SpouseA").Value = .CustomDocumentProperties("SpouseB").Value .CustomDocumentProperties("SpouseB").Value = StrTmp Select Case UCase(.CustomDocumentProperties("Sex").Value) Case "M": .CustomDocumentProperties("Sex").Value = "F" Case "F": .CustomDocumentProperties("Sex").Value = "M" End Select .Fields.Update End With End Sub See attached demo. The various fields described above are accessible via Insert|Quick Parts>Field, but that's difficult to use for the IF tests associated with the he/she conditional outputs. For more on fields and field switches, see: List of field codes in Word - Microsoft Support Format field results - Microsoft Support
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#8
|
||||
|
||||
![]()
I would use mapped dropdown content controls to remove the need to use vba at all.
See the attached demo which has a series of content controls all mapped to the same built in document property. Each drop down has two entries where the displayed text is controlled by the stored value.
__________________
Andrew Lockton Chrysalis Design, Melbourne Australia |
#9
|
|||
|
|||
![]()
Hi Chaps
I can't understand how such a simple task needs to be coded in such a complex way. I wrote a WordPerfect macro for this over ten years ago and, while not elegant, it was simple to write and worked as needed. Thanks Andrew but the point of my exercise is to try and understand and learn VBA so that I can perform simple tasks, that's why I'm struggling along with VBA. I have achieved a few simple macros with Paul's kind help. DOCPROPERTY doesn't suit because I start with a standard precedent for all circumstances and I prefer not to insert field braces where they might not be needed. Bookmarks aren't as obvious. Same with Quick Parts. I start with that standard precedent with bookmarks in it that are irrelevant in many cases, but not intrusive. What are the complications that make it so hard to select text after a bookmark and then move it to another place, or places. For other changes I suppose that it is inelegant to simply go to a bookmarked "she" and delete the "s", or to amend "my wife" to "my husband" but that is simply done. I have had to abandon assigning selected text to variables and will try to code line by line, step by step, and see how it goes. Thanks for time you have clearly put into your responses, but you assume a level of sophistication that is beyond a Swannies supporter. |
#10
|
||||
|
||||
![]() Quote:
If you start with a template that has the basic setup, you can simply copy/paste the existing fields if you need additional copies of them, or delete them if not needed. It's not rocket science.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#11
|
|||
|
|||
![]()
I'm sorry, Paul, but it is rocket science to me. The coding is way above my pay rate, and I really do appreciate your efforts - you have put a lot into it. Microsoft blocked your macro and I had to unblock it. I was then left with a few words on a page that I didn't understand how to manipulate. Maybe when I have learned some more about the complexities of Word and VBA I could come back and have another attempt to understand it. As I said, I start with a standard document that I have put bookmarks in. Then a paralegal will do whatever is needed to personalise that document for the husband. I then have a document that is appropriate for the spouses and only need to swap names & change him/her, he she. I don't understand how DOCPROPERTY makes that easier, but I will try and work out how it might. I sense that your frustration is similar to mine.
|
#12
|
||||
|
||||
![]()
Hang on, I've given you a document with both the VBA coding and the field coding that you could simply copy & paste into your own template, and you say it's above your pay rate - when all you'd have to do is create three custom document properties???
Did you at least experiment with the macros I included in the document to see how they affect the output? You want your paralegal to type in all the name & his/her etc. content wherever required, then run your way more complex macro to swap the details, but they can't answer three simple prompts in a macro for supplying the testator's name & sex and the beneficiary's name? Then the second macro to swap them over with no further input? Is that what you're trying to tell us?
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#13
|
|||
|
|||
![]()
I realize I am a Johnny Come Late to this thread but after reading through it, I realized that I had done something similar for a rather progressive attorney a year or so ago who was trying to accommodate the "preferred pronoun" nonsense running rampant today.
Using the attached as a baseline template, you can define two parties, assign their respective role and their preferred pronouns. Once setup, it is a simple matter of displaying the include userform edit and swap roles. |
![]() |
|
![]() |
||||
Thread | Thread Starter | Forum | Replies | Last Post |
Find the Bold words | Sainath | Word | 7 | 02-27-2024 10:30 PM |
![]() |
thiagoafdoria | Word VBA | 4 | 12-02-2019 11:41 AM |
![]() |
jeffreybrown | Word | 4 | 08-20-2019 05:58 PM |
![]() |
Kalü | Word VBA | 22 | 04-24-2018 05:35 AM |
![]() |
Ezra | Word | 4 | 07-31-2017 06:53 AM |