CopyAttributesOfStyle

J

jerem

I went into the VBA Language Reference for Word but can't seem to find the
language to accomplish the below explanation with the below code, but I admit
I've only been in there for about 2 hours. I tried to get "t" from an
InputBox but keep getting a message that member doesn't exist or something to
that effect so . . .maybe somebody out there can make this process a little
speedier.

Explanation: what I want to do is be able to copy a preexisting style's
["Source"] attributes (as represented by t in the code that follows) to
another preexisting style ["Target"] but I don't want the "Target" style to
be hard coded but variable - that is, gotten from a response from a user via
an input box, if that's possible. As mentioned above, I've been trying to
figure that out looking at Methods (CopyFormatMethod -which gives an example
of copying from paragraph to paragraph) but I need a Copy Paragraph Style
Attribute Method [my terminology] and thus far I haven't come across that
yet. So, in the end if X1 is the Source, this will always be the style
copied from and be hard coded and if X2 is the Target, this will always be
gotten from the user and when all is said and done X2 will still be style X2
only with X1's "With t" attributes. Hope I've made that easy to funderstand.

As always, thanks for the help.

Sub CopyAttributesOfStyle()

Dim t As Style, s As Style
Set s = ActiveDocument.Styles("Source style")
Set t = ActiveDocument.Styles.Add("Target style")

With t
.BaseStyle = ""
.ParagraphFormat = s.ParagraphFormat
.Font = s.Font
.LanguageID = s.LanguageID
End With
End Sub
 
P

Pesach Shelnitz

Hi,

The following macro, which is based on your macro, shows how to get the name
of the target style from the user.

It also handles the problem that the user-specified style may or may not
exist. You used the Styles.Add method to create the object representing the
target style. This method is not suitable for the case in which you want to
use an existing style, so I changed it to the Styles.Item method. The name of
the Item method can be omitted because it is the default method for
collections. If the user enters the name of a style that does not exist, an
error is raised by the Styles.Item method. My macro tests for this error, If
the style specified by the user does not exist, the code pops up a message
and then gracefully exists the macro.

I also used the enum value for the name of built-in Heading 2 style as the
name of the source style to ensure that an error wouldn't be raised when you
test the macro, but you can change it to the name of another style that
exists.

At the end of the macro, I added lines to prepare the objects that the macro
creates for garbage collection. I recommend doing this in all macros.

Sub CopyAttributesOfStyle()
Const Error_StyleDoesNotExist = 5941
Dim t As Style, s As Style
Dim trgStyleName As String

trgStyleName = InputBox("Enter the name of the style to create")

Set s = ActiveDocument.Styles(wdStyleHeading2)
On Error Resume Next
Set t = ActiveDocument.Styles(trgStyleName)
If Err.Number = Error_StyleDoesNotExist Then
MsgBox "The style specified does not exist."
GoTo Done
End If
On Error GoTo 0
With t
.BaseStyle = ""
.ParagraphFormat = s.ParagraphFormat
.Font = s.Font
.LanguageID = s.LanguageID
End With
Done:
Set s = Nothing
Set t = Nothing
End Sub

--
Hope this helps,
Pesach Shelnitz


jerem said:
I went into the VBA Language Reference for Word but can't seem to find the
language to accomplish the below explanation with the below code, but I admit
I've only been in there for about 2 hours. I tried to get "t" from an
InputBox but keep getting a message that member doesn't exist or something to
that effect so . . .maybe somebody out there can make this process a little
speedier.

Explanation: what I want to do is be able to copy a preexisting style's
["Source"] attributes (as represented by t in the code that follows) to
another preexisting style ["Target"] but I don't want the "Target" style to
be hard coded but variable - that is, gotten from a response from a user via
an input box, if that's possible. As mentioned above, I've been trying to
figure that out looking at Methods (CopyFormatMethod -which gives an example
of copying from paragraph to paragraph) but I need a Copy Paragraph Style
Attribute Method [my terminology] and thus far I haven't come across that
yet. So, in the end if X1 is the Source, this will always be the style
copied from and be hard coded and if X2 is the Target, this will always be
gotten from the user and when all is said and done X2 will still be style X2
only with X1's "With t" attributes. Hope I've made that easy to funderstand.

