Advertisement
JustJoe21

Untitled

Dec 17th, 2024
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VisualBasic 8.71 KB | Source Code | 0 0
  1.  
  2.  
  3.  
  4. Private Sub cmb_Test_Click()
  5.  
  6. End Sub
  7.  
  8. Private Sub UserForm_Initialize()
  9.     InitializeForm
  10.       ' Set opt_Maint as the default selected option
  11.    opt_Maint.Value = True
  12. End Sub
  13. Private Function BuildWPFull() As String
  14.     Dim regPart As String
  15.     regPart = Right(txt_Reg.text, 3) ' Get the last 3 characters of txt_Reg.text
  16.    BuildWPFull = UCase(regPart & "-" & txt_WPYear.text & "-" & txt_WPNumber.text)
  17. End Function
  18.  
  19. Private Sub opt_Maint_Click()
  20.     Dim ccMaintenance As contentControl
  21.     Dim Doc_Header As contentControl
  22.     Dim line1 As contentControl
  23.     Dim maintEntry As String
  24.     Dim maintDetail As String
  25.       Dim WPFull As String
  26.     ' Build the WPFull string
  27.    WPFull = BuildWPFull()
  28.    
  29.     ' Set visibility for Maintenance
  30.    With Me
  31.         cmb_InspectionType.Visible = True
  32.         label_InspectionType.Visible = True
  33.         txt_SnagEntry.Visible = False
  34.         label_SnagEntry.Visible = False
  35.         label_SnagRectification.Visible = False
  36.         txt_SnagRect.Visible = False
  37.         cb_Extension.Visible = True
  38.     End With
  39.  
  40.     ' Determine maintenance detail based on cb_Extension
  41.    If cb_Extension.Value = True Then
  42.         maintDetail = cmb_InspectionType.Value & " extension carried out" & " REF " & WPFull
  43.     Else
  44.         maintDetail = cmb_InspectionType.Value & " carried out"
  45.     End If
  46.  
  47.     ' Set the document context for Maintenance type
  48.    Set ccMaintenance = ActiveDocument.SelectContentControlsByTag("Doc_MaintType").Item(1)
  49.     If Not ccMaintenance Is Nothing Then
  50.         ccMaintenance.Range.text = "Maintenance"
  51.     Else
  52.         MsgBox "Content control 'Doc_MaintType' not found!", vbExclamation
  53.     End If
  54.  
  55.     ' Update the Header with Maintenance entry
  56.    Set Doc_Header = ActiveDocument.SelectContentControlsByTag("Doc_Header").Item(1)
  57.     If Not Doc_Header Is Nothing Then
  58.         If cb_Extension.Value = True Then
  59.             Doc_Header.Range.text = Trim(cmb_InspectionType.Value) & " extension to be carried out"
  60.         Else
  61.             Doc_Header.Range.text = Trim(cmb_InspectionType.Value) & " to be carried out"
  62.         End If
  63.     Else
  64.         MsgBox "Content control 'Doc_Header' not found!", vbExclamation
  65.     End If
  66.  
  67.     ' Update line1 with Maintenance details
  68.    Set line1 = ActiveDocument.SelectContentControlsByTag("Text_Line1").Item(1)
  69.     If Not line1 Is Nothing Then
  70.         line1.Range.text = maintDetail
  71.     Else
  72.         MsgBox "Content control 'Text_Line1' not found!", vbExclamation
  73.     End If
  74. End Sub
  75.  
  76.  
  77. Private Sub opt_Snag_Click()
  78.     Dim ccMaintenance As contentControl
  79.     Dim snagEntry As String
  80.     Dim snagRect As String
  81.     Dim docHeader As contentControl
  82.     Dim line1 As contentControl
  83.  
  84.     ' Set visibility for Snag
  85.    With Me
  86.         cmb_InspectionType.Visible = False
  87.         label_InspectionType.Visible = False
  88.         txt_SnagEntry.Visible = True
  89.         label_SnagEntry.Visible = True
  90.         label_SnagRectification.Visible = True
  91.         txt_SnagRect.Visible = True
  92.         cb_Extension.Visible = False
  93.     End With
  94.  
  95.     ' Retrieve Snag data
  96.    snagEntry = Trim(txt_SnagEntry.Value)
  97.     snagRect = Trim(txt_SnagRect.Value)
  98.  
  99.     ' Update 'Doc_MaintType' to "Snag"
  100.    On Error Resume Next
  101.     Set ccMaintenance = ActiveDocument.SelectContentControlsByTag("Doc_MaintType").Item(1)
  102.     On Error GoTo 0
  103.     If Not ccMaintenance Is Nothing Then
  104.         ccMaintenance.Range.text = "Snag"
  105.     Else
  106.         MsgBox "Content control 'Doc_MaintType' not found!", vbExclamation
  107.     End If
  108.  
  109.     ' Update Doc_Header with Snag entry
  110.    On Error Resume Next
  111.     Set docHeader = ActiveDocument.SelectContentControlsByTag("Doc_Header").Item(1)
  112.     On Error GoTo 0
  113.     If Not docHeader Is Nothing Then
  114.         docHeader.Range.text = snagEntry
  115.     Else
  116.         MsgBox "Content control 'Doc_Header' not found!", vbExclamation
  117.     End If
  118.  
  119.     ' Update line1 with Snag rectification
  120.    On Error Resume Next
  121.     Set line1 = ActiveDocument.SelectContentControlsByTag("Text_Line1").Item(1)
  122.     On Error GoTo 0
  123.     If Not line1 Is Nothing Then
  124.         line1.Range.text = snagRect
  125.     Else
  126.         MsgBox "Content control 'Text_Line1' not found!", vbExclamation
  127.     End If
  128. End Sub
  129.  
  130. Private Sub btnSubmit_Click()
  131.     If opt_Maint.Value Then
  132.         Call opt_Maint_Click
  133.     ElseIf opt_Snag.Value Then
  134.         Call opt_Snag_Click
  135.     End If
  136.    
  137.     ' After handling the button click, update the content controls
  138.    Call UpdateContentControls
  139.    
  140.     ' Add bottom entries with user-specific data
  141.    Dim userNumberX As String
  142.     Dim userNumberY As String
  143.     Dim customEntry As String
  144.    
  145.     ' Get the numbers from the TextBoxes
  146.    userNumberX = text25.Value
  147.     userNumberY = text5.Value
  148.     customEntry = txtCustomEntry.Value
  149.    
  150.     ' Add entries to the bottom of the document
  151.    Call AddBottomEntries(userNumberX, userNumberY, customEntry)
  152. End Sub
  153.  
  154. Private Sub UpdateContentControls()
  155.     Dim cc As contentControl
  156.     Dim WPFull As String
  157.     ' Build the WPFull string
  158.    WPFull = BuildWPFull()
  159.    
  160.     ' Loop through all content controls in the document
  161.    For Each cc In ActiveDocument.ContentControls
  162.         Select Case cc.Tag
  163.             Case "Text_Reg"
  164.                 cc.Range.text = UCase(txt_Reg.text) ' Convert to uppercase before setting the value
  165.            Case "Text_Date"
  166.                 cc.Range.text = txt_Date.text
  167.             Case "Doc_RecDef"
  168.                 cc.Range.text = cmb_RecurDef.Value
  169.             Case "Text_TTAF"
  170.                 cc.Range.text = txt_TTAF.text
  171.             Case "Text_WPFull"
  172.                 cc.Range.text = WPFull ' Update the WPFull content control
  173.                cc.Range.Font.Bold = True ' Apply bold formatting
  174.            Case "Doc_InspectionType"
  175.                 cc.Range.text = cmb_InspectionType.Value
  176.                
  177.                
  178.         End Select
  179.     Next cc
  180. End Sub
  181.  
  182. Private Sub InitializeForm()
  183.     ' Populate the BottomAddsList with preset options
  184.    BottomAddsList.AddItem "Engine leak check required after first flight"
  185.     BottomAddsList.AddItem "Aircraft released conditional to satisfactory 1500hr inspection / engine break in test flight "
  186.     BottomAddsList.AddItem "Aircraft released conditional to satisfactory test flight due to fuel system adjustments carried out"
  187.     BottomAddsList.AddItem "Propeller torque check required after first flight and after first 25hrs to not exceed X hrs TTAF"
  188.     BottomAddsList.AddItem "Initial 5hr alternator belt tension check required to not exceed Y hrs "
  189.     BottomAddsList.AddItem "Initial 25hr engine operation inspection required to not exceed X hrs TTAF"
  190.     BottomAddsList.AddItem "25 hour propeller retorque due at X hrs TTAF"
  191.    
  192.     ' Allow multiple selections
  193.    BottomAddsList.MultiSelect = fmMultiSelectMulti
  194.  
  195.   ' Populate cmb_InspectionType
  196.    With Me.cmb_InspectionType
  197.         .AddItem "Maintenance"
  198.         .AddItem "Snag"
  199.     End With
  200.  
  201.     ' Optional: Set a default value
  202.    Me.cmb_InspectionType.Value = "Maintenance"
  203.  
  204.     ' Populate cmb_InspectionType
  205.    With Me.cmb_InspectionType
  206.      cmb_InspectionType.Clear
  207.         .AddItem "75 Hour inspection"
  208.         .AddItem "150 Hour inspection"
  209.         .AddItem "300 Hour inspection"
  210.         .AddItem "1500 Hour inspection"
  211.         .AddItem "6000 Hour inspection"
  212.         .AddItem "100 Hour inspection"
  213.         .AddItem "500 Hour inspection"
  214.         .AddItem "1000 Hour inspection"
  215.     End With
  216.  
  217.     ' Optional: Set a default value
  218.    Me.cmb_InspectionType.Value = "300 Hour"
  219.    
  220.     ' Populate cmb_RecurDef
  221.    With Me.cmb_RecurDef
  222.         .AddItem "No"
  223.         .AddItem "Yes"
  224.     End With
  225.  
  226.     ' Optional: Set a default value
  227.    Me.cmb_RecurDef.Value = "No"
  228. End Sub
  229.  
  230. Private Sub AddBottomEntries(userNumberX As String, userNumberY As String, customEntry As String)
  231.     Dim i As Integer
  232.     Dim selectedItem As String
  233.  
  234.     For i = 0 To BottomAddsList.ListCount - 1
  235.         If BottomAddsList.Selected(i) Then
  236.             selectedItem = Replace(Replace(BottomAddsList.List(i), "X", userNumberX), "Y", userNumberY)
  237.             AddTextToDocument "*** " & Trim(selectedItem) & " ***", True
  238.         End If
  239.     Next i
  240.    
  241.     ' If there is a custom entry, add it to the document
  242.    If customEntry <> "" Then
  243.         AddTextToDocument "*** " & Trim(customEntry) & " ***", True
  244.     End If
  245. End Sub
  246.  
  247.  
  248. Private Sub btnCompass_Click()
  249.     AddNumberedLineToDocument "Compass calibration needed"
  250. End Sub
  251.  
  252.  
  253.  
  254. Private Sub AddTextToDocument(text As String, alignCenter As Boolean)
  255.     Selection.EndKey Unit:=wdStory
  256.     Selection.TypeParagraph
  257.     If alignCenter Then
  258.         Selection.ParagraphFormat.Alignment = wdAlignParagraphCenter
  259.     End If
  260.     Selection.TypeText text:=text & vbCrLf
  261. End Sub
  262.  
Tags: vba Userforms
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement