PDA

View Full Version : Newbie needs some macro help



duffdiggler
03-15-2006, 08:49 AM
I'm very knew to using vba. I've been asked to alter a template that incorporates macros. Basically there is a macro toolbar that a user can click on and it inserts certain text and whatever.

I've been able to do some of the stuff already. Having a problem with this one though.

There is a macro set up to display the phrase WARNING: (this is a test warning). I was successful in making the word "WARNING" bold and italic, but I want the "(this is a test warning)" to be italic.

I want it to look like this. WARNING: (this is a test warning)

Here is the code that is already on it


Sub Warning()

Selection.Style = "Warning"
Selection.TypeText Text:="WARNING: (this is a test warning)"
End Sub

mdmackillop
03-15-2006, 11:22 AM
Hi Duff
Welcome to VBAX.
It's a bit fiddly but this should do it.

Sub InsText()
Dim Doc As Document
Dim rngDoc As Range
Dim Txt As String

Set Doc = ActiveDocument
Set rngDoc = Doc.Range(Start:=Selection.Start, _
End:=Selection.Start)
Selection.TypeText "WARNING:"
Set rngDoc = Doc.Range(Start:=rngDoc.Start, _
End:=Selection.Start)
'Format range
With rngDoc.Font
.Bold = True
.Italic = True
End With
Set rngDoc = Doc.Range(Start:=Selection.Start, _
End:=Selection.Start)
Selection.TypeText "(This is a test warning)"
Set rngDoc = Doc.Range(Start:=rngDoc.Start, _
End:=Selection.Start)
'Format range
With rngDoc.Font
.Bold = False
.Italic = True
End With
End Sub



It seemed a worthwhile utility, so here's a Userform version attached.
Regards
MD

lucas
03-15-2006, 11:36 AM
Malcolm said fiddly......then I downloaded it and I see what you mean...has potential though.

duffdiggler
03-15-2006, 11:50 AM
Thanks a lot. Works great.

fumei
03-15-2006, 07:14 PM
Since you apparently have a Style named Warning - and I hope it is a Character style, not a paragraph style - then simply have another style to cover the other text.

Assumption:
Warning1 style = 14 pt Bold
Warning2 style = 12 pt italic

Of course they could be any font attribute.Sub WarningText()
With Selection
.Style = "Warning1"
.TypeText Text:="Warning: "
.Style = "Warning2"
.TypeText Text:="(this is a test warning)"
End With
End Sub

lucas
03-15-2006, 10:00 PM
Selection.TypeParagraph will put me on the next line but how do I get rid of the italics and back to normal font? I tried .style ="Normal"

Sub WarningText()
With Selection
.Style = "Warning1"
.TypeText Text:="Warning: "
.Style = "Warning2"
.TypeText Text:="(this is a test warning)"
Selection.TypeParagraph
End With
End Sub

fumei
03-15-2006, 11:28 PM
You should never being using "Normal" anyway.

However...this is fascinating. I have never used ONLY Character styles in a paragraph before. My paragraphs are made of styles, and then I may have a character style within it.

You are completely correct. It will NOT go back to Normal - or any other style. This is very very strange.

Not only that, but unless you put a space before the warning text, AND a space after the warning text, there is no way to MAKE the next paragraph anything but the character style. How very very odd. You can select the next paragraph, and explicitly make it a different style...it will not show any attributes other than those of the preceding character style.

I am looking at this. This is a new one for me.

Tony...are you out there? Is an existing paragraph style - in the paragraph mark - superceded by a character style, if such a style is immediately before it. Seems odd to me, but this is what seems to be happening.

Comments?

mdmackillop
03-16-2006, 12:19 AM
Hi Gerry,
In my userform routine I had to return the final selection to the original formatting to permit typing of "normal" text.
Regards
Malcolm

TonyJollans
03-16-2006, 10:08 AM
VBA just reflects the UI.

When you are typing, pressing Enter maintains the formatting you are using at that point - Paragraph Style, Character Style and/or Direct Formatting. I dont think it's possible to selectively switch off a character style and leave direct formatting alone but there are a few other things you can do ..

Ctrl+Space removes all Character Formatting (Style and Direct)
Ctrl+Q removes removes Direct Paragraph Formatting
Clear Formatting (from the Styles Task Pane) does both of the above at once (not sure about this one pre-2002)

I'm not entirely sure of all the rules for the scope of the affected text.

In VBA the equivalents, respectively, are:

Selection.Font.Reset
Selection.ParagraphFormat.Reset
Selection.ClearFormatting
So in Steve's example ...

Sub WarningText()
With Selection
.Style = "Warning1"
.TypeText Text:="Warning: "
.Style = "Warning2"
.TypeText Text:="(this is a test warning)"
Selection.TypeParagraph
.Font.Reset
.TypeText Text:="This is back to normal"
End With
End Sub

lucas
03-16-2006, 12:45 PM
Thanks Tony,
That works like a charm