As always, thanks for the help.

Sub CopyAttributesOfStyle()

Dim t As Style, s As Style
Set s = ActiveDocument.Styles("Source style")
Set t = ActiveDocument.Styles.Add("Target style")

With t
.BaseStyle = ""
.ParagraphFormat = s.ParagraphFormat
.Font = s.Font
.LanguageID = s.LanguageID
End With
End Sub
 
J

jerem

Hey Pesach,

The code works great for non-numbering styles so I shall restrict my use of
it for those styles only -- (tried it on numbering styles [third party
numbering suites, Outline numbering - Heading 1. etc.], but it gets a little
unpredictable). Thanks for your help.

Pesach Shelnitz said:
Hi,

The following macro, which is based on your macro, shows how to get the name
of the target style from the user.

It also handles the problem that the user-specified style may or may not
exist. You used the Styles.Add method to create the object representing the
target style. This method is not suitable for the case in which you want to
use an existing style, so I changed it to the Styles.Item method. The name of
the Item method can be omitted because it is the default method for
collections. If the user enters the name of a style that does not exist, an
error is raised by the Styles.Item method. My macro tests for this error, If
the style specified by the user does not exist, the code pops up a message
and then gracefully exists the macro.

I also used the enum value for the name of built-in Heading 2 style as the
name of the source style to ensure that an error wouldn't be raised when you
test the macro, but you can change it to the name of another style that
exists.

At the end of the macro, I added lines to prepare the objects that the macro
creates for garbage collection. I recommend doing this in all macros.

Sub CopyAttributesOfStyle()
Const Error_StyleDoesNotExist = 5941
Dim t As Style, s As Style
Dim trgStyleName As String

trgStyleName = InputBox("Enter the name of the style to create")

Set s = ActiveDocument.Styles(wdStyleHeading2)
On Error Resume Next
Set t = ActiveDocument.Styles(trgStyleName)
If Err.Number = Error_StyleDoesNotExist Then
MsgBox "The style specified does not exist."
GoTo Done
End If
On Error GoTo 0
With t
.BaseStyle = ""
.ParagraphFormat = s.ParagraphFormat
.Font = s.Font
.LanguageID = s.LanguageID
End With
Done:
Set s = Nothing
Set t = Nothing
End Sub

--
Hope this helps,
Pesach Shelnitz


jerem said:
I went into the VBA Language Reference for Word but can't seem to find the
language to accomplish the below explanation with the below code, but I admit
I've only been in there for about 2 hours. I tried to get "t" from an
InputBox but keep getting a message that member doesn't exist or something to
that effect so . . .maybe somebody out there can make this process a little
speedier.

Explanation: what I want to do is be able to copy a preexisting style's
["Source"] attributes (as represented by t in the code that follows) to
another preexisting style ["Target"] but I don't want the "Target" style to
be hard coded but variable - that is, gotten from a response from a user via
an input box, if that's possible. As mentioned above, I've been trying to
figure that out looking at Methods (CopyFormatMethod -which gives an example
of copying from paragraph to paragraph) but I need a Copy Paragraph Style
Attribute Method [my terminology] and thus far I haven't come across that
yet. So, in the end if X1 is the Source, this will always be the style
copied from and be hard coded and if X2 is the Target, this will always be
gotten from the user and when all is said and done X2 will still be style X2
only with X1's "With t" attributes. Hope I've made that easy to funderstand.

As always, thanks for the help.

Sub CopyAttributesOfStyle()

Dim t As Style, s As Style
Set s = ActiveDocument.Styles("Source style")
Set t = ActiveDocument.Styles.Add("Target style")

With t
.BaseStyle = ""
.ParagraphFormat = s.ParagraphFormat
.Font = s.Font
.LanguageID = s.LanguageID
End With
End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top