Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 02-15-2019, 06:26 AM
FlyingBlind FlyingBlind is offline Why are style updates in VBA not reflected in the document? Windows 10 Why are style updates in VBA not reflected in the document? Office 2016
Novice
Why are style updates in VBA not reflected in the document?
 
Join Date: Feb 2019
Posts: 3
FlyingBlind is on a distinguished road
Question Why are style updates in VBA not reflected in the document?

Greetings. I have been coding VBA and vb.net for 15 years. I have made dozens of time-saving add-ins, applications, and macros in word, excel, access, and powerpoint. I have a question to which I cannot find an answer. I only wish to know "why".

I do not seek an alternative method for doing this (unless it's easy ;-)). I don't have code to show you. I know all about the virtues and vices of style templates. I do not seek to be chastised for wanting to do something so stupid. I only want to know why something does not work.

OK - with that as the caveat - If I am in a word document and select modify a style, select the font and change it from Arial to Times New Roman, when I am done with the dialogue all the text of that style changes from Arial to Times New Roman in the document text.

If I do the exact same thing in VBA, (find the style from the styles collection and modify the font) the text does not update. I run my code and examine the style in the style manager dialogue box I can see my changes (the font for the style shows Times New Roman), yet the text in the document stubbornly stays Arial. Why?

I can force the update by looping through the paragraph collection and applying the style (!) but I don't want to do that.



I have searched and searched and cannot find an answer to this question. Maybe one of you knows.
Reply With Quote
  #2  
Old 02-15-2019, 10:04 AM
kilroy kilroy is offline Why are style updates in VBA not reflected in the document? Windows 10 Why are style updates in VBA not reflected in the document? Office 2016
Competent Performer
 
Join Date: Sep 2016
Location: Southern Ontario
Posts: 122
kilroy is on a distinguished road
Default

If it's one thing I've learned here it's if your code doesn't work you've done something wrong and it's your fault. You can't share code why? Have a better attitude if you want help.

This works for me:

Code:
Sub ModifyStyle()
'
'
'
    Selection.Find.ClearFormatting
    Selection.Find.Style = ActiveDocument.Styles("Quote") 'Your Style Here
    Selection.Find.Replacement.ClearFormatting
    Selection.Find.Replacement.Font.Size = 14
    Selection.Find.Replacement.Font.Bold = True
    Selection.Find.Replacement.Font.Italic = True
    With Selection.Find
        .Text = "?"
        .Replacement.Text = "^&"
        .Forward = True
        .Wrap = wdFindContinue
        .Format = True
        .MatchCase = False
        .MatchWholeWord = False
        .MatchByte = False
        .MatchAllWordForms = False
        .MatchSoundsLike = False
        .MatchWildcards = True
    End With
    Selection.Find.Execute Replace:=wdReplaceAll
End Sub
Reply With Quote
  #3  
Old 02-15-2019, 03:03 PM
FlyingBlind FlyingBlind is offline Why are style updates in VBA not reflected in the document? Windows 10 Why are style updates in VBA not reflected in the document? Office 2016
Novice
Why are style updates in VBA not reflected in the document?
 
Join Date: Feb 2019
Posts: 3
FlyingBlind is on a distinguished road
Default Lol!

Thanks for the laugh! I needed it today
Reply With Quote
  #4  
Old 02-15-2019, 04:47 PM
macropod's Avatar
macropod macropod is offline Why are style updates in VBA not reflected in the document? Windows 7 64bit Why are style updates in VBA not reflected in the document? Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 22,467
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

Code like:
ActiveDocument.Styles("MyStyle").Font.Italic = True
works for me - all content in that Style is immediately updated to have the italic attribute. Of course, if someone has been overriding Style definitions with direct formatting, simply changing the Style definition may not be enough to get the desired result...
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #5  
Old 02-15-2019, 10:27 PM
Guessed's Avatar
Guessed Guessed is offline Why are style updates in VBA not reflected in the document? Windows 10 Why are style updates in VBA not reflected in the document? Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 4,176
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 would presume it doesn't work for you because the font name has been locally overridden on your instances of that style. Certainly the following works for me if there is no local font formatting.
Code:
Sub ChangeStyleDef()
  ActiveDocument.Styles("Normal").Font.Name = "Times New Roman"
End Sub
If that fails on your document, does adding a font reset resolve that particular issue?
ActiveDocument.Range.Font.Reset
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote
  #6  
Old 02-16-2019, 01:57 PM
FlyingBlind FlyingBlind is offline Why are style updates in VBA not reflected in the document? Windows 10 Why are style updates in VBA not reflected in the document? Office 2016
Novice
Why are style updates in VBA not reflected in the document?
 
Join Date: Feb 2019
Posts: 3
FlyingBlind is on a distinguished road
Default Thank you all

Thank you all for the assistance. I am wondering if the characteristics of the document are making this hard. These are engineering manuals that are several 100 pages (between 500-1000) long and have many bespoke styles in them.

I need to do some more research on styles and how they inter-connect.

Again, thank you for your assistance and time, and (@Kilroy) I realized you may not have been joking - my apologies if so.

I will keep you posted if I make progress.
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
How Do I stop automatic style updates for Word 2013 Lupe71903 Word 2 02-27-2018 01:48 PM
Why are style updates in VBA not reflected in the document? A mystery: how to hop about in a document, inserting updates in a single, new font? markh10178 Word 3 11-26-2015 04:07 PM
Why are style updates in VBA not reflected in the document? Updates tries to install Office 2010 updates which I don't use jlumbtx Office 1 03-13-2015 04:50 PM
Why are style updates in VBA not reflected in the document? Use multiple style sets in the same Word document (depending on which section the style is in) Ricyteach Word VBA 6 03-09-2015 07:11 PM
Automating document updates with new text wordfoolish2 Word 0 01-04-2011 01:01 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 03:50 PM.


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