Advertisement
JustJoe21

Untitled

Dec 27th, 2024
19
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 20.06 KB | None | 0 0
  1. Top Userform
  2.  
  3. Private Sub cb_DualTCs_Click()
  4. DualsUserform.Show
  5. End Sub
  6.  
  7. Private Sub cmb_Duals_Change()
  8. ' When a name is selected, get its full version
  9. Dim fullName As String
  10. fullName = GetFullName(cmb_Duals.Value)
  11.  
  12. ' Using tag property to identify the content control
  13. Dim cc As ContentControl
  14. For Each cc In ActiveDocument.ContentControls
  15. If cc.tag = "text_PerfDual" Then
  16. cc.Range.text = fullName
  17. Exit For
  18. End If
  19. Next cc
  20. End Sub
  21.  
  22. Private Sub cmb_Test_Click()
  23. ' Add bottom entries with user-specific data
  24. Call AddBottomEntries(text25.Value, text5.Value, txtCustomEntry.Value)
  25.  
  26. End Sub
  27.  
  28. Private Sub UserForm_Initialize()
  29. InitializeForm
  30. ' Sets object defaults
  31. cb_Extension = False
  32. txt_Extension.Visible = False
  33. label_ExtendTime.Visible = False
  34. CompassFrame.Visible = False
  35. FireExtFrame.Visible = False
  36. FirstAidFrame.Visible = False
  37. WBFrame.Visible = False
  38. DualsFrame.Visible = False
  39. opt_Maint.Value = True
  40.  
  41. End Sub
  42.  
  43. Private Sub cb_Extension_Click()
  44. label_ExtendTime.Visible = cb_Extension.Value
  45. txt_Extension.Visible = cb_Extension.Value
  46. End Sub
  47.  
  48. Private Sub cb_FireExt_Click()
  49. FireExtFrame.Visible = cb_FireExt.Value
  50. End Sub
  51.  
  52. Private Sub cb_FirstAidKit_Click()
  53. FirstAidFrame.Visible = cb_FirstAidKit.Value
  54. End Sub
  55.  
  56. Private Sub cb_WB_Click()
  57. WBFrame.Visible = cb_WB.Value
  58. End Sub
  59.  
  60.  
  61. Private Sub InitializeForm()
  62. ' Arrays for combo box values
  63. Dim inspectionTypes As Variant
  64. inspectionTypes = Array("75 Hour inspection", "150 Hour inspection", "300 Hour inspection", _
  65. "1500 Hour inspection", "6000 Hour inspection", "100 Hour inspection", _
  66. "500 Hour inspection", "1000 Hour inspection")
  67.  
  68. Dim dualNames As Variant
  69. dualNames = Array("Dave J", "Dave M", "Steve", "Peter", "Pierre", "Marc")
  70.  
  71. Dim bottomAddsItems As Variant
  72. bottomAddsItems = Array("Engine leak check required after first flight", _
  73. "Aircraft released conditional to satisfactory 1500hr inspection / engine break in test flight ", _
  74. "Aircraft released conditional to satisfactory test flight due to fuel system adjustments carried out", _
  75. "Propeller torque check required after first flight and after first 25hrs to not exceed X hrs TTAF", _
  76. "Initial 5hr alternator belt tension check required to not exceed Y hrs ", _
  77. "Initial 25hr engine operation inspection required to not exceed X hrs TTAF", _
  78. "25 hour propeller retorque due at X hrs TTAF")
  79.  
  80. ' Populate BottomAddsList
  81. Dim item As Variant
  82. For Each item In bottomAddsItems
  83. Me.BottomAddsList.AddItem item
  84. Next item
  85. Me.BottomAddsList.MultiSelect = fmMultiSelectMulti
  86.  
  87. ' Populate inspection type combo box
  88. For Each item In inspectionTypes
  89. Me.cmb_InspectionType.AddItem item
  90. Next item
  91. Me.cmb_InspectionType.Value = "75 Hour inspection" ' Default value
  92.  
  93. ' Populate duals combo box
  94. For Each item In dualNames
  95. Me.cmb_Duals.AddItem item
  96. Next item
  97.  
  98. ' Populate recurring defect combo box
  99. With Me.cmb_RecurDef
  100. .AddItem "No"
  101. .AddItem "Yes"
  102. .Value = "No" ' Default value
  103. End With
  104. End Sub
  105.  
  106. Public Function BuildWPFull() As String
  107. Dim regPart As String
  108. regPart = Right(txt_Reg.text, 3) ' Get the last 3 characters of txt_Reg.text **Tested**
  109. BuildWPFull = UCase(regPart & "-" & txt_WPYear.text & "-" & txt_WPNumber.text)
  110. End Function
  111.  
  112. Private Function GetFullName(shortName As String) As String
  113. ' Map short names to full names
  114. Select Case shortName
  115. Case "Dave J"
  116. GetFullName = "David Jones 450901"
  117. Case "Dave M"
  118. GetFullName = "Bob Smith"
  119. Case "Steve"
  120. GetFullName = "Sue Ellen"
  121. Case "Peter"
  122. GetFullName = "Sue Ellen"
  123. Case "Pierre"
  124. GetFullName = "Sue Ellen"
  125. Case "Marc"
  126. GetFullName = "xxx"
  127. Case Else
  128. GetFullName = shortName ' Return original if no mapping exists
  129. End Select
  130. End Function
  131.  
  132.  
  133.  
  134.  
  135. Private Sub cb_Compass_Click()
  136. Dim doc As Document
  137. Dim cc As ContentControl
  138. Dim tbl As Table
  139. Dim ccInTable As ContentControl
  140. Dim tblRange As Range
  141.  
  142. ' Check the state of the checkbox and hide/show CompassFrame accordingly
  143. CompassFrame.Visible = cb_Compass.Value
  144.  
  145. ' Reference the active document
  146. Set doc = ActiveDocument
  147.  
  148. ' Find the table content control by its title or tag
  149. For Each cc In doc.ContentControls
  150. If cc.Title = "box_Compass" Or cc.tag = "box_Compass" Then
  151. If cc.Range.Tables.Count > 0 Then
  152. Set tbl = cc.Range.Tables(1) ' Get the table within the content control
  153. Set tblRange = tbl.Range
  154. Exit For
  155. End If
  156. End If
  157. Next cc
  158.  
  159. ' If the table is found, toggle its visibility based on the checkbox value
  160. If Not tbl Is Nothing Then
  161. If cb_Compass.Value Then
  162. tblRange.Font.Hidden = False ' Show the table
  163. ' Show all content controls within the table
  164. For Each ccInTable In doc.ContentControls
  165. If ccInTable.Range.Start >= tblRange.Start And ccInTable.Range.End <= tblRange.End Then
  166. ccInTable.Range.Font.Hidden = False
  167. End If
  168. Next ccInTable
  169. Else
  170. tblRange.Font.Hidden = True ' Hide the table
  171. ' Hide all content controls within the table
  172. For Each ccInTable In doc.ContentControls
  173. If ccInTable.Range.Start >= tblRange.Start And ccInTable.Range.End <= tblRange.End Then
  174. ccInTable.Range.Font.Hidden = True
  175. End If
  176. Next ccInTable
  177. End If
  178. End If
  179. End Sub
  180.  
  181. Private Sub SetVisibility(ByVal inspectionTypeVisible As Boolean, _
  182. ByVal snagEntryVisible As Boolean, _
  183. ByVal extensionVisible As Boolean)
  184. cmb_InspectionType.Visible = inspectionTypeVisible
  185. label_InspectionType.Visible = inspectionTypeVisible
  186. cb_Extension.Visible = extensionVisible
  187. txt_SnagEntry.Visible = snagEntryVisible
  188. label_SnagEntry.Visible = snagEntryVisible
  189. label_SnagRectification.Visible = snagEntryVisible
  190. txt_SnagRect.Visible = snagEntryVisible
  191. End Sub
  192.  
  193. Private Sub opt_Snag_Click()
  194.  
  195. SetVisibility False, True, False
  196. UpdateContentBoxesVisibility
  197. End Sub
  198.  
  199. Private Sub opt_Maint_Click()
  200. SetVisibility True, False, True
  201. UpdateContentBoxesVisibility
  202. End Sub
  203.  
  204. Private Sub opt_Both_Click()
  205. SetVisibility True, True, True
  206. UpdateContentBoxesVisibility
  207. End Sub
  208. Private Sub UpdateContentBoxesVisibility()
  209. Dim ccSnag As ContentControl
  210. Dim ccMaint As ContentControl
  211.  
  212. ' Get reference to content controls
  213. Set ccSnag = ActiveDocument.SelectContentControlsByTag("box_Snag")(1)
  214. Set ccMaint = ActiveDocument.SelectContentControlsByTag("box_Maint")(1)
  215.  
  216. ' Set visibility based on the selected option button
  217. If opt_Snag.Value Then
  218. ccSnag.Range.Font.Hidden = False
  219. ccMaint.Range.Font.Hidden = True
  220. ElseIf opt_Maint.Value Then
  221. ccSnag.Range.Font.Hidden = True
  222. ccMaint.Range.Font.Hidden = False
  223. ElseIf opt_Both.Value Then
  224. ccSnag.Range.Font.Hidden = False
  225. ccMaint.Range.Font.Hidden = False
  226. End If
  227. End Sub
  228.  
  229.  
  230. Private Sub btnSubmit_Click()
  231.  
  232. 'Make WPFull string available
  233. Dim doc As Document
  234. Dim WPFull As String
  235. WPFull = BuildWPFull()
  236.  
  237.  
  238. ' Set the active document
  239. Set doc = Application.ActiveDocument
  240.  
  241. ' After handling the button click, update the content controls **Tested
  242. Call UpdateContentControls
  243.  
  244. ' If Snag or Both is checked, enter Snag Entry and Rectification **Tested
  245. If opt_Snag.Value = True Or opt_Both.Value = True Then
  246. ' Prepend "Snag: " to the text and call the module procedure
  247. UpdateSnagContent "Snag: " & txt_SnagEntry.Value, "text_SnagTitle"
  248. UpdateSnagContent txt_SnagRect.Value, "text_SnagRectify"
  249.  
  250. End If
  251.  
  252.  
  253. ' Updates inspection type with or without extension in Maintenance header **Tested
  254. UpdateMaintHeader doc, cmb_InspectionType.Value, cb_Extension.Value
  255.  
  256. ' Adds extension entry and hours
  257. Dim ExtensionChecked As Boolean
  258. Dim AddWorkChecked As Boolean
  259. Dim inspectionType As String
  260.  
  261. ' Capture values from the userform for extension
  262. ExtensionChecked = cb_Extension.Value
  263. AddWorkChecked = cb_Addwork.Value
  264. inspectionType = cmb_InspectionType.Value
  265.  
  266. ' Call the UpdateMaintenanceBody subroutine
  267. UpdateMaintenanceBody ExtensionChecked, inspectionType, AddWorkChecked
  268.  
  269.  
  270.  
  271. ' Updates compass headings, only if compass checkmark is ticked
  272. If cb_Compass.Value = True Then
  273. ' Call the UpdateCompassHeadings subroutine to update the compass headings in the document
  274. UpdateCompassHeadings
  275. End If
  276.  
  277.  
  278. ' Add bottom entries with user-specific data
  279. Call AddBottomEntries(text25.Value, text5.Value, txtCustomEntry.Value)
  280.  
  281. End Sub
  282.  
  283.  
  284. Private Sub UpdateContentControls()
  285. Dim cc As ContentControl
  286. Dim WPFull As String
  287. ' Build the WPFull string
  288. WPFull = BuildWPFull()
  289.  
  290. ' Loop through all content controls in the document
  291. For Each cc In ActiveDocument.ContentControls
  292. Select Case cc.tag
  293. Case "Text_Reg"
  294. cc.Range.text = UCase(txt_Reg.text) ' Convert to uppercase before setting the value
  295. Case "Text_Date"
  296. cc.Range.text = txt_Date.text
  297. Case "Doc_RecDef"
  298. cc.Range.text = cmb_RecurDef.Value
  299. Case "Text_TTAF"
  300. cc.Range.text = txt_TTAF.text & " Hrs"
  301. Case "Text_WPFull"
  302. cc.Range.text = WPFull ' Update the WPFull content control
  303. cc.Range.Font.Bold = True ' Apply bold formatting
  304. Case "Doc_InspectionType"
  305. cc.Range.text = cmb_InspectionType.Value
  306. End Select
  307. Next cc
  308. End Sub
  309.  
  310. Private Sub cb_Duals_Click()
  311. ' Get reference to the document
  312. Dim doc As Document
  313. Set doc = ActiveDocument
  314.  
  315. DualsFrame.Visible = cb_Duals.Value
  316.  
  317. ' Get reference to the content control box
  318. Dim boxDuals As ContentControl
  319. On Error Resume Next
  320. Set boxDuals = doc.SelectContentControlsByTitle("box_Duals").item(1)
  321. On Error GoTo 0
  322.  
  323. ' Check if the content control exists
  324. If boxDuals Is Nothing Then
  325. MsgBox "Content control 'box_Duals' not found!", vbExclamation
  326. Exit Sub
  327. End If
  328.  
  329. ' Show/hide the box based on checkbox state
  330. If cb_Duals.Value Then
  331. boxDuals.Range.Font.Hidden = False
  332. Else
  333. boxDuals.Range.Font.Hidden = True
  334. End If
  335. End Sub
  336.  
  337.  
  338.  
  339.  
  340. Private Sub AddBottomEntries(userNumberX As String, userNumberY As String, customEntry As String)
  341. Dim i As Integer
  342. Dim selectedItem As String
  343.  
  344. For i = 0 To BottomAddsList.ListCount - 1
  345. If BottomAddsList.Selected(i) Then
  346. selectedItem = Replace(Replace(BottomAddsList.List(i), "X", userNumberX), "Y", userNumberY)
  347. AddTextToDocument "*** " & Trim(selectedItem) & " ***", True
  348. End If
  349. Next i
  350.  
  351. ' If there is a custom entry, add it to the document
  352. If customEntry <> "" Then
  353. AddTextToDocument "*** " & Trim(customEntry) & " ***", True
  354. End If
  355. End Sub
  356.  
  357. Private Sub UpdateCompassHeadings()
  358. Dim cc As ContentControl
  359. Dim userFormControl As MSForms.Control
  360. Dim contentControlName As String
  361. Dim userFormTextboxName As String
  362.  
  363. ' Loop through all content controls in the document
  364. For Each cc In ActiveDocument.ContentControls
  365. ' Check if the content control's tag matches one of the predefined values
  366. Select Case cc.tag
  367. Case "text_North", "text_30", "text_60", "text_East", "text_120", _
  368. "text_150", "text_South", "text_210", "text_240", _
  369. "text_West", "text_300", "text_330"
  370.  
  371. ' Determine the corresponding textbox on the userform based on the tag
  372. userFormTextboxName = Replace(cc.tag, "text_", "txt")
  373.  
  374. ' Check if the textbox exists on the userform
  375. On Error Resume Next
  376. Set userFormControl = Me.Controls(userFormTextboxName)
  377. On Error GoTo 0
  378.  
  379. ' If the textbox exists, update the content control with its value
  380. If Not userFormControl Is Nothing Then
  381. cc.Range.text = UCase(userFormControl.Value) ' Convert to uppercase
  382. End If
  383. End Select
  384. Next cc
  385. End Sub
  386.  
  387. Private Sub AddTextToDocument(text As String, alignCenter As Boolean)
  388. Selection.EndKey Unit:=wdStory
  389. Selection.TypeParagraph
  390. If alignCenter Then
  391. Selection.ParagraphFormat.Alignment = wdAlignParagraphCenter
  392. End If
  393. Selection.TypeText text:=text & vbCrLf
  394. End Sub
  395.  
  396.  
  397. Dual Userform
  398.  
  399. Option Explicit
  400. Private counter As Integer
  401.  
  402. Private Sub UserForm_Initialize()
  403. counter = 1
  404. End Sub
  405.  
  406. Private Sub cb_AddRow_Click()
  407. ' Increment counter for new textbox naming
  408. counter = counter + 1
  409.  
  410. ' Get reference to last textboxes for positioning
  411. Dim lastControlBox As MSForms.TextBox
  412. Dim lastTCBox As MSForms.TextBox
  413. Set lastControlBox = Me.Controls("txt_DualControl" & (counter - 1))
  414. Set lastTCBox = Me.Controls("txt_DualTC" & (counter - 1))
  415.  
  416. ' Create new textboxes
  417. Dim newControlBox As MSForms.TextBox
  418. Dim newTCBox As MSForms.TextBox
  419.  
  420. ' Create and set properties for Control description textbox
  421. Set newControlBox = Me.Controls.Add("Forms.TextBox.1", "txt_DualControl" & counter)
  422. With newControlBox
  423. .Left = lastControlBox.Left
  424. .Top = lastControlBox.Top + lastControlBox.Height + 5
  425. .Width = lastControlBox.Width
  426. .Height = lastControlBox.Height
  427. End With
  428.  
  429. ' Create and set properties for Task Card textbox
  430. Set newTCBox = Me.Controls.Add("Forms.TextBox.1", "txt_DualTC" & counter)
  431. With newTCBox
  432. .Left = lastTCBox.Left
  433. .Top = lastTCBox.Top + lastTCBox.Height + 5
  434. .Width = lastTCBox.Width
  435. .Height = lastTCBox.Height
  436. End With
  437.  
  438. ' Add new row to the table in the document
  439. Dim cc As ContentControl
  440. Dim tbl As Table
  441.  
  442. ' Find the content control by its tag
  443. For Each cc In ActiveDocument.ContentControls
  444. If cc.tag = "box_DualTCs" Then
  445. Set tbl = cc.Range.Tables(1)
  446. ' Add new row to the table
  447. tbl.Rows.Add
  448. Exit For
  449. End If
  450. Next cc
  451. End Sub
  452.  
  453. Public Sub TransferDataToTable()
  454. Dim cc As ContentControl
  455. Dim tbl As Table
  456. Dim i As Integer
  457.  
  458. ' Find the content control and its table
  459. For Each cc In ActiveDocument.ContentControls
  460. If cc.tag = "box_DualTCs" Then
  461. Set tbl = cc.Range.Tables(1)
  462.  
  463. ' Loop through all textbox pairs and add their data to the table
  464. For i = 1 To counter
  465. Dim controlBox As MSForms.TextBox
  466. Dim tcBox As MSForms.TextBox
  467.  
  468. Set controlBox = Me.Controls("txt_DualControl" & i)
  469. Set tcBox = Me.Controls("txt_DualTC" & i)
  470.  
  471. ' Add data to table
  472. tbl.Rows(i).Cells(1).Range.text = controlBox.text
  473. tbl.Rows(i).Cells(2).Range.text = tcBox.text
  474. Next i
  475.  
  476. Exit For
  477. End If
  478. Next cc
  479. End Sub
  480.  
  481.  
  482. Private Sub cb_AddData_Click()
  483.  
  484. 'Submits the data into the document
  485. TransferDataToTable
  486.  
  487. 'HIDES the Duals form
  488. Me.Hide
  489.  
  490. End Sub
  491.  
  492.  
  493. Formatting Module
  494.  
  495. Public Sub ApplyTextFormatToText(ByRef targetRange As Range, _
  496. ByVal textToFormat As String, _
  497. Optional ByVal FontName As String = "Times New Roman", _
  498. Optional ByVal FontSize As Single = 8, _
  499. Optional ByVal Bold As Boolean = True, _
  500. Optional ByVal Italic As Boolean = False, _
  501. Optional ByVal Underline As Boolean = False, _
  502. Optional ByVal FontColor As Long = wdColorAutomatic, _
  503. Optional ByVal Alignment As WdParagraphAlignment = wdAlignParagraphLeft)
  504. ' Check if targetRange is valid
  505. If targetRange Is Nothing Or targetRange.text = "" Then Exit Sub
  506.  
  507. ' Find and format the text
  508. With targetRange.Find
  509. .text = textToFormat
  510. .MatchCase = False
  511. .MatchWholeWord = False
  512. If .Execute Then
  513. With .Parent
  514. ' Apply font properties
  515. With .Font
  516. .Name = FontName
  517. .Size = FontSize
  518. .Bold = Bold
  519. .Italic = Italic
  520. .Underline = IIf(Underline, wdUnderlineSingle, wdUnderlineNone)
  521. .Color = FontColor
  522. End With
  523. ' Apply paragraph alignment
  524. .ParagraphFormat.Alignment = Alignment
  525. End With
  526. End If
  527. End With
  528. End Sub
  529.  
  530.  
  531. ' Helper function to add section content with proper formatting
  532. Public Sub AddFormattedSection(ByRef content As String, ByVal mainText As String, ByRef subItems() As String, ByVal WPFull As String)
  533. content = content & vbCrLf & mainText & " REF WP " & WPFull
  534. Dim subItem As Variant
  535. For Each subItem In subItems
  536. If subItem <> "" Then
  537. content = content & vbCrLf & subItem
  538. End If
  539. Next subItem
  540. End Sub
  541.  
  542. Updating content module
  543.  
  544. Public Sub ApplyTextFormatToText(ByRef targetRange As Range, _
  545. ByVal textToFormat As String, _
  546. Optional ByVal FontName As String = "Times New Roman", _
  547. Optional ByVal FontSize As Single = 8, _
  548. Optional ByVal Bold As Boolean = True, _
  549. Optional ByVal Italic As Boolean = False, _
  550. Optional ByVal Underline As Boolean = False, _
  551. Optional ByVal FontColor As Long = wdColorAutomatic, _
  552. Optional ByVal Alignment As WdParagraphAlignment = wdAlignParagraphLeft)
  553. ' Check if targetRange is valid
  554. If targetRange Is Nothing Or targetRange.text = "" Then Exit Sub
  555.  
  556. ' Find and format the text
  557. With targetRange.Find
  558. .text = textToFormat
  559. .MatchCase = False
  560. .MatchWholeWord = False
  561. If .Execute Then
  562. With .Parent
  563. ' Apply font properties
  564. With .Font
  565. .Name = FontName
  566. .Size = FontSize
  567. .Bold = Bold
  568. .Italic = Italic
  569. .Underline = IIf(Underline, wdUnderlineSingle, wdUnderlineNone)
  570. .Color = FontColor
  571. End With
  572. ' Apply paragraph alignment
  573. .ParagraphFormat.Alignment = Alignment
  574. End With
  575. End If
  576. End With
  577. End Sub
  578.  
  579.  
  580. ' Helper function to add section content with proper formatting
  581. Public Sub AddFormattedSection(ByRef content As String, ByVal mainText As String, ByRef subItems() As String, ByVal WPFull As String)
  582. content = content & vbCrLf & mainText & " REF WP " & WPFull
  583. Dim subItem As Variant
  584. For Each subItem In subItems
  585. If subItem <> "" Then
  586. content = content & vbCrLf & subItem
  587. End If
  588. Next subItem
  589. End Sub
  590.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement