Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 05-19-2022, 08:51 AM
grNadpa grNadpa is offline Word VBA overlay and restore word2016 heading / footer style Windows 10 Word VBA overlay and restore word2016 heading / footer style Office 2016
Advanced Beginner
Word VBA overlay and restore word2016 heading / footer style
 
Join Date: Mar 2022
Posts: 46
grNadpa is on a distinguished road
Default Word VBA overlay and restore word2016 heading / footer style

My document shows a "landing" form that provides command buttons for the various functions (Waivers, Invoices, Roster and Notary). I coded the document heading in Times New Roman 14 Bold.



Each of the subforms replaces the Heading, and in the case of the Invoice, replaces both the header and footer with a logo image.

In anticipation of restoring the Heading when the subforms unload, the code below shows my attempt to capture the default heading before invoking the subforms.

I am able to restore the text, but the style is lost, defaulting to Calabri 11 Normal. I've resorted to testing with a module (so as not to corrupt my main application). Here's the code
Code:
Sub Main()
    Dim strCurrentHeading As String
    Dim objHeaderStyles As Object
 'clicking heading text shows Times New Roman 14 Bold   
    strCurrentHeading = Word.ActiveDocument.Sections(1).Headers(1).Range.Text
    Set objHeaderStyles = Word.ActiveDocument.Styles(wdStyleHeading1)
'debug print Styles show Calibri 11 Normal
'Load Header
    

    Word.ActiveDocument.Sections(1).Headers(1).Range.Text = ""

'Restore Heading
'loosely based on: https://stackoverflow.com/questions/56697719/_
	how-to-change-style-of-some-texts-to-style-of-heading-1-without-affect-rest-of-t
'debug print Styles show Calibri 11 Normal
    With ActiveDocument.Styles(wdStyleHeading1)
    .ParagraphFormat.Alignment = wdAlignParagraphCenter
    .Font.Name = "Times New Roman"
    .Font.Size = 14
    .Font.Bold = True
  End With
'debug print Styles show Calibri 11 Normal
      Word.ActiveDocument.Sections(1).Headers(1).Range.Text = strCurrentHeading
'debug print Styles show Calibri 11 Normal
Exit Sub
...
I've checked the immediate window at the places indicated and the font remains Calabri 11 Normal. I also tried the "... = strCurrentHeading" both before and after the "With ActiveDocument.Styles ..." statment.
Reply With Quote
  #2  
Old 05-19-2022, 07:50 PM
Guessed's Avatar
Guessed Guessed is offline Word VBA overlay and restore word2016 heading / footer style Windows 10 Word VBA overlay and restore word2016 heading / footer style 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

Your code is not specifying the style in the Header. If you want to control the look of the header then you should apply a style to that range (and make sure the style looks the way you want it to).
Code:
Word.ActiveDocument.Sections(1).Headers(1).Range.Style = "Header"
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote
  #3  
Old 05-20-2022, 05:45 AM
grNadpa grNadpa is offline Word VBA overlay and restore word2016 heading / footer style Windows 10 Word VBA overlay and restore word2016 heading / footer style Office 2016
Advanced Beginner
Word VBA overlay and restore word2016 heading / footer style
 
Join Date: Mar 2022
Posts: 46
grNadpa is on a distinguished road
Default

Thank you Guessed.
Whine alert: I do not understand the concept. Because of that, I do not know how to "apply a style to that range". Obviously the following does not do that.
Code:
Word.ActiveDocument.Sections(1).Headers(1).Range.Style = "Header"
    With ActiveDocument.Styles(wdStyleHeading1)
    .ParagraphFormat.Alignment = wdAlignParagraphCenter
    .Font.Name = "Times New Roman"
    .Font.Size = 14
    .Font.Bold = True
  End With
I do understand that Styles is a collection and Style is an object. And that the Style has properties. But the concept of "range" has me completely baffled.
Might you share the specific code that would achieve my Bold, 14 Point, Times New Roman heading?
Reply With Quote
  #4  
Old 05-20-2022, 11:30 AM
Italophile Italophile is offline Word VBA overlay and restore word2016 heading / footer style Windows 11 Word VBA overlay and restore word2016 heading / footer style Office 2021
Expert
 
Join Date: Mar 2022
Posts: 315
Italophile is just really niceItalophile is just really niceItalophile is just really niceItalophile is just really nice
Default

“Header” and “Heading” are entirely different. The default style for a page header is wdStyleHeader, not wdStyleHeading1. Headings are used in the body of the document to organise content by subject.
Reply With Quote
  #5  
Old 05-20-2022, 12:24 PM
grNadpa grNadpa is offline Word VBA overlay and restore word2016 heading / footer style Windows 10 Word VBA overlay and restore word2016 heading / footer style Office 2016
Advanced Beginner
Word VBA overlay and restore word2016 heading / footer style
 
Join Date: Mar 2022
Posts: 46
grNadpa is on a distinguished road
Default

Thank you @Italophile. That did it.

All that's left is to conditionally center a .png image in the footer without losing the "Page X of Y" on the right.
Reply With Quote
Reply

Tags
header and footer, vba code, word2016

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Word VBA overlay and restore word2016 heading / footer style Word: adding tabs to a heading style sakols Word 11 05-31-2019 10:02 AM
Word 2010 Having more than one font for same style heading normancamp Word 1 08-06-2018 02:21 PM
How to restore my Style Header 3? jongleur Word 3 05-16-2017 10:48 AM
Word VBA overlay and restore word2016 heading / footer style How to Restore Heading 1, Heading 2, etc. within a Word Document cheech1981 Word 9 01-11-2017 02:14 AM
Word VBA overlay and restore word2016 heading / footer style Very frequent heading style, but still not built in Word!!!!! why??? Jamal NUMAN Word 17 06-30-2011 05:47 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 10:00 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