Results 1 to 7 of 7
  1. #1
    3 Star Lounger
    Join Date
    May 2002
    Location
    Smithfield, Free State, South Africa
    Posts
    210
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Style update (Word2000/3)

    I have created a style using a macro.
    Now, I need to update this style.
    If I re-run the macro, it gives me an error because the style already exists.
    How to I over-write the style, update it, or whatever by means of the macro.
    For example, the first time the macro must create the style, the next time the macro is run, it must update the style.
    Help!
    Regards

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Style update (Word2000/3)

    You can loop through all styles to see if a style with the specified name exists, or you can try to refer to the style, and trap the error that occurs if it doesn't exist. For example:

    Dim styl As Style
    Dim strStyle As String

    strStyle = "MyStyle"

    On Error Resume Next
    Set styl = ActiveDocument.Styles(strStyle)
    If Not (Err = 0) Then
    Set styl = ActiveDocument.Styles.Add(strStyle, wdStyleTypeParagraph)
    End If
    On Error GoTo ErrHandler ' or GoTo 0

    styl.Font.Name = "Arial"
    ...

  3. #3
    3 Star Lounger
    Join Date
    May 2002
    Location
    Smithfield, Free State, South Africa
    Posts
    210
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Style update (Word2000/3)

    Hans, how about the following thinking...
    I know which style I want to "update", so
    Can I delete the style first and then add the updated style?
    The style name for example is "ChangeBorder"
    Of course if the style does not exist we need to add it without there being an error message.
    My knowledge (although increasing) does not allow me to do this successfully, yet!
    Regards

  4. #4
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Style update (Word2000/3)

    You can use On Error Resume Next to prevent error messages while deleting the style:

    On Error Resume Next
    ActiveDocument.Styles("ChangeBorder").Delete
    On Error GoTo ErrHandler ' or GoTo 0
    With ActiveDocument.Styles.Add("ChangeBorder", wdStyleTypeParagraph)
    ' set style properties here
    End With

  5. #5
    3 Star Lounger
    Join Date
    May 2002
    Location
    Smithfield, Free State, South Africa
    Posts
    210
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Style update (Word2000/3)

    Thanks Hans
    Am I right in saying that if I use the Error Handler, I need to create one, like for instance a Msg box or whatever?
    Regards

  6. #6
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Style update (Word2000/3)

    Yes, if you want an error handler, you must create it yourself, for example:

    Sub Test()
    On Error Resume Next
    ...
    On Error GoTo ErrHandler
    ...
    Exit Sub

    ErrHandler:
    MsgBox Err.Description, vbExclamation
    End Sub

    If you want the default (built-in) error handling, use On Error GoTo 0:

    Sub Test()
    On Error Resume Next
    ...
    On Error GoTo 0
    ...
    End Sub

  7. #7
    3 Star Lounger
    Join Date
    May 2002
    Location
    Smithfield, Free State, South Africa
    Posts
    210
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Style update (Word2000/3)

    Gotcha.
    Thanks again
    Regards

Posting Permissions

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