分享

在Word中用VBA写的辅助打印彩色VB代码的小程序

 nxhujiee 2010-11-29

由 于最近开发软件的过程中需要打印VB的代码,但当代码很多时感觉打印出来的黑黑的满篇子不如在编辑器里看那彩色的代码省力,在网上找了半天也没找到合适的 工具来解决这个问题,看来想偷懒是不行了,于是就用Word编了一个小程序,算法不优,速度也不快,不过总比手动强多了0 && image.height>0){if(image.width>=700){this.width=700;this.height=image.height*700/image.width;}}" src="http://blog.hexun.com//CuteSoft_Client/CuteEditor/images/emwink.gif" src_cetemp="http://blog.hexun.com//CuteSoft_Client/CuteEditor/images/emwink.gif" align="absMiddle" border="0">, 以后在网上发代码也可以是彩色的了,下面就是用这段代码就是程序格式化后的效果。下载后将两个文件导入到Word的VBA中,然后第一次使用前先运行宏 “ShowFormatSet”做好相关设置,再运行宏“FormatVBCode”就能将复制到word中的VB代码格式化好了。说明:最终格式化结果 可能和vb编辑器略有不同。

Dim EditChanged As Boolean, CurrRow As Integer, CurrCol As Integer, EditState As Boolean

 

Private Sub Command1_Click()

    '设置单元格A1的格式为"K0+000.00"

    Sheet("A1").FormatString = "K0+000.00": Call Calculate

End Sub

 

Private Sub Form_Load()

    Dim I As Integer, J As Integer

    Me.Caption = "模拟Excel计算表格"

    CreateTableHead 100 '生成表头

    With Text2

        .Appearance = 0

        .Visible = False: EditChanged = False

        .Font.Size = 11

    End With

    With MSHFlexGrid1

        Frame1.Caption = "单元格" & .TextMatrix(0, 1) & .TextMatrix(1, 0) & "的公式"

        '初始化表格对象

        For J = 1 To .Rows - 1

            For I = 1 To .Cols - 1

                          '单元格地址用A1形式表示,公式,单元格格式,单元格文本,,,索引关键字

                Sheet.Add .TextMatrix(0, I) & J, "", "", "", J, I, .TextMatrix(0, I) & J

            Next

        Next

    End With

   

End Sub

Private Sub CreateTableHead(R As Integer)

    With MSHFlexGrid1

        .Cols = 20

        .Rows = 20

        .Font.Size = 12

        .AllowUserResizing = flexResizeBoth

        s$ = " |"

        For J = 65 To 90

            s$ = s$ & Chr(J) & "|"

        Next

        s$ = Left(s$, Len(s$) - 1)

        s$ = s$ & ";|"

        For J = 1 To R

            s$ = s$ & J & "|"

        Next

        .FormatString = Left(s$, Len(s$) - 1)

        For J = 1 To 26

            .ColWidth(J) = 1000

        Next

    End With

End Sub

 

Private Sub Label1_Click()

'打开某个网址

'ShellExecute Me.hwnd, "open", "http://dongxingsofthome.blog.hexun.com/", vbNullString, vbNullString, vbNormalFocus

Shell "C:\\Program Files\\Internet Explorer\\IEXPLORE.EXE http://dongxingsofthome.blog.hexun.com/8341928_d.html", vbMaximizedFocus

'给某个信箱发电子邮件

'ShellExecute hWnd, "open", "mailto:sst95@21cn.com", vbNullString, vbNullString, 0

 

End Sub

 

Private Sub MSHFlexGrid1_DblClick()

    If MSHFlexGrid1.Text <> "" Then

        EditState = True

    Else

        EditState = False

    End If

    With MSHFlexGrid1

        Text2.Text = Sheet(.TextMatrix(0, .Col) & .Row).Formula

    End With

    Text2.Visible = True

    With MSHFlexGrid1

        Text2.Top = .CellTop + 2010

        Text2.Left = .CellLeft + 90

        Text2.Height = .CellHeight - 20

        Text2.Width = .CellWidth + 30

        Text2.SetFocus

    End With

End Sub

 

Private Sub MSHFlexGrid1_KeyDown(KeyCode As Integer, Shift As Integer)

 

    If KeyCode = 229 Then

        Text2.Text = ""

    ElseIf KeyCode <> 37 And KeyCode <> 38 And KeyCode <> 39 And KeyCode <> 40 Then

        Text2.Text = ""

    End If

    If KeyCode = 46 Then '处理Delete

        Text1.Text = ""

        With MSHFlexGrid1

            For J = .Row To .RowSel

                For I = .Col To .ColSel

                    .TextMatrix(J, I) = ""

                    Sheet.Item(.TextMatrix(0, I) & J).Formula = ""

                Next

            Next

        End With

    End If

End Sub

 

Private Sub MSHFlexGrid1_KeyPress(KeyAscii As Integer)

 

    If KeyAscii < 255 And KeyAscii <> 27 And KeyAscii <> 8 Then

        If Chr(KeyAscii) = "=" Then

            Text2.Text = ""

        End If

        Text2.Text = Text2.Text & Chr(KeyAscii)

    End If

    If KeyAscii = 8 Then 'back

        Text2.Text = ""

    End If

    If KeyAscii <> 27 And KeyAscii <> 13 Then

        Text2.SelStart = Len(Text2.Text)

        Text2.Visible = True

        With MSHFlexGrid1

            Text2.Top = .CellTop + 2010

            Text2.Left = .CellLeft + 90

            Text2.Height = .CellHeight - 20

            Text2.Width = .CellWidth + 30

            Text2.SetFocus

        End With

    End If

    If KeyAscii = 13 Then

        MSHFlexGrid1.Row = MSHFlexGrid1.Row + 1: EditChanged = False

        With MSHFlexGrid1

            Frame1.Caption = "单元格" & .TextMatrix(0, .Col) & .TextMatrix(.Row, 0) & "的公式"

            Text1.Text = Sheet.Item(MSHFlexGrid1.TextMatrix(0, MSHFlexGrid1.Col) & MSHFlexGrid1.Row).Formula

        End With

    End If

End Sub

 

Private Sub MSHFlexGrid1_RowColChange()

    If EditChanged = True Then

        With MSHFlexGrid1

            .TextMatrix(CurrRow, CurrCol) = Text2.Text

            Sheet.Item(.TextMatrix(0, CurrCol) & CurrRow).Formula = Text2.Text

        End With

        Call Calculate

    End If

    Text2.Visible = False: EditChanged = False

    Text1.Text = Sheet.Item(MSHFlexGrid1.TextMatrix(0, MSHFlexGrid1.Col) & MSHFlexGrid1.Row).Formula

End Sub

 

Private Sub MSHFlexGrid1_SelChange()

    With MSHFlexGrid1

        Frame1.Caption = "单元格" & .TextMatrix(0, .Col) & .TextMatrix(.Row, 0) & "的公式"

        '在公式栏内显示单元格的公式

        Text1.Text = Sheet.Item(MSHFlexGrid1.TextMatrix(0, MSHFlexGrid1.Col) & MSHFlexGrid1.Row).Formula

    End With

End Sub

 

Private Sub Text2_Change()

    EditChanged = True

    CurrRow = MSHFlexGrid1.Row

    CurrCol = MSHFlexGrid1.Col

End Sub

 

Private Sub Text2_KeyDown(KeyCode As Integer, Shift As Integer)

    Select Case KeyCode '处理光标键

        Case 37, 38, 39, 40

            If EditState = False Then

                Call SetCellContent(Text2.Text)

                Text2.Visible = False

                If KeyCode = 40 Then

                    MSHFlexGrid1.Row = MSHFlexGrid1.Row + 1

                ElseIf KeyCode = 37 Then

                    MSHFlexGrid1.Col = MSHFlexGrid1.Col - 1

                ElseIf KeyCode = 39 Then

                    MSHFlexGrid1.Col = MSHFlexGrid1.Col + 1

                ElseIf KeyCode = 38 Then

                    MSHFlexGrid1.Row = MSHFlexGrid1.Row - 1

                End If

            End If

            EditState = False

            MSHFlexGrid1.SetFocus

    End Select

End Sub

 

Private Sub Text2_KeyPress(KeyAscii As Integer)

    Select Case KeyAscii

        Case 13 '处理回车键Enter

                Call SetCellContent(Text2.Text)

                Text2.Visible = False

                MSHFlexGrid1.Row = MSHFlexGrid1.Row + 1

                MSHFlexGrid1.SetFocus

                EditState = False

                EditChanged = False

            MSHFlexGrid1.SetFocus

        Case 27 '处理ESC

            Text2.Visible = False

            MSHFlexGrid1.SetFocus

            EditChanged = False

    End Select

End Sub

 

0 && image.height>0){if(image.width>=700){this.width=700;this.height=image.height*700/image.width;}}" alt="查看更多精彩图片" src="http://photo5.hexun.com/p/2007/0321/84313/b_F54E8D9A59F01B29A80CDDBFF8084ABC.jpg" src_cetemp="http://photo5.hexun.com/p/2007/0321/84313/b_F54E8D9A59F01B29A80CDDBFF8084ABC.jpg" border="0">点此下载

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多