PDA

View Full Version : Bulleting New Paragraphs



clvestin
12-04-2005, 09:53 AM
I.m using a userform to collect text comments. I would then like to add these comments to a word document and add bullets to the comments. The problem is: I only want those paragraphs with text to be bulleted. Question: Does the bulleting remain in effect until I Un-bullet; in which case how do I clear the bulletization before forming another paragraph?


Public Sub Memos()

Dim WordApp As Object
Set WordApp = CreateObject("Word.Application")
SaveAsName = ThisWorkbook.Path & "\" & "wren" & Format(Date, "mmddyyyy") & ".doc"
' Send commands to Word
With WordApp
.Documents.Add
With .Selection
.Font.Size = 14
.Font.Bold = True
.ParagraphFormat.Alignment = 1
.typetext Text:="WRENSHALL LNG REPORT"
.typeparagraph
.typeparagraph
.Font.Size = 12
.ParagraphFormat.Alignment = 0
.Font.Bold = False
.typetext Text:="Date:" & vbTab & _
Format(Date, "mmmm d, yyyy")
.typeparagraph
.typetext Text:="To:" & vbTab & Region & " Manager"
.typeparagraph
.typetext Text:="From:" & vbTab & _
Application.UserName
.typeparagraph
.typeparagraph
'.TypeText Message
.typeparagraph
.typeparagraph
.typetext Text:="Liquid Inventory:" & vbTab & vbTab & Format(volvalue, "#.###")
.typeparagraph
.typetext Text:="Liquifaction Rate Y'Day:" & vbTab & Format(liqvalue, "##.###")
.typeparagraph
End With

Load UserForm1
UserForm1.Show

c = .activedocument.paragraphs.Count
For j = 0 To UBound(alphastr)

With .Selection
.typeparagraph
.typetext Text:=alphastr(j)
End With

c = .activedocument.paragraphs.Count
If alphastr(j) <> "" Then _
.activedocument.paragraphs(c).Range.listformat.Applybulletdefault

Next j

.activedocument.SaveAs Filename:=SaveAsName
.ActiveWindow.Close

End With
WordApp.Quit
Set WordApp = Nothing


End Sub

fumei
12-04-2005, 01:09 PM
1. My usual rant....

Could you please use the underscore character to make your code lines shorter. This prevents the code window from having to scroll left/right. This is a pain for those of use who do not use big monitors and high resolution. Your code:
.typetext Text:="Liquid Inventory:" & vbTab & vbTab & Format(volvalue, "#.###")
.typeparagraph
would look like:
.typetext Text:="Liquid Inventory:" & vbTab & _
vbTab & Format(volvalue, "#.###")
.typeparagraph

2. The issue of only having paragraphs with text be bulleted could be very easily solved by the proper use of styles. Word is designed to use styles. You are putting "extra" paragraphs (all those .typeparagraph) for no reason at all - except, I assume, to make space between paragraphs.

If you used styles you would never have to have those "extra" paragraphs. The paragraphs with text would have the space built in.

mdmackillop
12-04-2005, 02:07 PM
Hi Gerry,
I was playing around with this as I can see my own uses for it. I'd like to set and clear tabs by passing a value to another sub, rather than repetious code in the main routine. I tried
'.....
With WordApp
.Documents.Add
With .Selection
DoTab 2.5
'......

Sub DoTab(Pos As Double)
.ParagraphFormat.TabStops.Add Position:=(Pos * 28.35) _
, Alignment:=wdAlignTabLeft, Leader:=wdTabLeaderSpaces
End Sub

but I'm getting "Invalid or unqualified reference" on .ParagraphFormat. Is there something more that needs to be passed to the sub?
Regards
Malcolm

fumei
12-04-2005, 05:01 PM
I am assuming this is not all the code. Also I assume that WordApp is still in scope when your instructions are passed to the other Sub. So WHAT is the object you are using with
.ParagraphFormat

mdmackillop
12-04-2005, 05:12 PM
I was using the code as above. amended as follows.
Public Sub Memos()

Dim WordApp As Object
Set WordApp = CreateObject("Word.Application")
SaveAsName = ThisWorkbook.Path & "\" & "wren" & _
Format(Date, "mmddyyyy") & ".doc"
' Send commands to Word
With WordApp
.Documents.Add
With .Selection
'This works
.ParagraphFormat.TabStops.Add Position:=(2.5 * 28.35) _
, Alignment:=wdAlignTabLeft, Leader:=wdTabLeaderSpaces

'This doen't work
DoTab 2.5

.Font.Size = 14
.Font.Bold = True

'etc.

fumei
12-04-2005, 10:04 PM
Uh,
Sub DoTab(Pos As Double)
.ParagraphFormat.TabStops.Add Position:=(Pos * 28.35) _
, Alignment:=wdAlignTabLeft, Leader:=wdTabLeaderSpaces
End Sub
does not have a referenced object....if that is the entire code. It is an unqualified reference. Once the instruction passes to the other Sub, the original Selection (using the With) is not in scope. There must be more code.
With .Selection
'This works
' because the With Selection is in scope
.ParagraphFormat.TabStops.Add Position:=(2.5 * 28.35) _
, Alignment:=wdAlignTabLeft, Leader:=wdTabLeaderSpaces

'This doen't work
' because the With Selection is not passed in scope to _
' the Sub - if there is no other code in that Sub _
'...which is why I asked.
DoTab 2.5
Am I missing something???

mdmackillop
12-05-2005, 01:25 AM
Hi Gerry,
The "Not in Scope" comment claifies things. It doesn't seem worthwhile calling another sub for this limited change.
Thanks
Malcolm

fumei
12-05-2005, 07:49 AM
Not really. However, you could, in the first sub, create a Selection object, and pass that to the second.

clvestin
12-05-2005, 08:51 AM
Sorry for the long lines..I guess the relevent code for my question would be below. It seems that applying the bulleting does not apply only to the first instance: that is the next empty line is bulleted, and then by reapplying the bulleting the next text filled paragraph is left bulletless. My first response was to assign bullets before the loop so that the apply would take effect. You're probably right about using a template or styles, but Word is not my forte. This routine is called from Excel and forms a fairly simple memo, so the loose code should do.





c = .activedocument.paragraphs.Count
For j = 0 To UBound(alphastr)

With .Selection
.typeparagraph
.typetext Text:=alphastr(j)
End With

c = .activedocument.paragraphs.Count
If alphastr(j) <> "" Then _
.activedocument.paragraphs(c).Range.listformat.Applybulletdefault

Next j

clvestin
12-09-2005, 07:29 PM
ok-I'm following the styles idea. I've played around in Word directly and think i'm getting a handle. From Excel however, I'm having some issues(probably with focus). Thusly


Public Sub MakeMemos()
Dim WordApp As Object

Set WordApp = CreateObject("Word.Application")

SaveAsName = ThisWorkbook.Path & "\" & "wren" & Format(Date, "mmddyyyy") & ".doc"
With WordApp
.documents.Add
With .Selection
.typeparagraph
.typeparagraph 'along with some other stuff;see 1st post for detail


c = WordApp.activedocument.paragraphs.Count
.paragraphs(c).Style = wdstylelistbullet4
.typetext Text:="Liquid Inventory:" & Format(volvalue, "#.###")
.typeparagraph

End With

set myrange = .activedocument.Range(Start:=.activedocument.paragraphs(9).Range.Start, End:=.activedocument.paragraphs(10).Range.End)
myrange.Style = wdstylelistbullet2

WordApp.Quit
Set WordApp = Nothing
End Sub




Neither one of the style settings seems to work for me from Excel. Either an error of a "value out of range" or "object required". Any ideas?

clvestin
12-09-2005, 08:40 PM
This doesn't work:


Set myrange = .activedocument.Range(Start:=.activedocument.paragraphs(9).Range.Start, End:=.activedocument.paragraphs(10).Range.End)
myrange.Style = wdstylelistbullet2



this does:

Set myrange = .activedocument.Range(Start:=.activedocument.paragraphs(9).Range.Start, End:=.activedocument.paragraphs(10).Range.End)
myrange.Style = -55



So.....How do I specify the enum and use the wdstylelistbullet2(which,strangely enough is easier to remember)?

fumei
12-09-2005, 10:52 PM
Aaaahhhh, PLEASE use the underscore character.
Set myrange = .activedocument.Range _
(Start:=.activedocument.paragraphs(9).Range.Start, _
End:=.activedocument.paragraphs(10).Range.End)
myrange.Style = wdstylelistbullet2
See, isn't that better? It does not streeetttttttttttttttttttttttttttttttttttttttttttttttcccccccccccccccccccccc cccccccccchhhhhhhhhhhhhhhhhh out so far. And, in a real VBE you can control the tabs, and it makes the reading MUCH easier. The Start and End can be on separate lines for easier scanning. Using the underscore is not just for my benefit - although I admitedly whine a lot about it - but it DOES make for easier to read code.

As for your problem, try:
myrange.Style = .activedocument.styles wdstylelistbullet2
You are declaring Wordapp as an Object, not an Application, so you are using late binding: the wdType must be specified.