Advertisement
Guest User

vba

a guest
Jul 24th, 2014
38
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Sub ShowForm()
  2. Dim CaseFiles(1000) As String
  3. Dim i As Integer
  4. Dim j As Integer
  5. Dim CountFiles As Integer
  6. Dim Duplicate As Boolean
  7.  
  8. CountFiles = 0
  9. i = 2
  10. While (ThisWorkbook.Sheets("data").Cells(i, 1).Value <> Null) Or (ThisWorkbook.Sheets("data").Cells(i, 1).Value <> "")
  11.     Duplicate = False
  12.     j = 1
  13.     While (j <= CountFiles) And (Duplicate = False)
  14.         If ThisWorkbook.Sheets("data").Cells(i, 1).Value = CaseFiles(j) Then Duplicate = True
  15.         j = j + 1
  16.     Wend
  17.    
  18.     If Not Duplicate Then
  19.         CountFiles = CountFiles + 1
  20.         CaseFiles(CountFiles) = ThisWorkbook.Sheets("data").Cells(i, 1).Value
  21.         UserForm1.ComboBox1.AddItem CaseFiles(CountFiles)
  22.     End If
  23.    
  24.     i = i + 1
  25. Wend
  26.  
  27. UserForm1.ComboBox2.AddItem "level1"
  28. UserForm1.ComboBox2.AddItem "level2"
  29.  
  30. UserForm1.Show
  31.  
  32.  
  33. End Sub
  34.  
  35. Sub LoadTemplate(template As String, CaseFile As String)
  36. Dim wdApp As Word.Application
  37. Dim wdDoc As Word.Document
  38. Dim S As Word.Selection
  39. Dim FPath As String
  40. Dim CaseFileRow As Integer
  41. Dim CaseFileFound As Boolean
  42. Dim SearchString As String
  43. Dim i As Integer
  44.  
  45. CaseFileFound = False
  46. CaseFileRow = 2
  47. While ((ThisWorkbook.Sheets("data").Cells(CaseFileRow, 1).Value <> Null) Or (ThisWorkbook.Sheets("data").Cells(CaseFileRow, 1).Value <> "")) And (Not CaseFileFound)
  48.     If ThisWorkbook.Sheets("data").Cells(CaseFileRow, 1).Value = CaseFile Then
  49.         CaseFileFound = True
  50.     Else
  51.         CaseFileRow = CaseFileRow + 1
  52.     End If
  53. Wend
  54.  
  55. On Error Resume Next
  56. Set wdApp = GetObject(, "Word.Application")
  57.  
  58. If Err.Number <> 0 Then 'Word isn't already running
  59.    Set wdApp = CreateObject("Word.Application")
  60. End If
  61.  
  62. On Error GoTo 0
  63. 'Open Template file
  64. FPath = ThisWorkbook.Path
  65. If FileExists(FPath + "\Templates", True) Then
  66.     Set wdDoc = wdApp.Documents.Open(FPath + "\Templates\L____(" + template + ")_empty.docx")
  67. Else
  68.     Set wdDoc = wdApp.Documents.Open(FPath + "\L____(" + template + ")_empty.docx")
  69. End If
  70. wdApp.Visible = True
  71.  
  72. For i = 1 To 75
  73.     If ThisWorkbook.Sheets("data").Cells(1, i).Value <> "" Then
  74.         SearchString = "$" + FormatCellValue(ThisWorkbook.Sheets("data").Cells(1, i).Value) + "(" + ConvertToLetter(i) + ")$"
  75.         With wdDoc.Range.Find
  76.             .Format = True
  77.             .Text = SearchString
  78.             .Replacement.Text = ThisWorkbook.Sheets("data").Cells(CaseFileRow, i).Text
  79.             .Replacement.Font.Color = RGB(0, 0, 0)
  80.             .Execute Replace:=wdReplaceAll, Forward:=True, Wrap:=wdFindContinue
  81.         End With
  82.     End If
  83. Next i
  84.  
  85.  
  86. wdApp.WindowState = wdWindowStateMaximize
  87. wdApp.Activate
  88.  
  89. FPath = FPath + "\L" + CaseFile + "(" + template + ").docx"
  90. wdDoc.SaveAs FPath
  91. End Sub
  92.  
  93. Function ConvertToLetter(iCol As Integer) As String
  94.    Dim iAlpha As Integer
  95.    Dim iRemainder As Integer
  96.    iAlpha = Int(iCol / 27)
  97.    iRemainder = iCol - (iAlpha * 26)
  98.    If iAlpha > 0 Then
  99.       ConvertToLetter = Chr(iAlpha + 64)
  100.    End If
  101.    If iRemainder > 0 Then
  102.       ConvertToLetter = ConvertToLetter & Chr(iRemainder + 64)
  103.    End If
  104. End Function
  105.  
  106. Function FormatCellValue(Value As String) As String
  107. Dim i As Integer
  108. Dim Formated As Boolean
  109.  
  110. 'For i = Len(Value) To 1
  111. Formated = False
  112. i = Len(Value)
  113. While (i >= 1) And (Not Formated)
  114.     If Mid(Value, i, 1) = " " Then
  115.         i = i - 1
  116.     Else
  117.         Formated = True
  118.         Value = Mid(Value, 1, i)
  119.     End If
  120. Wend
  121.  
  122. Formated = False
  123. i = 1
  124. While (i <= Len(Value)) And (Not Formated)
  125.     If Mid(Value, i, 1) = " " Then
  126.         i = i + 1
  127.     Else
  128.         Formated = True
  129.         FormatCellValue = Mid(Value, i, Len(Value))
  130.     End If
  131. Wend
  132. End Function
  133.  
  134. Private Function FileExists(ByVal sPathName As String, Optional Directory As Boolean) As Boolean
  135.  
  136. On Error Resume Next
  137. If sPathName <> "" Then
  138.     If IsMissing(Directory) Or Directory = False Then
  139.         FileExists = (Dir$(sPathName) <> "")
  140.     Else
  141.         FileExists = (Dir$(sPathName, vbDirectory) <> "")
  142.     End If
  143. End If
  144. End Function
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement