PDA

View Full Version : Word VBA and Styles



Morz68
05-08-2018, 07:18 PM
Hi all

I have a style which I want to populate old files as well as new ones. Any suggestions?

I have this code in normal.dotx, but it says 'operation failed' each time I use word. This error only comes up once? :crying:

Thanks in advance.

Sub AutoOpen()

Dim strMyStyle As String
strMyStyle = "MorzNum"

Application.OrganizerCopy Source:=ThisDocument.fullname _
, Destination:=ActiveDocument.fullname, Name:=strMyStyle, _
Object:=wdOrganizerObjectStyles
End Sub

gmayor
05-08-2018, 09:06 PM
Try the following saved in the normal template (which is normal.dotm, not normal.dotx).
The normal template must also have the named style
The message boxes are there for information only while testing. They can be removed along with the 'Else' branch of the conditional statement.
New documents based on the normal template will have the style.


Sub AutoOpen()

Dim strMyStyle As String
Dim oStyle As Style
Dim bFound As Boolean
strMyStyle = "MorzNum"
For Each oStyle In ActiveDocument.Styles
If oStyle.NameLocal = strMyStyle Then
bFound = True
Exit For
End If
Next oStyle
If Not bFound = True Then
MsgBox strMyStyle & " Style added to the document"
Application.OrganizerCopy _
Source:=ThisDocument.FullName, _
Destination:=ActiveDocument.FullName, _
Name:=strMyStyle, _
Object:=wdOrganizerObjectStyles
Else
MsgBox "The style " & strMyStyle & " already exists in the document "
End If
End Sub

macropod
05-08-2018, 09:28 PM
Cross-posted at: https://social.msdn.microsoft.com/Forums/office/en-US/157506bb-654d-49b6-9d92-4fe857f70a4d/trying-to-create-code-for-normaldotx?
Please read VBA Express' policy on Cross-Posting in item 3 of the rules: http://www.vbaexpress.com/forum/faq.php?faq=new_faq_item#faq_new_faq_item3

Morz68
05-15-2018, 01:02 AM
https://i1.social.s-msft.com/globalresources/Images/trans.gif?cver=0%0d%0a


https://i1.social.s-msft.com/globalresources/Images/trans.gif?cver=0%0d%0a0
(https://social.msdn.microsoft.com/Forums/Account/Login?ReturnUrl=https%3a%2f%2fsocial.msdn.microsoft.com%3a443%2fForums%2fof fice%2fen-US%2f157506bb-654d-49b6-9d92-4fe857f70a4d%2ftrying-to-create-code-for-normaldotx%3fforum%3dworddev%26prof%3drequired)
Sign in to vote (https://social.msdn.microsoft.com/Forums/Account/Login?ReturnUrl=https%3a%2f%2fsocial.msdn.microsoft.com%3a443%2fForums%2fof fice%2fen-US%2f157506bb-654d-49b6-9d92-4fe857f70a4d%2ftrying-to-create-code-for-normaldotx%3fforum%3dworddev%26prof%3drequired)


Hi
I have a style and want it to populate all files in the past as well as the new ones. I put it into Normal.dotx and then got this code:
Sub AutoOpen()

Dim strMyStyle As String
strMyStyle = "MyNumbering"

Application.OrganizerCopy Source:=ThisDocument.FullName _
, Destination:=ActiveDocument.FullName, Name:=strMyStyle, _
Object:=wdOrganizerObjectStyles

End Sub

However, I get an error with a "command failed". I am unsure if this is the correct code to use. Any suggestions? Thanks...

gmayor
05-15-2018, 04:32 AM
Did you read the replies before posting again?