Please help every time i run this function Excel 2016 crashes and excel restarts no errors given before the crash.
'Function the make a new incident report
Public Sub incidentDoc(lastSheet As String)
Call ApplicationActions(True) 'Disable all Excel Automatic Actions
Dim fileDir As String
fileDir = FileSelection.BrowseFolders 'Save file Directory
If (fileDir = vbNullString) Then 'If user cancled the file dialog
Exit Sub 'Exit funtion
End If
Dim jobNumber As String
Dim RigName As String
Dim clientName As String
Dim eventDate As String
Dim msWord As Word.Application
Dim msWordDoc As Word.Document
Dim newLine As String
newLine = Chr(11)
Set msWord = CreateObject("Word.Application")
msWord.Visible = False
Set msWordDoc = msWord.Documents.Add
'msWordDoc.Name = lastSheet
Dim incidentType As String
Dim rowIdx As Integer
For rowIdx = 50 To 61
If (CBool(Sheets(lastSheet).Range("D" & rowIdx).Value)) Then
If (incidentType <> vbNullString) Then incidentType = incidentType & " / "
incidentType = incidentType & UCase(Sheets(lastSheet).Range("A" & rowIdx).Value)
End If
Next rowIdx
If (incidentType = vbNullString) Then incidentType = "TBD"
Dim serialList As String
For rowIdx = 10 To 11
If (Sheets(lastSheet).Range("B" & rowIdx).Value <> vbNullString) Then
If (serialList <> vbNullString) Then serialList = serialList & " / "
serialList = serialList & UCase(Sheets(lastSheet).Range("B" & rowIdx).Value)
'If (Sheets(lastSheet).Range("D" & rowIdx).Value <> vbNullString) Then
'serialList = serialList & " (" & Sheets(lastSheet).Range("D" & rowIdx).Value & ")"
'Else
'serialList = serialList & " (N/A)"
'End If
End If
Next rowIdx
For rowIdx = 9 To 11
If (Sheets(lastSheet).Range("F" & rowIdx).Value <> vbNullString) Then
If (serialList <> vbNullString) Then serialList = serialList & " / "
serialList = serialList & UCase(Sheets(lastSheet).Range("F" & rowIdx).Value)
'If (Sheets(lastSheet).Range("H" & rowIdx).Value <> vbNullString) Then
'serialList = serialList & " (" & Sheets(lastSheet).Range("H" & rowIdx).Value & ")"
'Else
'serialList = serialList & " (N/A)"
'End If
End If
Next rowIdx
If (serialList = vbNullString) Then serialList = "N/A"
Dim fieldPersonnel As String
For rowIdx = 5 To 6
If (Sheets(lastSheet).Range("F" & rowIdx).Value <> vbNullString) Then
If (fieldPersonnel <> vbNullString) Then fieldPersonnel = fieldPersonnel & " / "
fieldPersonnel = fieldPersonnel & Sheets(lastSheet).Range("F" & rowIdx).Value
End If
Next rowIdx
If (fieldPersonnel = vbNullString) Then fieldPersonnel = "N/A"
With msWordDoc
.Content.InsertAfter "Client: " & Sheets(lastSheet).Range("B5").Value & newLine
If (Sheets(lastSheet).Range("B5").Value <> vbNullString) Then clientName = Replace(CleanFileName(Sheets(lastSheet).Range("B5").Value), " ", "_")
.Content.InsertAfter "Well Name: " & Sheets(lastSheet).Range("B6").Value & newLine
.Content.InsertAfter "Job #: " & Sheets(lastSheet).Range("F4").Value & newLine
If (Sheets(lastSheet).Range("F4").Value <> vbNullString) Then jobNumber = Replace(CleanFileName(Sheets(lastSheet).Range("F4").Value), " ", "_")
.Content.InsertAfter "Rig Name & Number: " & Sheets(lastSheet).Range("B7").Value & newLine
If (Sheets(lastSheet).Range("B7").Value <> vbNullString) Then RigName = Replace(CleanFileName(Sheets(lastSheet).Range("B7").Value), " ", "_")
.Content.InsertAfter "County, State: " & Sheets(lastSheet).Range("B8").Value
.Content.InsertParagraphAfter
.Content.InsertAfter "Incident Date: " & Format(Sheets(lastSheet).Range("B4").Value, "Short Date") & newLine
If (Sheets(lastSheet).Range("B4").Value <> vbNullString) Then eventDate = CStr(Format(Sheets(lastSheet).Range("B4").Value, "MM_DD_YYYY"))
.Content.InsertAfter "Incident Time: " & Format(Sheets(lastSheet).Range("D4").Value, "hh:mm") & newLine
.Content.InsertAfter "Incident Type: " & incidentType
.Content.InsertParagraphAfter
.Content.InsertAfter "Tool Serial Number: " & serialList & newLine
.Content.InsertAfter "MWD monel OD/ID: " & Sheets(lastSheet).Range("F42").Value & newLine
.Content.InsertAfter "Fin Gauge: " & Sheets(lastSheet).Range("D48").Value & newLine
.Content.InsertAfter "Temp.: " & Sheets(lastSheet).Range("B47").Value & newLine
.Content.InsertAfter "Lockdown Type: " & Sheets(lastSheet).Range("B48").Value & newLine
.Content.InsertAfter "Axial Isolator SN: " & Sheets(lastSheet).Range("B49").Value & newLine
.Content.InsertAfter "Axial Isolator Tupe: " & Sheets(lastSheet).Range("D49").Value & newLine
.Content.InsertAfter "Agitator Placement above MWD: " & Sheets(lastSheet).Range("F48").Value
If (UCase(Sheets(lastSheet).Range("F48").Value) = "YES") Then
.Content.InsertAfter newLine & "Agitator Brand: " & Sheets(lastSheet).Range("H48").Value & newLine
.Content.InsertAfter "Agitator Distance To MWD: " & Sheets(lastSheet).Range("F49").Value
End If
.Content.InsertParagraphAfter
.Content.InsertAfter "MWD Engineers: " & fieldPersonnel & newLine
If (Sheets(lastSheet).Range("F8").Value <> vbNullString) Then
.Content.InsertAfter "Downtime: " & Sheets(lastSheet).Range("F8").Value & newLine
Else
Sheets(lastSheet).Range("F8").Value = "TBD"
End If
.Content.InsertAfter "Reporter: " & newLine
.Content.InsertAfter "MWD Coordinator: " & Sheets(lastSheet).Range("B9").Value
If (Sheets(lastSheet).Range("A21").Value <> vbNullString) Then
.Content.InsertParagraphAfter
.Content.InsertAfter "Description of issue: " & Sheets(lastSheet).Range("A21").Value
End If
If (Sheets(lastSheet).Range("A27").Value <> vbNullString) Then
.Content.InsertParagraphAfter
.Content.InsertAfter "Corrective Action Taken: " & Sheets(lastSheet).Range("A27").Value
End If
If (Sheets(lastSheet).Range("B35").Value <> vbNullString) Then
.Content.InsertParagraphAfter
Dim currDepthStr As String
currDepthStr = "Current Depth: " & Sheets(lastSheet).Range("B35").Value & "'"
If (Sheets(lastSheet).Range("B41").Value <> vbNullString) Then currDepthStr = currDepthStr & ", Flow " & Sheets(lastSheet).Range("B41").Value
If (Sheets(lastSheet).Range("B37").Value <> vbNullString) Then currDepthStr = currDepthStr & ", Mud Type " & Sheets(lastSheet).Range("B37").Value
If (Sheets(lastSheet).Range("B38").Value <> vbNullString) Then currDepthStr = currDepthStr & ", Mud Wt. " & Sheets(lastSheet).Range("B38").Value & " GPM"
If (Sheets(lastSheet).Range("F7").Value <> vbNullString) Then currDepthStr = currDepthStr & ", Run # " & Sheets(lastSheet).Range("F7").Value
If (Sheets(lastSheet).Range("F7").Value <> vbNullString) Then currDepthStr = currDepthStr & ", Run # " & Sheets(lastSheet).Range("F7").Value
If (Sheets(lastSheet).Range("F45").Value <> vbNullString) Then currDepthStr = currDepthStr & ", " & Sheets(lastSheet).Range("F45").Value & " circ hours"
If (Sheets(lastSheet).Range("F44").Value <> vbNullString) Then currDepthStr = currDepthStr & ", " & Sheets(lastSheet).Range("F44").Value & " pwr hours"
.Content.InsertAfter currDepthStr
End If
Dim saveFile As String
If (Right(fileDir, 1) <> "\") Then fileDir = fileDir & "\"
#If VBA7 Or VBA8 Then
saveFile = fileDir & Replace(lastSheet, " ", "_") & "_" & jobNumber & "_" & clientName & "_" & RigName & "_" & eventDate & ".docx"
#Else
saveFile = fileDir & Replace(lastSheet, " ", "_") & "_" & jobNumber & "_" & clientName & "_" & RigName & "_" & eventDate & ".doc"
#End If
If (FileExists(saveFile)) Then
If (MsgBox("Cannot save file it already existis in that location." & vbCrLf _
& "Would you like to overwrite it?" & vbCrLf & vbCrLf _
& "If you choose no the file will be opened but not saved", _
vbYesNo, "File Existis") = vbYes) Then
Application.DisplayAlerts = False
.SaveAs saveFile
Application.DisplayAlerts = True
End If
Else
.SaveAs saveFile
End If
End With
msWord.Visible = True
Call ApplicationActions(False) 'Enable all Excel Automatic Actions
End Sub
Bookmarks