vba: Display of Styles Pane upon start of Word fails - [Edit: Not possible to change visible property through vba]

User avatar
Charles Kenyon
5StarLounger
Posts: 615
Joined: 10 Jan 2016, 15:56
Location: Madison, Wisconsin

vba: Display of Styles Pane upon start of Word fails - [Edit: Not possible to change visible property through vba]

Post by Charles Kenyon »

The following AutoExec in Normal.dotm fails.

Code: Select all

Sub AutoExec()
    Application.TaskPanes(wdTaskPaneFormatting).Visible = True
End Sub
The following error message is generated:
00 deleteme 5.png
If run after Word is open but with no Active Document, the same error message is generated.

If I run it with a document open, it displays the Styles Pane.

If I open and run the UserForm from my Navigation Pane Add-In, I can get the Styles Pane to display if there is a document open, otherwise the same error.
https://www.dropbox.com/s/an7i2jv601rk2 ... .dotm?dl=0

A curious thing is that if the Styles Pane is visible and I close all documents but leave Word open, the Styles Pane remains on the screen.

Hans, I think I may have raised this a number of years ago with you but if so, I've lost the solution.
I know at one time you gave me DoEvents and a delay macro, these are in the Navigation Pane Add-In.

The following seem to be equivalent:
commandbars("Styles").Visible
Application.TaskPanes(wdTaskPaneFormatting).Visible
Both can be read or set, but not when there is not a document open. Both seem to do the same thing.
You do not have the required permissions to view the files attached to this post.
Last edited by Charles Kenyon on 03 Jun 2023, 17:55, edited 1 time in total.

User avatar
SpeakEasy
4StarLounger
Posts: 550
Joined: 27 Jun 2021, 10:46

Re: Display of Styles Pane upon start of Word fails

Post by SpeakEasy »

Try putting it an AutoOpen macro rather than AutoExec

>The following seem to be equivalent:
>commandbars("Styles").Visible
>Application.TaskPanes(wdTaskPaneFormatting).Visible

In my experience, not quite. In more recent versions of Word "Styles" does not appear to be part of the CommandBarss collection by default (so commandbars("Styles").Visible fails) It gets added to the collection after a single successful use of Application.TaskPanes(wdTaskPaneFormatting).Visible

User avatar
Charles Kenyon
5StarLounger
Posts: 615
Joined: 10 Jan 2016, 15:56
Location: Madison, Wisconsin

Re: Display of Styles Pane upon start of Word fails

Post by Charles Kenyon »

SpeakEasy wrote:
13 Dec 2022, 14:30
Try putting it an AutoOpen macro rather than AutoExec

>The following seem to be equivalent:
>commandbars("Styles").Visible
>Application.TaskPanes(wdTaskPaneFormatting).Visible

In my experience, not quite. In more recent versions of Word "Styles" does not appear to be part of the CommandBarss collection by default (so commandbars("Styles").Visible fails) It gets added to the collection after a single successful use of Application.TaskPanes(wdTaskPaneFormatting).Visible
Thank you. I will try putting it in pseudoAuto macros for new and open in my Add-In.

As to the commandbars, that is an interesting observation. It may explain why the Width property for styles seems to be read-only. I really would like to be able to adjust that.
Last edited by Charles Kenyon on 13 Dec 2022, 15:02, edited 1 time in total.

User avatar
Charles Kenyon
5StarLounger
Posts: 615
Joined: 10 Jan 2016, 15:56
Location: Madison, Wisconsin

Re: Display of Styles Pane upon start of Word fails

Post by Charles Kenyon »

@SpeakEasy

I followed the directions from the MVP site on PseudoAuto macros.
https://wordmvp.com/FAQs/MacrosVBA/PseudoAutoMacros.htm

It seems to be working, including displaying the Styles Pane when Word starts up if checked in my userform.

User avatar
Charles Kenyon
5StarLounger
Posts: 615
Joined: 10 Jan 2016, 15:56
Location: Madison, Wisconsin

Re: Display of Styles Pane upon start of Word fails

Post by Charles Kenyon »

For anyone interested, the Add-In can be downloaded from my downloads page.
https://addbalance.com/word/download.htm#NavPane

User avatar
HansV
Administrator
Posts: 78471
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Display of Styles Pane upon start of Word fails

Post by HansV »

Thanks!
Best wishes,
Hans

User avatar
Charles Kenyon
5StarLounger
Posts: 615
Joined: 10 Jan 2016, 15:56
Location: Madison, Wisconsin

vba CommandBars("Styles").Visible = True throws an error

Post by Charles Kenyon »

No longer working!
It is no longer possible to change the visible property of the Styles Pane using vba. Word throws an error.

I removed the Styles option from my UserForm while retaining the code in hope of a fix.
I've filed a bug report in the feedback forum.
https://feedbackportal.microsoft.com/fe ... 0d3a7a48db

I invite votes and comments.
The vba command:

Code: Select all

application.commandbars("Styles").visible = True
worked fine through Word 2013, at least, and now throws an error. Runtime error -21474672259 (80004005) Unspecified error

It can also show error Method 'Visible' of object 'CommandBar' failed. -21474672259 (80004005)

This is to change display of the Styles Pane in Word.

Code: Select all

 ? application.commandbars("Styles").visible
in the immediate window will report True or False

Code: Select all

? application.commandbars("Styles").position
will give a position

The Visible Property can be changed with other commandbars.

See https://www.msofficeforums.com/word/509 ... 021-a.html

This is a bug and should be fixed
Last edited by Charles Kenyon on 05 Jun 2023, 01:50, edited 1 time in total.

User avatar
Charles Kenyon
5StarLounger
Posts: 615
Joined: 10 Jan 2016, 15:56
Location: Madison, Wisconsin

Re: vba: Display of Styles Pane upon start of Word fails - [Edit: Not possible to change visible property through vba]

Post by Charles Kenyon »

:evilgrin: I have found a workaround using the TaskPanes collection wdTaskPanesFormatting will respond to a change in visible.
oops

User avatar
HansV
Administrator
Posts: 78471
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: vba: Display of Styles Pane upon start of Word fails - [Edit: Not possible to change visible property through vba]

Post by HansV »

Good to know that, thanks.
Best wishes,
Hans

snb
4StarLounger
Posts: 574
Joined: 14 Nov 2012, 16:06

Re: vba: Display of Styles Pane upon start of Word fails - [Edit: Not possible to change visible property through vba]

Post by snb »

What's the issue ?

Code: Select all

Sub M_snb()
    If Documents.Count = 0 Then
      Documents.Add
      Application.TaskPanes(0).Visible = True
      ActiveDocument.Close
    End If
End Sub
I don't see any use for an empty styles pane.

User avatar
Charles Kenyon
5StarLounger
Posts: 615
Joined: 10 Jan 2016, 15:56
Location: Madison, Wisconsin

Re: vba: Display of Styles Pane upon start of Word fails - [Edit: Not possible to change visible property through vba]

Post by Charles Kenyon »

snb wrote:
04 Jun 2023, 09:38
What's the issue ?

Code: Select all

Sub M_snb()
    If Documents.Count = 0 Then
      Documents.Add
      Application.TaskPanes(0).Visible = True
      ActiveDocument.Close
    End If
End Sub
I don't see any use for an empty styles pane.
The initial issue was solved earlier. The idea was to have the styles pane turned on when Word displays a document. The problem was doing this in an AutoExec procedure does not work because there is no document open when an AutoExec normally executes. Instead, it is being done through Document_Open and Document_New procedures. These are shown above.

A side problem is that the Styles Pane is also a Command Bar and it does not respond to the visible or width property settings for command bars. Visible is handled by using TaskPanes instead. There is no vba solution at this time for width because width is not a TaskPanes property.

User avatar
SpeakEasy
4StarLounger
Posts: 550
Joined: 27 Jun 2021, 10:46

Re: vba: Display of Styles Pane upon start of Word fails - [Edit: Not possible to change visible property through vba]

Post by SpeakEasy »

>and it does not respond to the visible or width property settings for command bars

Well ... it does, but only if the .Enabled property is True. And thereby lies a challenge. Since Styles is a builtin commandbar the Enabled property is under the control of the application not your VBA program; simply setting CommandBars("Styles").Enabled = True doesn't work.

So ... is there a way we can get the Application to unlock the Enabled property? Yep ..

Public Sub example()
' Should only need to execute the following line first time, as it unlocks the Enabled property (but in so doing sets Enabled = True and Visible = True)
ActiveDocument.CommandBars.ExecuteMso "StylesPane"
ActiveDocument.CommandBars("Styles").Width = 1024 ' should now work
ActiveDocument.CommandBars("Styles").Visible = True ' or False should now work
End Sub

User avatar
HansV
Administrator
Posts: 78471
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: vba: Display of Styles Pane upon start of Word fails - [Edit: Not possible to change visible property through vba]

Post by HansV »

Clever! :thumbup:
Best wishes,
Hans

User avatar
Charles Kenyon
5StarLounger
Posts: 615
Joined: 10 Jan 2016, 15:56
Location: Madison, Wisconsin

Re: vba: Display of Styles Pane upon start of Word fails - [Edit: Not possible to change visible property through vba]

Post by Charles Kenyon »

SpeakEasy wrote:
05 Jun 2023, 12:53
>and it does not respond to the visible or width property settings for command bars

Well ... it does, but only if the .Enabled property is True. And thereby lies a challenge. Since Styles is a builtin commandbar the Enabled property is under the control of the application not your VBA program; simply setting CommandBars("Styles").Enabled = True doesn't work.

So ... is there a way we can get the Application to unlock the Enabled property? Yep ..

Public Sub example()
' Should only need to execute the following line first time, as it unlocks the Enabled property (but in so doing sets Enabled = True and Visible = True)
ActiveDocument.CommandBars.ExecuteMso "StylesPane"
ActiveDocument.CommandBars("Styles").Width = 1024 ' should now work
ActiveDocument.CommandBars("Styles").Visible = True ' or False should now work
End Sub
I will explore this further! Thank you.
My recent tests reported here (https://www.msofficeforums.com/175461-post12.html) have shown anomalous results, in part due to the Enabled state, it would appear.

Like the .visible command previously, ActiveDocument.CommandBars.ExecuteMso "StylesPane" when in AutoExec does not work because there is no document open. Once a document is open, the TaskPanes command to make it visible will also work.

In both Document_New and Document_Open, it does not work on documents opened/created from Windows Explorer, but does for documents opened/created from within Word.

User avatar
Charles Kenyon
5StarLounger
Posts: 615
Joined: 10 Jan 2016, 15:56
Location: Madison, Wisconsin

Re: vba: Display of Styles Pane upon start of Word fails - [Edit: Not possible to change visible property through vba]

Post by Charles Kenyon »

The Navigation Pane will also throw an error on .visible, for me, in Safe Mode, when the Navigation Pane has not been previously displayed.

User avatar
SpeakEasy
4StarLounger
Posts: 550
Joined: 27 Jun 2021, 10:46

Re: vba: Display of Styles Pane upon start of Word fails - [Edit: Not possible to change visible property through vba]

Post by SpeakEasy »

>when in AutoExec does not work

Well, no, of course not. AutoExec has fairly limited capabilities.

But AutoOpen and AutoNew (in Normal template) work fine for this. I find Document_New and Document_Open a little inconsistent in their behaviour in comparison, and prefer to use the older Auto macros.

User avatar
Charles Kenyon
5StarLounger
Posts: 615
Joined: 10 Jan 2016, 15:56
Location: Madison, Wisconsin

Re: vba: Display of Styles Pane upon start of Word fails - [Edit: Not possible to change visible property through vba]

Post by Charles Kenyon »

Width for the Styles Pane continues to act erratically for me in Office 365.
Works fine in Office 2010. Same code.
Here is my latest version of my Add-In.
https://addbalance.com/word/download.htm#NavPane

User avatar
Charles Kenyon
5StarLounger
Posts: 615
Joined: 10 Jan 2016, 15:56
Location: Madison, Wisconsin

Re: vba: Display of Styles Pane upon start of Word fails - [Edit: Not possible to change visible property through vba]

Post by Charles Kenyon »

Thank you.
For me, the width property is erratic, sometimes working, more often not, when applied to the Styles Pane.
I am trying to fix an Add-In that used to work.
Here is a link to the current version.
https://www.dropbox.com/s/7fjaf1m9f1baj ... .dotm?dl=0

It has a dialog box that displays upon first use and thereafter upon request. This manages document variables in the Add-In that are used to determine whether to display the Navigation and Styles Pane. The AutoExec either displays the dialog box or simply implements the settings upon startup.

It has