MS Office Forum / Word / Programming / September 2006
Discrepancy between VBA and Word interface
|
|
Thread rating:  |
larrysulky@gmail.com - 18 Sep 2006 21:55 GMT Me again, but different problem.
I have a macro that selects all of the paragraph where the cursor is, except for the paragraph marker, and copies it into the Word clipboard. (I know that I could monkey with the "Use smart paragraph selection" setting, but I get more control over what's happening this way.)
I have another macro that selects the paragraph where the cursor is, except for the paragraph marker, and pastes the Word clipboard in place of it (this retains the style of the target paragraph). The macro then re-applies the current style to remove any locally applied font -- in other words, it ensures that the font of the target paragraph is what the style says it should be.
These two macros together let me copy one paragraph over another, keeping the contents of the source paragraph but the style of the target paragraph.
Weirdness: If the source paragraph had some italic text in it, that italicism is lost upon pasting. But if I undo one step, before the style is re-applied, the italicism is restored. If I then apply the current style via the Word style interface, the italicism is NOT lost. Yet the VBA code was created basically by recording a macro of applying the style via the interface!
Summary: Applying a style to a paragraph via a macro will wipe out italics (and bold, I reckon) in the paragraph, but doing the same thing via the Word interface (that was recorded to create the macro in the first place) will NOT.
Any insights? I need to retain italic, bold, etc., but lose any "on-a-whim" fonts.
Thanks -- --larry
''''''''''''''''''''''''''''''''''''''''''''''''''
Sub ParaCopy() On Error GoTo Error_Handler Selection.MoveRight Unit:=wdCharacter, Count:=1 Selection.MoveUp Unit:=wdParagraph, Count:=1 Selection.MoveDown Unit:=wdParagraph, Count:=1, Extend:=wdExtend Selection.MoveLeft Unit:=wdCharacter, Count:=1, Extend:=wdExtend Selection.Copy Error_Handler: If Err.Number <> 0 Then MsgBox Err.Description, vbExclamation, "ParaCopy" End If End Sub
Sub ParaPaste() On Error GoTo Error_Handler Dim myStyle As String Selection.MoveRight Unit:=wdCharacter, Count:=1 Selection.MoveUp Unit:=wdParagraph, Count:=1 Selection.MoveDown Unit:=wdParagraph, Count:=1, Extend:=wdExtend Selection.MoveLeft Unit:=wdCharacter, Count:=1, Extend:=wdExtend Selection.Paste myStyle = Selection.Style ' Selection.MoveLeft Unit:=wdCharacter, Count:=1 ' Doesn't help. ' Selection.MoveRight Unit:=wdCharacter, Count:=1 ' Doesn't help! ' Selection.EndKey Unit:=wdLine ' Jeesh! Doesn't help!! Selection.Style = ActiveDocument.Styles(myStyle)
Error_Handler: If Err.Number <> 0 Then MsgBox Err.Description, vbExclamation, "ParaPaste" End If End Sub
larrysulky@gmail.com - 18 Sep 2006 22:05 GMT Sorry, I didn't make mention -- this solution has to work in Word 2000 and later. Hence some of the funny business in the macros about selecting just the right bit. --larry
Shauna Kelly - 19 Sep 2006 11:15 GMT Hi Larry
> I need to retain italic, bold, etc., but lose any "on-a-whim" fonts. The fundamental problem is that Word can't read your mind. It can't tell whether italic or bold counts as "on-a-whim" formatting or not.
The general rule in the UI and also, as far as I know, in VBA is that if you apply a paragraph style *to a paragraph* then direct formatting (like italic) will be retained where it covers less than half the text in the paragraph, and rejected when it covers more than half.
This is quite sensible. If, for example, you had a paragraph in style Heading 1 and the text was "The sinking of the _Titanic_" and you applied Heading 2 to that paragraph, then you'd expect the _Titanic_ to retain its italics. If, however, the whole paragraph had been formatted green over the top of Heading 1, and you applied Heading 2, then you'd expect Heading 2 to be applied and the green to disappear.
The issue arises, however, as to whether you're applying the paragraph style *to a paragraph*. In the UI, if you either (a) just click in the paragraph and select nothing or (b) select the whole paragraph including the paragraph marker or (c) select text that spans more than one paragraph, and then apply a paragraph style, then it is applied to the whole paragraph(s). If you have actually selected some text, then the paragraph style will be applied (well, its font characteristics will be applied) only to the selected characters.
VBA is more forgiving. If you apply a paragraph style to a range using the .Style property of a .Range object, then the style is applied to the paragraph(s). That is, it works like Word pre-2002.
There are often discrepancies between what you do in the UI and record as a macro, and what that macro will actually then do. For more on that, see How to modify a recorded macro http://word.mvps.org/FAQS/MacrosVBA/ModifyRecordedMacro.htm
Hope this helps.
Shauna Kelly. Microsoft MVP. http://www.shaunakelly.com/word
> Me again, but different problem. > [quoted text clipped - 66 lines] > End If > End Sub larrysulky@gmail.com - 19 Sep 2006 17:14 GMT Thanks for responding again, Shauna; you totally rock! (I am informed by my teenagers that this is accurate, albeit quaint, usage of the phrase.)
> Hi Larry > > > I need to retain italic, bold, etc., but lose any "on-a-whim" fonts. > > The fundamental problem is that Word can't read your mind. It can't tell > whether italic or bold counts as "on-a-whim" formatting or not. But I have ReadMyMind.dll installed, I'm sure of it!
> The general rule in the UI and also, as far as I know, in VBA is that if you > apply a paragraph style *to a paragraph* then direct formatting (like > italic) will be retained where it covers less than half the text in the > paragraph, and rejected when it covers more than half. I had no idea about this algorithm. Very interesting and, as you say, sensible.
> The issue arises, however, as to whether you're applying the paragraph style > *to a paragraph*. I've discovered that the UI behaves differently in this regard if I (a) select the whole paragraph versus (b) leave the cursor within the paragraph but with nothing selected. In the latter case, I get my desired behaviour; in the former, I don't. But VBA, regardless of whether I use the selection or a range, behaves as if I had explicitly selected the whole paragraph (that is, case (a)).
> There are often discrepancies between what you do in the UI and record as a > macro, and what that macro will actually then do. For more on that, see > How to modify a recorded macro > http://word.mvps.org/FAQS/MacrosVBA/ModifyRecordedMacro.htm Yep. What was odd to me was that recording the action of assigning a style to a paragraph yields a one-command macro:
Selection.Style = ActiveDocument.Styles("EPG_SRC")
where I then replace the literal style with a variable. I don't see any other way aside from setting selection style or range style, to do this.
---larry
Helmut Weber - 19 Sep 2006 12:03 GMT Hi,
how about this one:
Sub Test800() Dim sTmp As String Dim prg1 As Range Dim prg2 As Range Set prg2 = selection.Paragraphs.Last.Range Set prg1 = selection.Paragraphs.Last.Previous.Range prg2.End = prg2.End - 1 prg2.Copy sTmp = prg1.Font.Name prg1.End = prg1.End - 1 prg1.Paste prg1.Font.Name = sTmp End Sub
Set a range to the actual paragaph. Set a range to the preceding paragraph. Reduce both ranges by one character. Get the font of the preceding paragraph. Copy actual paragraph's text over the preceding paragraph's text. Reset the font of the preceding paragraph.
Which may work and may not work, depending on all kinds of formatting issues, e.g. if there are character styles, or if the preceding paragraphs holds different fonts.
An lots of error handling would have to be done in addition, e.g. if there was no preceding paragraph.
HTH, nevertheless.
 Signature Greetings from Bavaria, Germany
Helmut Weber, MVP WordVBA
Win XP, Office 2003 "red.sys" & Chr$(64) & "t-online.de"
|
|
|