Advertisement
Guest User

Untitled

a guest
Jun 24th, 2019
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.04 KB | None | 0 0
  1. Sub fournitureExcel(trigram As String, nbTable As Long, folderPath As String)
  2.  
  3. Dim filename As String, dataRange As String, dataC As New Collection
  4. Dim refRow As Long, refColumn As Long, desigColumn As Long 'la ligne de la trigramme recherche
  5. Dim j As Long, c As Long
  6.  
  7. With ActiveDocument
  8.  
  9. .Application.ScreenUpdating = False
  10.  
  11. On Error Resume Next
  12. Set xlApp = GetObject(, "Excel.Application")
  13. If err Then
  14. Set xlApp = CreateObject("Excel.Application")
  15. End If
  16. On Error GoTo 0
  17.  
  18. filename = "DE_Nom_art_" & trigram & ".xlsx"
  19. Set xlBook = xlApp.workbooks.Open(folderPath & filename)
  20. xlApp.Visible = False 'does not open the file, read only => faster to get the info
  21.  
  22. With xlBook.sheets(1)
  23.  
  24. ' searching for the Reference
  25. Set rg = .Cells.Find(what:="Référence")
  26. refRow = .Range(rg.Address).Row: refColumn = .Range(rg.Address).Column
  27.  
  28. Set desigAdrs = .Cells.Find(what:="Désignation")
  29. 'numero de colone Designation
  30. desigColumn = .Range(desigAdrs.Address).Column: dataRange = "G" & (refRow + 2) & ":I" & 10000
  31. 'stock excel data into a collection
  32. For Each cell In .Range(dataRange)
  33.  
  34. If cell.Column = refColumn Then
  35. If Not IsEmpty(cell) Then ' checking if reference exists or not
  36. 'designation & quantite
  37. dataC.Add .Cells(cell.Row, refColumn - 2).Value: dataC.Add .Cells(cell.Row, refColumn - 1).Value
  38. End If
  39. End If
  40. Next cell
  41. xlBook.Close SaveChanges:=False ' pour ne pas sauvegarder le document
  42. Set src = Nothing
  43. Set xlApp = Nothing
  44. Set xlBook = Nothing
  45. End With
  46. 'ajoute des lignes a la table fournitures i.e table nr3
  47. .Tables(nbTable).Select
  48. c = .Tables(nbTable).Range.Rows.Count
  49. 'c = .Tables(nbTable).Rows.Count
  50. If c - (dataC.Count / 2) < 0 Then 'check if we need to add rows or not
  51. With Selection
  52. .InsertRowsBelow -(c - (dataC.Count / 2))
  53. With .Shading
  54. .Texture = wdTextureNone
  55. .ForegroundPatternColor = wdColorAutomatic
  56. .BackgroundPatternColor = -603914241
  57. End With
  58. .Font.ColorIndex = wdBlack
  59. 'ajout des bordures dans le tableau
  60. With .Borders
  61. .InsideLineStyle = wdLineStyleSingle
  62. .OutsideLineStyle = wdLineStyleSingle
  63. .InsideColorIndex = wdBlack
  64. .OutsideColorIndex = wdBlack
  65. End With
  66. End With
  67. Else
  68. ' do nothing
  69. End If
  70. j = 3 'indice apartir du quel on va commencer a lire les donnees de la collection car on skip les 2 premiers
  71. 'fill the table
  72. For i = 2 To dataC.Count / 2
  73. With .Tables(nbTable).Rows(i)
  74. ' la designation & la quantites
  75. With .Cells(1).Range
  76. .Text = dataC(j):
  77. .ParagraphFormat.Alignment = wdAlignParagraphLeft 'aligne text to the left
  78. End With
  79. .Cells(2).Range.Text = dataC(j + 1)
  80. With .Range
  81. .Font.ColorIndex = wdBlack 'text color :black
  82. .Font.Size = 9 ' Set String size = 9
  83. ' If the string begins with "Baie" then make it Bold
  84. If Left(dataC(j), Len("Baie")) = "Baie" Then
  85. .Bold = True
  86. Else
  87. .Bold = False
  88. End If
  89. End With
  90. j = j + 2
  91. End With
  92. Next i
  93. 'ActiveDocument.Tables(3).Rows.Last.Cells.Delete 'on efface la derniere ligne
  94. .Application.ScreenUpdating = True
  95. End With
  96. End Sub
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement