PDA

View Full Version : [SOLVED:] Local name for built-in styles



Roderick
01-25-2019, 11:53 AM
Three users in one of my client offices have different versions of Word 2016: one in English, one in French and the other is a Spanish one. Microsoft changes the names of the built-in styles depending on the language used. For this project I need to identify the local name of Heading 1 depending on the template being used in each language.

I've seen them in the list of styles for each template: English is, obviously, "Heading 1"; French is "Titre 1" and Spanish it is "Título 1". What I need to do now, depending on the language version of Word users are working with, is to identify what is the local name for that heading using the ".NameLocal" VBA designation.

I have tried various combinations of using .NameLocal but all I keep getting are errors in its construction syntax. After that I need to strip out the '1' of Heading 1 and just use its designation. Would a '.Left' statement do the trick here?

Thanks for any help.

Roderick

Paul_Hossler
01-25-2019, 12:47 PM
I think if you use the Word Style enumeration values, it should work for different languages

https://docs.microsoft.com/en-us/office/vba/api/word.wdbuiltinstyle

macropod
01-25-2019, 03:43 PM
I think if you use the Word Style enumeration values, it should work for different languages
You can also use the corresponding Word Style constants. For example, "Heading 1"; "Titre 1", and Título 1" are all wdStyleHeading1.

Roderick
01-27-2019, 06:07 AM
Thanks to both of you for the above replies.

I used the following code and it gave me the name of wdStyleHeading1 for the active document:

Dim myStyle As String
myStyle = ActiveDocument.Styles(wdStyleHeading1)

Just what was needed.

Now I have two questions relating to this:
1. Should the code be referring to the ActiveDocument or should it be the template to which the active document refers?

2. Having found that in my English version of Word, it says that it is 'Heading 1', how can I now extract the first word from the string to only use 'Heading'? I have searched the net for string functions in Word VBA but cannot find a suitable answer to resolve the problem.

I appreciate all the help and guidance.

Roderick

macropod
01-27-2019, 01:24 PM
I used the following code and it gave me the name of wdStyleHeading1 for the active document:

Dim myStyle As String
myStyle = ActiveDocument.Styles(wdStyleHeading1)

You could reduce that to:

Dim myStyle As String
myStyle = wdStyleHeading1

or, if you're only using that one Style anywhere:

Const myStyle As String = wdStyleHeading1

1. Should the code be referring to the ActiveDocument or should it be the template to which the active document refers?
The above code revision will work with either. You need only use code like ActiveDocument.Styles(wdStyleHeading1) or ActiveDocument.AttachedTemplate.Styles(wdStyleHeading1) if there is a need to differentiate between them for some reason or need to know their 'local' names.

2. Having found that in my English version of Word, it says that it is 'Heading 1', how can I now extract the first word from the string to only use 'Heading'? I have searched the net for string functions in Word VBA but cannot find a suitable answer to resolve the problem.
It's not apparent why you'd need to know what a part of a Style is called in a given language. But, as that seems to be a concern, you could use code like:

MsgBox Split(ActiveDocument.Styles(wdStyleHeading1).NameLocal, " ")(0)
or:


MsgBox Split(ActiveDocument.Styles(myStyle).NameLocal, " ")(0)

Roderick
02-03-2019, 04:13 AM
Very grateful to you, Paul for answering those questions. Those were items I couldn't fathom out.

The reason I asked about a given part of a style name was something a client had posed to me and I thought I better see if there was an answer. I didn't know about the SPLIT command.

Thanks again.

Roderick