Consulting

Results 1 to 7 of 7

Thread: Code Printing

  1. #1
    VBAX Expert
    Joined
    Feb 2005
    Posts
    929
    Location

    Code Printing

    Does anyone have a simple method to print out code modules and have the results look like compilations of old, i.e., each module starts on a new page, etc. I wrote a postprocessor for Word that does most of what I want, but there must be an easier way.

    thanks
    "It's not just the due date that's important, it's also the do date" [MWE]

    When your problem has been resolved, mark the thread SOLVED by clicking on the Thread Tools dropdown menu at the top of the thread.

  2. #2
    Administrator
    Chat VP
    VBAX Guru johnske's Avatar
    Joined
    Jul 2004
    Location
    Townsville, Australia
    Posts
    2,872
    Location
    Not sure if this's what you're after (it's a mod from a very-much WIP)

    [VBA]
    Option Explicit
    '
    Sub GetProjectCodeForPrinting()
    '
    Dim N As Long
    Dim Component As VBComponent
    Dim OpenBook As Workbook
    '
    Set OpenBook = ActiveWorkbook
    '
    Application.ScreenUpdating = False
    Workbooks.Add (xlWBATWorksheet)
    '
    GoSub FormatSheet
    '
    For Each Component In OpenBook.VBProject.VBComponents
    With [A3]
    .Value = " " & Component.Name & " Code Module"
    With .Font
    .Size = 9
    .Bold = True
    .Underline = True
    .ColorIndex = 11
    End With
    End With
    '
    With Component.CodeModule
    For N = 1 To .CountOfLines
    If .Lines(N, 1) = Empty Then
    Range("A" & Rows.Count).End(xlUp).Offset(1, 0) = "'"
    Else
    'put space before and after codeline (this is needed)
    Range("A" & Rows.Count).End(xlUp).Offset(1, 0) = " " & .Lines(N, 1) & " "
    End If
    Next
    End With
    'Call FormatCodeAsPerVBIDE
    'Call ConvertToBBcode
    ActiveWorkbook.Sheets.Add after:=Sheets(Sheets.Count)
    GoSub FormatSheet
    Next
    '
    Application.DisplayAlerts = False
    Sheets(Sheets.Count).Delete 'there's 1 too many sheets
    Application.DisplayAlerts = True
    Application.ScreenUpdating = True
    '
    Exit Sub
    '
    FormatSheet:
    '
    With ActiveWindow
    .DisplayHeadings = False
    .DisplayGridlines = False
    End With
    '
    With ActiveSheet
    With .Cells.Font
    .Name = "Verdana"
    .Size = 8
    End With
    '
    With .[A1]
    .Value = " " & OpenBook.Name & " " & OpenBook.VBProject.Name
    With .Font
    .Size = 12
    .Bold = True
    .Underline = True
    End With
    End With
    End With
    Return
    '
    End Sub

    [/VBA]
    You know you're really in trouble when the light at the end of the tunnel turns out to be the headlight of a train hurtling towards you

    The major part of getting the right answer lies in asking the right question...


    Made your code more readable, use VBA tags (this automatically inserts [vba] at the start of your code, and [/vba ] at the end of your code) | Help those helping you by marking your thread solved when it is.

  3. #3
    VBAX Mentor Marcster's Avatar
    Joined
    Jun 2005
    Posts
    434
    Location
    There's a product selling for $59 that will print your VBA code in colour, to PDF, HTML, rich text etc...

    http://www.starprint2000.com/vbaprint_features.html

    Marcster.

  4. #4
    Administrator
    Chat VP VBAX Guru johnske's Avatar
    Joined
    Jul 2004
    Location
    Townsville, Australia
    Posts
    2,872
    Location
    Why pay when you can do it for nothing?
    You know you're really in trouble when the light at the end of the tunnel turns out to be the headlight of a train hurtling towards you

    The major part of getting the right answer lies in asking the right question...


    Made your code more readable, use VBA tags (this automatically inserts [vba] at the start of your code, and [/vba ] at the end of your code) | Help those helping you by marking your thread solved when it is.

  5. #5
    VBAX Expert
    Joined
    Feb 2005
    Posts
    929
    Location
    Quote Originally Posted by johnske
    Not sure if this's what you're after (it's a mod from a very-much WIP)

    [vba]
    Option Explicit
    '
    Sub GetProjectCodeForPrinting()
    '
    Dim N As Long
    Dim Component As VBComponent
    Dim OpenBook As Workbook
    '
    Set OpenBook = ActiveWorkbook
    '
    Application.ScreenUpdating = False
    Workbooks.Add (xlWBATWorksheet)
    '
    GoSub FormatSheet
    '
    For Each Component In OpenBook.VBProject.VBComponents
    With [A3]
    .Value = " " & Component.Name & " Code Module"
    With .Font
    .Size = 9
    .Bold = True
    .Underline = True
    .ColorIndex = 11
    End With
    End With
    '
    With Component.CodeModule
    For N = 1 To .CountOfLines
    If .Lines(N, 1) = Empty Then
    Range("A" & Rows.Count).End(xlUp).Offset(1, 0) = "'"
    Else
    'put space before and after codeline (this is needed)
    Range("A" & Rows.Count).End(xlUp).Offset(1, 0) = " " & .Lines(N, 1) & " "
    End If
    Next
    End With
    'Call FormatCodeAsPerVBIDE
    'Call ConvertToBBcode
    ActiveWorkbook.Sheets.Add after:=Sheets(Sheets.Count)
    GoSub FormatSheet
    Next
    '
    Application.DisplayAlerts = False
    Sheets(Sheets.Count).Delete 'there's 1 too many sheets
    Application.DisplayAlerts = True
    Application.ScreenUpdating = True
    '
    Exit Sub
    '
    FormatSheet:
    '
    With ActiveWindow
    .DisplayHeadings = False
    .DisplayGridlines = False
    End With
    '
    With ActiveSheet
    With .Cells.Font
    .Name = "Verdana"
    .Size = 8
    End With
    '
    With .[A1]
    .Value = " " & OpenBook.Name & " " & OpenBook.VBProject.Name
    With .Font
    .Size = 12
    .Bold = True
    .Underline = True
    End With
    End With
    End With
    Return
    '
    End Sub

    [/vba]
    thanks. This is specific to Excel and I am looking for something more general. However, it contians some useful concepts and I will see what I can do with it.
    "It's not just the due date that's important, it's also the do date" [MWE]

    When your problem has been resolved, mark the thread SOLVED by clicking on the Thread Tools dropdown menu at the top of the thread.

  6. #6
    VBAX Expert
    Joined
    Feb 2005
    Posts
    929
    Location
    Quote Originally Posted by Marcster
    There's a product selling for $59 that will print your VBA code in colour, to PDF, HTML, rich text etc...

    http://www.starprint2000.com/vbaprint_features.html

    Marcster.
    thanks for the lead. For now, I will poke at what is available for free as source code (and can thus be tweaked to do exactly what I want)
    "It's not just the due date that's important, it's also the do date" [MWE]

    When your problem has been resolved, mark the thread SOLVED by clicking on the Thread Tools dropdown menu at the top of the thread.

  7. #7
    Administrator
    Chat VP VBAX Guru johnske's Avatar
    Joined
    Jul 2004
    Location
    Townsville, Australia
    Posts
    2,872
    Location
    Quote Originally Posted by MWE
    thanks. This is specific to Excel and I am looking for something more general. However, it contians some useful concepts and I will see what I can do with it.
    Maybe this can be of more use then...
    [vba]
    Option Explicit

    Private Sub AddReference()
    'call this with a workbook open event
    Dim Reference As Object

    With ThisWorkbook.VBProject
    For Each Reference In .References
    If Reference.Description Like "Microsoft Forms 2.0 Object Library" Then Exit Sub
    Next
    .References.AddFromGuid "{0D452EE1-E08F-101A-852E-02608C4D0BB4}", 2, 0
    End With

    End Sub


    Sub CopyCodeToClipboard()

    Dim CodeText As String, CodeBody As New DataObject

    With ThisWorkbook.VBProject.VBComponents("Module1").CodeModule
    CodeText = .Lines(1, .countoflines)
    With CodeBody
    .SetText CodeText
    .PutInClipboard
    End With
    MsgBox "Module1 code is in the clipboard - paste it to where-ever you want"
    End With

    Set CodeBody = Nothing

    End Sub
    [/vba]If you want to format it in colour as per the VBIDE, let me know and I'll give you what I have so far
    You know you're really in trouble when the light at the end of the tunnel turns out to be the headlight of a train hurtling towards you

    The major part of getting the right answer lies in asking the right question...


    Made your code more readable, use VBA tags (this automatically inserts [vba] at the start of your code, and [/vba ] at the end of your code) | Help those helping you by marking your thread solved when it is.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •