Hi,

I got this code by searching "DataGrid Printing". When I tried this code most of the code turned into red color (it means the code is not correct). How can it be corrected? The following is the code:

VB Code:
  1. Sub GridPrint(Grid As Control, szTitle As String, bLines As Integer)
  2.  
  3. Dim nRow As Long
  4.  
  5.  
  6. Dim nCol As Long
  7.  
  8.  
  9. Dim sCurrentX As Single
  10.  
  11.  
  12. Dim sCurrentY As Single
  13.  
  14.  
  15. Dim sMaxLine As Single
  16.  
  17.  
  18. Dim nPage As Integer
  19.  
  20.  
  21. Dim szPage As String
  22.  
  23.  
  24. Dim sOldY As Single
  25.  
  26.  
  27. Dim sPageWidth As Single
  28.  
  29.  
  30. Dim sBeginLeft As Single
  31.  
  32.  
  33. Dim sBeginGrid As Single
  34.  
  35.  
  36. If (Grid.Rows = 0) Then Exit Sub
  37.  
  38.  
  39. ' *** We resize the grid ***
  40.  
  41.  
  42. GridSize Grid
  43.  
  44.  
  45. ' *** We look for the maximum size for the fontsize ***
  46.  
  47.  
  48. sMaxLine = 99999999
  49.  
  50.  
  51. ' *** By default, we set the fontsize of the grid ***
  52.  
  53.  
  54. Printer.FontSize = Grid.FontSize
  55.  
  56.  
  57. ' *** Here is the maximum width of the page
  58.  
  59.  
  60. sPageWidth = Printer.Width * 0.94 - 200
  61.  
  62.  
  63. ' *** We calculate the maximum possible fontsize ***
  64.  
  65.  
  66. Do While (sMaxLine > sPageWidth) And (Printer.FontSize > 0)
  67.  
  68.  
  69. sMaxLine = 0
  70.  
  71.  
  72. For nCol = 0 To Grid.Cols - 1
  73.  
  74.  
  75. [B]sMaxLine = sMaxLine + (Printer.FontSize / Grid.FontSize) * [/B] Grid.ColWidth(nCol)
  76.  
  77.  
  78. Next
  79.  
  80.  
  81. ' *** We change the fontsiz if needed ***
  82.  
  83.  
  84. [B]If (sMaxLine > sPageWidth) Then Printer.FontSize = Printer.FontSize -
  85. 1 [/B]
  86.  
  87. Loop
  88.  
  89.  
  90. ' *** We begin on page 1 ***
  91.  
  92.  
  93. nPage = 1
  94.  
  95.  
  96. ' *** We put The title ***
  97.  
  98.  
  99. ' *** and the headers of each column ***
  100.  
  101.  
  102. GoSub PRINT_HEADERS
  103.  
  104.  
  105. For nRow = 1 To Grid.Rows - 1
  106.  
  107.  
  108. [B]If (bLines = True) Then Printer.Line (sBeginLeft,
  109. sCurrentY)-(sMaxLine, sCurrentY) [/B]
  110.  
  111. ' *** We print on a new page if needed ***
  112.  
  113.  
  114. If (sCurrentY >= Printer.Height * 0.93 - Printer.TextHeight("A")) Then
  115.  
  116.  
  117. If (bLines = True) Then
  118.  
  119.  
  120. ' *** Bottom line
  121.  
  122.  
  123. [B]Printer.Line (sBeginLeft, sCurrentY - 4 *
  124. Printer.TwipsPerPixelY)-(sMaxLine, sCurrentY - 4 *
  125. Printer.TwipsPerPixelY) [/B]
  126.  
  127. ' *** Left line ***
  128.  
  129.  
  130. [B]Printer.Line (sBeginLeft, sBeginGrid)-(sBeginLeft, sCurrentY - 4 *
  131. Printer.TwipsPerPixelY) [/B]
  132.  
  133. ' *** Right line ***
  134.  
  135.  
  136. [B]Printer.Line (sMaxLine, sBeginGrid)-(sMaxLine, sCurrentY - 4 *
  137. Printer.TwipsPerPixelY) [/B]
  138.  
  139. End If
  140.  
  141.  
  142. Printer.NewPage
  143.  
  144.  
  145. nPage = nPage + 1
  146.  
  147.  
  148. ' *** We put The title ***
  149.  
  150.  
  151. ' *** and the headers of each column ***
  152.  
  153.  
  154. GoSub PRINT_HEADERS
  155.  
  156.  
  157. End If
  158.  
  159.  
  160. sCurrentX = 4 * Printer.TwipsPerPixelX
  161.  
  162.  
  163. For nCol = 0 To Grid.Cols - 1
  164.  
  165.  
  166. If (nCol > 0) Then
  167.  
  168.  
  169. [B]sCurrentX = sCurrentX + (Printer.FontSize / Grid.FontSize) * [/B]
  170. Grid.ColWidth(nCol - 1)
  171.  
  172.  
  173. [B]If bLines = True Then Printer.Line (sCurrentX - 4 *
  174. Printer.TwipsPerPixelX, sBeginGrid)-(sCurrentX - 4 *
  175. Printer.TwipsPerPixelX, Printer.CurrentY + (Printer.TextHeight("A") /
  176. 2) - 4 * Printer.TwipsPerPixelY) [/B]
  177.  
  178.  
  179. End If
  180.  
  181.  
  182. ' *** Print cell text ***
  183.  
  184.  
  185. Grid.Col = nCol
  186.  
  187.  
  188. Grid.Row = nRow
  189.  
  190.  
  191. Printer.CurrentX = sCurrentX
  192.  
  193.  
  194. Printer.CurrentY = sCurrentY + (Printer.TextHeight("A") / 2)
  195.  
  196.  
  197. Printer.Print Grid.Text
  198.  
  199.  
  200. Next
  201.  
  202.  
  203. sCurrentY = sCurrentY + (Printer.TextHeight("A") * 2)
  204.  
  205.  
  206. Next
  207.  
  208.  
  209. If (bLines = True) Then
  210.  
  211.  
  212. ' *** Bottom line
  213.  
  214.  
  215. [B]Printer.Line (sBeginLeft, sCurrentY - 4 *
  216. Printer.TwipsPerPixelY)-(sMaxLine, sCurrentY - 4 *
  217. Printer.TwipsPerPixelY) [/B]
  218.  
  219. ' *** Left line ***
  220.  
  221.  
  222. [B]Printer.Line (sBeginLeft, sBeginGrid)-(sBeginLeft, sCurrentY - 4 *
  223. Printer.TwipsPerPixelY) [/B]
  224.  
  225. ' *** Right line ***
  226.  
  227.  
  228. [B]Printer.Line (sMaxLine, sBeginGrid)-(sMaxLine, sCurrentY - 4 *
  229. Printer.TwipsPerPixelY) [/B]
  230.  
  231. End If
  232.  
  233.  
  234. Printer.EndDoc
  235.  
  236.  
  237. Exit Sub
  238.  
  239.  
  240. PRINT_HEADERS:
  241.  
  242.  
  243. ' *** We print the title ***
  244.  
  245.  
  246. sCurrentY = Printer.CurrentY
  247.  
  248.  
  249. Printer.FontBold = True
  250.  
  251.  
  252. Printer.Print szTitle
  253.  
  254.  
  255. Printer.FontBold = False
  256.  
  257.  
  258. Printer.Print ""
  259.  
  260.  
  261. ' *** We print the page number on the first line ***
  262.  
  263.  
  264. sOldY = Printer.CurrentY
  265.  
  266.  
  267. szPage = "Page " & CStr(nPage)
  268.  
  269.  
  270. Printer.FontItalic = True
  271.  
  272.  
  273. Printer.CurrentX = sPageWidth - Printer.TextWidth(szPage)
  274.  
  275.  
  276. Printer.CurrentY = sCurrentY
  277.  
  278.  
  279. Printer.Print szPage
  280.  
  281.  
  282. Printer.FontItalic = False
  283.  
  284.  
  285. Printer.CurrentY = sOldY
  286.  
  287.  
  288. ' *** We print the grid ***
  289.  
  290.  
  291. Printer.CurrentY = Printer.CurrentY + (Printer.TextHeight("A"))
  292.  
  293.  
  294. sCurrentY = Printer.CurrentY
  295.  
  296.  
  297. sBeginGrid = sCurrentY
  298.  
  299.  
  300. sBeginLeft = 0
  301.  
  302.  
  303. sCurrentX = 4 * Printer.TwipsPerPixelX
  304.  
  305.  
  306. ' *** We print the header of each column ***
  307.  
  308.  
  309. [B]If (bLines = True) Then Printer.Line (sBeginLeft,
  310. sCurrentY)-(sMaxLine, sCurrentY) [/B]
  311.  
  312. Printer.Print
  313.  
  314.  
  315. sCurrentY = Printer.CurrentY + (Printer.TextHeight("A") / 2)
  316.  
  317.  
  318. For nCol = 0 To Grid.Cols - 1
  319.  
  320.  
  321. [B]If (nCol > 0) Then sCurrentX = sCurrentX + (Printer.FontSize /
  322. Grid.FontSize) * Grid.ColWidth(nCol - 1) [/B]
  323.  
  324. ' *** Print cell text ***
  325.  
  326.  
  327. Grid.Col = nCol
  328.  
  329.  
  330. Grid.Row = 0
  331.  
  332.  
  333. Printer.CurrentX = sCurrentX
  334.  
  335.  
  336. Printer.CurrentY = sCurrentY
  337.  
  338.  
  339. Printer.Print Grid.Text
  340.  
  341.  
  342. Next
  343.  
  344.  
  345. sCurrentY = sCurrentY + (Printer.TextHeight("A") * 1.5)
  346.  
  347.  
  348. Printer.Print
  349.  
  350.  
  351. [B]If (bLines = True) Then Printer.Line (sBeginLeft,
  352. sCurrentY)-(sMaxLine, sCurrentY) [/B]
  353. Return
  354. End Sub
  355.  
  356.  
  357. Sub GridSize(grdGrid As Control)
  358.  
  359.  
  360. ' *** We resize the grid ***
  361. Dim szTmp As String
  362.  
  363.  
  364. Dim nColSize As Long
  365.  
  366.  
  367. Dim nCounter As Long
  368.  
  369.  
  370. Dim nArray() As Integer
  371.  
  372.  
  373. Dim nRow As Integer
  374.  
  375.  
  376. ReDim nArray(grdGrid.Cols)
  377.  
  378.  
  379. For nCounter = 0 To grdGrid.Cols - 1
  380.  
  381.  
  382. For nRow = 0 To grdGrid.Rows - 1
  383.  
  384.  
  385. szTmp = GridGetText(grdGrid, nRow, nCounter)
  386.  
  387.  
  388. nColSize = IIf(Len(szTmp) < 1, 1, Len(szTmp))
  389.  
  390.  
  391. If (nArray(nCounter) < nColSize) Then nArray(nCounter) = nColSize
  392.  
  393.  
  394. Next
  395.  
  396.  
  397. Next
  398.  
  399.  
  400. For nCounter = 0 To grdGrid.Cols - 1
  401.  
  402.  
  403. grdGrid.ColWidth(nCounter) = nArray(nCounter) * grdGrid.FontSize * 14
  404.  
  405.  
  406. Next
  407.  
  408.  
  409. End Sub
  410.  
  411.  
  412. [B]Function GridGetText(grdGrid As Control, ByVal nRow As Long, ByVal
  413. nCol As Long) As String [/B]
  414.  
  415.  
  416. ' *** We get the text of a cell in the grid ***
  417. Dim nOldCol As Long
  418.  
  419.  
  420. nOldCol = grdGrid.Col
  421.  
  422.  
  423. grdGrid.Row = nRow
  424.  
  425.  
  426. grdGrid.Col = nCol
  427.  
  428.  
  429. GridGetText = grdGrid.Text
  430.  
  431.  
  432. grdGrid.Col = nOldCol
  433. End Function

The code in bold font is shown in red color.

Could somebody help me out, please?


Regards,

Seema_s