Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Copy paste creates Style issue

Status
Not open for further replies.

sheila11

Programmer
Dec 27, 2000
251
US
Hi all,

My VBA code copies large sections of content from one document into another.

What I notice is that if a sentence in the original document is using a style X, and the style X exists in the second document but is using a different font, the sentence changes its font to that used in the second document.

Is there a way to avoid this problem?

TIA,
Sheila
 
Thank you very much genomon.

I am trying to use the info from the mvps.org site.

Unfortunately the first link requires me to be a member of the experts-exchange site :(
Will it be possible for you to paste the solution / reply from that link as a reply here?

Thanks again,
Sheila
 
Hmmmm. I must be a member and forgot. Here is the post:
Code:
Article contributed by Henk van Boeijen

In Word, styles can be (and usually are) based on other styles. If you create a new style, Word bases it on the style of the paragraph that is currently selected, unless you explicitly change this in the “Modify Style” dialog. The style it is based on is called its “base style” (or sometimes, the “parent style”).

1.
	

If it's a paragraph style, the new style will inherit (in an object-oriented fashion) all of its font and paragraph properties from its base style, and will be defined by Word only as “the definition of the base style plus anything you have defined to be different from the base style”.

So in the Format + Style dialog, you will see definitions like: “Normal + Indent: Hanging 0.25", Space After 6 point”.

In this example, only the Indent and the Space After setting are held in the style definition. So if you change the Font in the Normal style, the font in the child style will automatically change, too. But if you change the “Space After” property of the Normal style, the “Space After” property of the child style will not change, because that property is stored in the child style's definition and so is not inherited.

Similarly,  if you see a definition like: “Normal + Font Arial” in the Format + Style dialog, then nothing but the font name is stored in the style definition. So if you change the Font of your Normal style, the font in the child style will not change; but if you change the  Space After property of the Normal style, the Space After property of the child style will change.
  

2.
	

Character styles behave similarly except that only font properties are defined or inherited. So you will see character style definitions in the Format + Style dialog like “Default Paragraph Font + Font color: Dark Blue”. In this example, nothing is stored in the character style definition except the font colour. Everything else is inherited from the underlying paragraph style of whichever paragraph you happen to have applied the character style to.

So a style can be based on another style which in turn is based on another style, and so on. In this way you can form an hierarchy tree of styles. Each member in that tree inherits the formatting options of its parent and adds its own specific options to it. Used sensibly, this mechanism is absolutely essential to your ability to maintain complex documents and templates.

Another example

Style "Normal":
 - Font.Name = "Times New Roman"
 - Font.Size = 11
 - Font.Bold = False

Style "Special":
 - this style is based on "Normal"
 - Font.Size = 9

Style "MoreSpecial"
 - this style is based on "Special"
 - Font.Bold = True

All three styles share the same font name. The font name is defined in “Normal” and is inherited by “Special” and “MoreSpecial”. When the font name in “Normal” is changed to “Arial”, this change also applies to “Special” and “MoreSpecial”.

The font size is only shared by “Special” and “MoreSpecial”: 9 points is defined in “Special”. It overrides the font size setting of “Normal” which is 11 points. “MoreSpecial” inherits 9 points from “Special”.

If the font size of “Normal” is changed to 12 points, this setting will not be inherited by “Special” or “MoreSpecial”.

In some cases you may want to delete formatting properties in a style's definition, in order that it should inherit those properties from its parent (or base) style. 

To do this in VBA, use the following logic:

Dim oStyle As Style
Set oStyle = ActiveDocument.Styles("Special")
oStyle.Font.Name = oStyle.BaseStyle.Font.Name

After running this code, the font name will no longer be stored in the style definition of the “Special” style, but will now be inherited from the Normal style.

If you need to do this more than once, you could make your life easier by calling a  subroutine like the following, which clears the font name of a given style. It accepts a style name (of type String) or a WdBuiltinStyle constant, or a style object, as its argument. So you could call it like this:

ClearStyle "List Number"

or like this:

ClearStyle wdStyleListNumber

or like this:

ClearStyle ActiveDocument.Styles("List number")

If the style is not based on another style, the routine exits without generating an error.

Public Sub ClearStyleFont(oStyle As Variant)

Dim oBaseFont As Font
With ActiveDocument.Styles(oStyle)
    If .BaseStyle = "" Then
        'There is no base style, nothing to clear
    Else
        Set oBaseFont = .BaseStyle.Font
        With .Font
            .Name = oBaseFont.Name
            'Add other font properties here if needed
        End With
        Set oBaseFont = Nothing
    End If
End With

End Sub

You could use exactly the same logic to clear paragraph properties from a style definition, so that they are inherited from the base style.

If you wanted to clear all font and paragraph properties from a style definition, (including any list numbering, language and borders definitions, although unfortunately, not including Frames definitions), in order that you can start defining your own properties for the style knowing that you are starting with a clean slate, you could use the following subroutine.

Public Sub ClearStyle(oStyle As Variant)

With ActiveDocument.Styles(oStyle)
        If .BaseStyle <> "" Then
            .Font = .BaseStyle.Font
            .ParagraphFormat = .BaseStyle.ParagraphFormat
        End If
    End With

End Sub

Hope it helps! [wink]


"Don't be irreplaceable. If you can't be replaced, you can't be promoted."
 
Thanks again, genomon.

The task seemed formidable when attempted thru VBA.

So I am trying to do it in an indirect way. I save a copy of the document to copy from in XML format. Then I open it as xml, and change all style-names to something that doesn't exist in the document to which I am going to copy. I again open and save the same document as Word document, and then do the copy-paste task. This way none of the style will conflict.

I hope this is going to work.


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top