PDA

View Full Version : VBA Code Format in Word



sheeeng
06-30-2005, 12:47 AM
Hi all,

Can someone think of a brilliant ideas to set format for certian section in Word to have display in VBA Code format?

eg. Word can display in its document sub below with exact format?

Private Sub Document_New()
MsgBox "Hello World!"
End Sub

Thanks in advance! :friends:

Killian
06-30-2005, 05:30 AM
There's probably a clever way of doing this involving smart tags or somesuch
You can do it the old-fashioned way... I started but got bored listing the keywords in the array. there's probably a few other thing you might want to add
EnjoySub FormatVBCode()

Dim arrKeywords
'You'll need to add all the keyword here
arrKeywords = Array("Dim", "As", "Private", "Sub", "End", "For", "Next")
Dim i As Long

For i = LBound(arrKeywords) To UBound(arrKeywords)
Selection.Font.Name = "Courier New"
Selection.Find.ClearFormatting
With Selection.Find
.Text = arrKeywords(i)
.Forward = True
.Format = True
.MatchCase = False
.MatchWholeWord = True
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
With .Replacement
.ClearFormatting
.Font.Color = wdColorBlue
.Text = arrKeywords(i)
End With
End With
Selection.Find.Execute Replace:=wdReplaceAll
Next

For i = 1 To Selection.Paragraphs.Count
If Selection.Paragraphs(i).Range.Characters(1).Text = Chr(39) Then
Selection.Paragraphs(i).Range.Font.Color = wdColorGreen
End If
Next

End Sub

Killian
06-30-2005, 05:32 AM
I just realised if you have autocorrect for smartquotes switched on, you will not pick up the comment character (chr 39)

fumei
06-30-2005, 08:46 AM
There arew third patries that offer stuff to do this. I too started building a template that could format VBA code nicely, but it was such a pain in the butt trying to cover everything, that I dropped it.

MOS MASTER
06-30-2005, 09:47 AM
I just realised if you have autocorrect for smartquotes switched on, you will not pick up the comment character (chr 39)
Hi K, :yes

Well you could turn it of on the beginning of the code and put it back on at the end. (like you didn't know that) :rofl:
And the quotes would stick.

Further more I don't thing there are smarter ways of doing this then with the code you presented. But it's a long way from covering all. (Or maybe in Autoformat functions but they would trouble you in Normal word use)

Like Gerry I started on it in the past but got bord with finishing it. :whistle:

Howard Kaikow
07-01-2005, 07:54 AM
One needs to create a parser for the code that then indents/colors as needed.
THere are likely many such parsers already available, some of whuch may even be free.

A crude method is to create the code in the VBA IDE, then COPY the code using Courier New.

Then if colors are desired, you can search for keywords. but you cannot search for comments by just looking for apostrophe, some intelligence has to be added to that search.

sheeeng
07-03-2005, 08:34 AM
One needs to create a parser for the code that then indents/colors as needed.
THere are likely many such parsers already available, some of whuch may even be free.

A crude method is to create the code in the VBA IDE, then COPY the code using Courier New.

Then if colors are desired, you can search for keywords. but you cannot search for comments by just looking for apostrophe, some intelligence has to be added to that search.

What is parsers? Any free parsers you know is free to use?
Thx..:friends:

sheeeng
07-03-2005, 08:36 AM
There's probably a clever way of doing this involving smart tags or somesuch
You can do it the old-fashioned way... I started but got bored listing the keywords in the array. there's probably a few other thing you might want to add
EnjoySub FormatVBCode()

Dim arrKeywords
'You'll need to add all the keyword here
arrKeywords = Array("Dim", "As", "Private", "Sub", "End", "For", "Next")
Dim i As Long

For i = LBound(arrKeywords) To UBound(arrKeywords)
Selection.Font.Name = "Courier New"
Selection.Find.ClearFormatting
With Selection.Find
.Text = arrKeywords(i)
.Forward = True
.Format = True
.MatchCase = False
.MatchWholeWord = True
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
With .Replacement
.ClearFormatting
.Font.Color = wdColorBlue
.Text = arrKeywords(i)
End With
End With
Selection.Find.Execute Replace:=wdReplaceAll
Next

For i = 1 To Selection.Paragraphs.Count
If Selection.Paragraphs(i).Range.Characters(1).Text = Chr(39) Then
Selection.Paragraphs(i).Range.Font.Color = wdColorGreen
End If
Next

End Sub

The color did not change...
Can it automatic do indenting?
Thx for helping. :friends:

sheeeng
07-04-2005, 02:29 AM
Does anyone know how to format VBA Code in Word?

Thx.

Killian
07-04-2005, 04:19 AM
The color did not change...
Can it automatic do indenting?
Which color? You might want to set a couple of breakpoints and debug each find result and paragraph in a small sample so you get the logic working reliably.
Regarding indenting: it will do what ever you program it to do... in a complex formatting system, you would probably need to process the code line by line and having assembled the logic from the indenting rules you want to use, set up a suitable data structure to keep track of the indent level and end of line status (for continuation lines).

It's an interesting project if you've got time on your hands - if you haven't, I've used this (http://www.amosfivesix.com/downloads/) a couple of times - seems to work fine.

sheeeng
07-04-2005, 06:54 AM
Which color? You might want to set a couple of breakpoints and debug each find result and paragraph in a small sample so you get the logic working reliably.
Regarding indenting: it will do what ever you program it to do... in a complex formatting system, you would probably need to process the code line by line and having assembled the logic from the indenting rules you want to use, set up a suitable data structure to keep track of the indent level and end of line status (for continuation lines).

It's an interesting project if you've got time on your hands - if you haven't, I've used this (http://www.amosfivesix.com/downloads/) a couple of times - seems to work fine.

Thx for your advise...
I might undergoing on this project for a long time form now...:friends:

fumei
07-04-2005, 07:48 AM
Oh yeah, you will be at it a long time.

K is totally correct, you WILL have to process the text line by line. There is no other way.

Especially if you want indents. You will need logic that can figure out if THIS line is still logically related to the previous one - so it keeps being indented. For example.

Dim bolIF_IsOn As Boolean

Then you are reading through the code, and you come to an IF, you set the IsOn to True. Using that, you can know to keep the urrent indent. At an End If, you move it back. However, obviously, you can have, and do have, nested If statements. So you need multiple variables to keep track of them.

I am mostly commenting here. Also, the code posted has an array of keywords. However, the keywords are all in the same array. So arrKeywords(3) = Sub, and arrKeywords(5) = For. Are all the keywords going to be the same color? You could build logic to set color according to where the item is in the array.

All I am saying is that this one is not a trivial project. It can be done, but it is probably more work that is worth the effort. UNLESS the process itself is the goal. You would probably learn a lot of VBA working through it.

Some suggestions:

Use a range variable to pick up the text of each line. Check the first word of the range.

Really study the Range and Selection objects.

Use character styles for the keywords.

Try and make sure your original code is cleanly written.

Run through the initial inport and locate all procedural chunks (Functions, Subs) and module level dclarations. Assign bookmarks to them as you go. That way you can recall them AS chunks.

Good luck.

sheeeng
07-04-2005, 07:59 AM
Thanks, Gerry & Killian!
Well, I welcome other responses on helping out this macro...

MOS MASTER
07-04-2005, 08:37 AM
It's an interesting project if you've got time on your hands - if you haven't, I've used this (http://www.amosfivesix.com/downloads/) a couple of times - seems to work fine.
Hi Killian, :hi:

Indeed a interesting program if you got hours to spare...

I've been using that addin you posted myself for a while and it works great. It's a shame however it doesn't do the formatting.

Excellent tutorial with free source on that page as well. :whistle: