Vba code formatter1/22/2024 Public Sub ColorComments(ByVal rng As Range)įor more information on programming the Microsoft Office applications with VBA code, see my book Microsoft Office Programming: A Guide for Experienced Developers. If it finds a single quote while a string is not open, it colors the rest of the paragraph green. It searches each paragraph character-by-character, keeping track of whether a quoted string is open. Options.AutoFormatAsYouTypeReplaceQuotes = _įinally subroutine ColorComments makes comments green. Options.AutoFormatAsYouTypeReplaceQuotes = False Public Sub StraightenQuotes(ByVal rng As Range) ' Replace curly quotes with straight quotes. When I select few lines of codes, go to Format -> Align ->. Also notics how it restores the original value for this setting when it is done. Ive been trying for sometime to left align my code, but the Align menu is faded off. Notice how it turns AutoFormatAsYouTypeReplaceQuotes off before starting to prevent Word from automatically replacing the straight quotes with curly quotes. Subroutine StraightenQuotes replaces curly quotes with straight quotes. Selection_range.Paragraphs(selection_).Style _ Selection_range.Style = ActiveDocument.Styles("Code " & _ Public Sub SetCodeStyles(ByVal rng As Range) ' Set the other paragraphs' styles to Code. ' Set the range's last paragraph's style to Code Last. It then sets the first and last paragraphs to Code First and Code Last. If the selection contains more than one paragraph, the code sets it's style to Code. For private or public declaration type p or pu before the type: private varname as boolean by typing pbool public varname as boolean by typing pubool. If the selection includes a single paragraph, it sets the paragraph's style to Code Single. Type the variable type: dim varname as boolean by typing bool dim varname as integer by typing int dim varname as string by typing string dim varname as type by typing type. Subroutine SetCodeStyles sets the selected text's style. ' Set the code styles, straighten quotes, and color ' comments. ' Format the selected text so it looks sort of like code. Subroutine ToCode calls subroutines SetCodeStyles, StraightenQuotes, and ColorComments to do the work. See the downloadable Word document for examples. You might also indent, use a slightly smaller font, and make the code bold. You might add extra paragraph spacing before Code First, after Code Last, and both before and after Code Single. Typically these will all use a fixed-width font. To use these macros, define Code, Code First, Code Last, and Code Single styles. This example shows how to use VBA macros to format text in Word to look like code. Use VBA macros to format text in Word to look like code VB Helper: HowTo: Use VBA macros to format text in Word to look like code
0 Comments
Leave a Reply.AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |