Consulting

Results 1 to 5 of 5

Thread: Word VBA and Styles

  1. #1
    VBAX Newbie
    Joined
    May 2018
    Posts
    2
    Location

    Word VBA and Styles

    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?

    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

  2. #2
    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
    Graham Mayor - MS MVP (Word)
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  3. #3
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    3,585
    Location
    Cross-posted at: https://social.msdn.microsoft.com/Fo...or-normaldotx?
    Please read VBA Express' policy on Cross-Posting in item 3 of the rules: http://www.vbaexpress.com/forum/faq...._new_faq_item3
    Cheers
    Paul Edstein
    [MS MVP - Word]

  4. #4
    VBAX Newbie
    Joined
    May 2018
    Posts
    2
    Location

    VBA Code for putting a style into all documents, old and new





    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...



  5. #5
    Did you read the replies before posting again?
    Graham Mayor - MS MVP (Word)
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •