Home |
Search |
Today's Posts |
#1
Posted to microsoft.public.excel.programming
|
|||
|
|||
Re. Module Comments
As an additional contribution to a recent question about commentary inside macros: A good documentation on the code is a must in advanced programming. Sometimes it is necessary to write a larger description. This may appear either in a separate Word document, or may be, of course, placed as a usual comment between Sub or Function, and End. In the last case it is a nuisance to divide a written paragraph into lines of adequate length. Especially it pains if one has to rewrite such a block. The following Subs have proven useful to facilitate this work. Both are Word macros, and thus they must be imported into Normal or other module of that application. You can easily create a compact text of a comment in Word, and after the appending the remark markers (apostrophe, or % for Matlab) by means of MacroComment you can copy the rebuilt text into the code page. In revert, you can copy a selected remark block from a code to a Word document, and by means of macro ConcatenateParagraph remove remark markers and inner ends of paragraph, and thus prepare the text for changing and replacing. You can work to and fro by switching both subroutines. Another benefit of ConcatenateParagraph is removing, in the selected paragraph, all the s that are appearing through a normal run of e-mail. In such a way you can convert that mutilated messages into a human language. Though this is aimed partly at Word groups, I offer this first here, hoping to receive some comments from those interested. Sub ConcatenateParagraph() ' Petr Bezucha, 2009 Dim Begin As Long 'selection of all the block to be arranged With Selection 'Application.ScreenUpdating = False Begin = .Start 'cursor after the last character of the selection .Collapse (wdCollapseEnd) Do 'repeating: selection of the whole line, .MoveEnd Unit:=wdLine, Count:=-1 'selection of the first character in the line .MoveRight Unit:=wdCharacter, Count:=1, Extend:=wdExtend 'deleting all 's and ' as first characters Do If .Text = "" Then .Delete Else Exit Do Loop If .Text = "'" Or .Text = "%" Then .Delete .Collapse (wdCollapseStart) 'finish if strikes the beginning If .Start <= Begin Then Exit Sub 'deleting end of paragraph .MoveLeft Unit:=wdCharacter, Count:=1, Extend:=wdExtend If Asc(.Text) = 13 Then .Delete Loop End With 'Application.ScreenUpdating = True End Sub Sub MacroComment() ' Petr Bezucha, 2009 Dim MB As String, RM As Single, Finish As Long, Begin As Long Static ComMark As String If ComMark = "" Then MB = InputBox("' % Rem", "Marker of commentary line", "'") If MB = "'" Or MB = "%" Then ComMark = MB Else Exit Sub End If With Selection .Font.Name = "Courier New" .ParagraphFormat.Alignment = wdAlignParagraphLeft 'diminishing of the line length by approx. one character space RM = .PageSetup.RightMargin .PageSetup.RightMargin = RM + .Font.Size MB = MsgBox("Change font size or margin if necessary", _ vbOKCancel + vbQuestion, "Comment layout") 'exit with original line length If MB = vbCancel Then .PageSetup.RightMargin = RM: Exit Sub 'number of the first character Begin = .Start 'move to the very end of the paragraph .EndKey Extend:=wdMove 'select the last character and if it is space, delete it .MoveLeft Unit:=wdCharacter, Count:=1, Extend:=wdExtend If .Text = " " Then .Delete 'move to the beginning of the line .HomeKey Unit:=wdLine, Extend:=wdMove 'add marker .TypeText Text:=ComMark Do .MoveUp Unit:=wdLine, Extend:=wdMove .EndOf Unit:=wdLine, Extend:=wdMove .TypeParagraph .MoveUp Unit:=wdLine, Count:=1, Extend:=wdMove .TypeText Text:=ComMark Loop While .Start Begin + 1 'return to the original line length .PageSetup.RightMargin = RM End With End Sub Thanks Petr Bezucha |
#2
Posted to microsoft.public.excel.programming
|
|||
|
|||
Re. Module Comments
Thanks! Very useful.
Paul On Apr 23, 2:31*pm, PBezucha wrote: As an additional contribution to a recent question about commentary inside macros: A good documentation on the code is a must in advanced programming. Sometimes it is necessary to write a larger description. This may appear either in a separate Word document, or may be, of course, placed as a usual comment between Sub or Function, and End. In the last case it is a nuisance to divide a written paragraph into lines of adequate length. Especially it pains if one has to rewrite such a block. The following Sub’s have proven useful to facilitate this work. Both are Word macros, and thus they must be imported into Normal or other module of that application. You can easily create a compact text of a comment in Word, and after the appending the remark markers (apostrophe, or % for Matlab) by means of MacroComment you can copy the rebuilt text into the code page. In revert, you can copy a selected remark block from a code to a Word document, and by means of macro ConcatenateParagraph remove remark markers and inner ends of paragraph, and thus prepare the text for changing and replacing. You can work to and fro by switching both subroutines. * Another benefit of ConcatenateParagraph is removing, in the selected paragraph, all the ’s that are appearing through a normal run of e-mail. In such a way you can convert that mutilated messages into a human language. Though this is aimed partly at Word groups, I offer this first here, hoping to receive some comments from those interested. Sub ConcatenateParagraph() ' Petr Bezucha, 2009 Dim Begin As Long 'selection of all the block to be arranged With Selection * 'Application.ScreenUpdating = False * Begin = .Start * 'cursor after the last character of the selection * .Collapse (wdCollapseEnd) * Do * * 'repeating: selection of the whole line, * * .MoveEnd Unit:=wdLine, Count:=-1 * * 'selection of the first character in the line * * .MoveRight Unit:=wdCharacter, Count:=1, Extend:=wdExtend * * 'deleting all *'s *and * ' as first characters * * Do * * * If .Text = "" Then .Delete Else Exit Do * * Loop * * If .Text = "'" Or .Text = "%" Then .Delete * * .Collapse (wdCollapseStart) * * 'finish if strikes the beginning * * If .Start <= Begin Then Exit Sub * * 'deleting end of paragraph * * .MoveLeft Unit:=wdCharacter, Count:=1, Extend:=wdExtend * * If Asc(.Text) = 13 Then .Delete * Loop End With 'Application.ScreenUpdating = True End Sub Sub MacroComment() ' Petr Bezucha, 2009 Dim MB As String, RM As Single, Finish As Long, Begin As Long Static ComMark As String If ComMark = "" Then * MB = InputBox("' * % * *Rem", "Marker of commentary line", "'") * If MB = "'" Or MB = "%" Then ComMark = MB Else Exit Sub End If With Selection * .Font.Name = "Courier New" * .ParagraphFormat.Alignment = wdAlignParagraphLeft * 'diminishing of the line length by approx. one character space * RM = .PageSetup.RightMargin * .PageSetup.RightMargin = RM + .Font.Size * MB = MsgBox("Change font size or margin if necessary", _ * * vbOKCancel + vbQuestion, "Comment layout") * 'exit with original line length * If MB = vbCancel Then .PageSetup.RightMargin = RM: Exit Sub * 'number of the first character * Begin = .Start * 'move to the very end of the paragraph * .EndKey Extend:=wdMove * 'select the last character and if it is space, delete it * .MoveLeft Unit:=wdCharacter, Count:=1, Extend:=wdExtend * If .Text = " " Then .Delete * 'move to the beginning of the line * .HomeKey Unit:=wdLine, Extend:=wdMove * 'add marker * .TypeText Text:=ComMark * Do * * .MoveUp Unit:=wdLine, Extend:=wdMove * * .EndOf Unit:=wdLine, Extend:=wdMove * * .TypeParagraph * * .MoveUp Unit:=wdLine, Count:=1, Extend:=wdMove * * .TypeText Text:=ComMark * Loop While .Start Begin + 1 * 'return to the original line length * * .PageSetup.RightMargin = RM End With End Sub Thanks Petr Bezucha |
Reply |
Thread Tools | Search this Thread |
Display Modes | |
|
|
Similar Threads | ||||
Thread | Forum | |||
Module Comments | Excel Programming | |||
VBA code in one Module referencing Public Variables Declared inanother Module failing | Excel Programming | |||
Need some comments on my Utility_Move class module. | Excel Worksheet Functions | |||
code in module A to not execute a Worksheet_SelectionChange sub of another module | Excel Discussion (Misc queries) | |||
Variable from a sheet module in a class module in XL XP | Excel Programming |