Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 08-04-2021, 05:55 AM
CharlieM CharlieM is offline Duplicating Ranges and changing Font Attributes Windows 10 Duplicating Ranges and changing Font Attributes Office 2016
Novice
Duplicating Ranges and changing Font Attributes
 
Join Date: Aug 2021
Posts: 2
CharlieM is on a distinguished road
Default Duplicating Ranges and changing Font Attributes


I have a Word document which has Hyperlinks which may contain subscripts. I have a special Character Style("MyStyle"), which I sometimes need to convert these Hyperlinks to. But if I select each Hyperlink as a Range and then change the Range.Font to MyStyle, the Range.Text all returns to plain, and the subscripts are lost. I have tried pasting the Range Text after the Range and then using a For Loop to try and duplicate the Font Attributes, also creating a second Range and again using a For Loop, all to no avail.

newRg = .Range.Duplicate
newRg.SetRange(.Range.Start + Len(.Range.Text), .Range.End + Len(.Range.Text))
newRg.Select() ' This attempt to move newRg to after .Range has no effect

newRg.Start = .Range.Start + Len(.Range.Text)
newRg.End = .Range.End + Len(.Range.Text) ' Nor does this ...

The second part of the problem is the Font Change:

newRg.Style = "MyStyle" 'here the subscripts are lost

For i = 1 To .Range.Characters.Count
newRg.Characters(i + Len(.Range.Text)).Font.Subscript = .Range.Characters(i).Font.Subscript
Next ' And this has no effect at all.

I fear I am missing something very fundamental! Could anyone help please? If I could simply get the two (identical) ranges positioned one after the other, then I could use the For Loop as shown. But duplicating the range and then trying to move it does not seem to work.

Last edited by CharlieM; 08-04-2021 at 10:34 PM.
Reply With Quote
  #2  
Old 08-05-2021, 09:15 PM
gmayor's Avatar
gmayor gmayor is offline Duplicating Ranges and changing Font Attributes Windows 10 Duplicating Ranges and changing Font Attributes Office 2019
Expert
 
Join Date: Aug 2014
Posts: 4,138
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

The problem is that your style is a character style. Change the style type to a linked paragraph and character style and the macro is should be unnecessary.
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote
  #3  
Old 08-10-2021, 04:05 AM
CharlieM CharlieM is offline Duplicating Ranges and changing Font Attributes Windows 10 Duplicating Ranges and changing Font Attributes Office 2016
Novice
Duplicating Ranges and changing Font Attributes
 
Join Date: Aug 2021
Posts: 2
CharlieM is on a distinguished road
Default

OK, I have re-created the style as a linked paragraph and character style, but now my ".Find" loop never finds it!
With rg.Find
.Forward = True
.Format = True
.Wrap = wdFindContinue
.Style = "MyStyle"
allTheTips = ""

Do While .Execute
rg.Select()
... ' Processing happens here...
...
Loop
End With

I have given the new style the same name as the old style (which I have deleted). I am wondering if I have discovered a Word 'idiosyncracy'?!
Reply With Quote
  #4  
Old 08-10-2021, 04:44 AM
Guessed's Avatar
Guessed Guessed is offline Duplicating Ranges and changing Font Attributes Windows 10 Duplicating Ranges and changing Font Attributes Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 4,166
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

Charlie, I don't think you are missing anything fundamental - it is an idiosyncracy. This is how Word chooses to apply Character styles - it removes local font settings when you apply it. You also can't stack Character styles (apply more than one at a time).

If you know your way around xml you could edit the document.xml to replace the Hyperlink style with MyStyle. This preserves the other local formatting.

When you need to do this - why do you need to do it? Could you modify the attributes of the Hyperlink style instead so that it looked like MyStyle?
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote
Reply

Tags
copying ranges, range styles/fonts



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to write individual lines of text with changing attributes. sts023 Word VBA 0 05-26-2019 07:43 AM
Duplicating Ranges and changing Font Attributes Take String of numbers, expand ranges, sort, then compress back into ranges AustinBrister Word VBA 19 08-22-2016 05:18 PM
Changing font TonyH Outlook 0 05-22-2014 07:58 AM
Duplicating Ranges and changing Font Attributes Changing font in VBA blaze Word VBA 1 04-15-2012 02:21 AM
changing font size without changing leading carolns Word 1 09-14-2009 12:30 PM

Other Forums: Access Forums

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


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2025, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft