Hi,
I apologise for the long code but i am completely stuck. I keep getting a Compile Error: Case Else Outside Select Case @ the final Case Else statement. I've been mucking around with the code for 2 hours and i cannot remedy, Please help
I apologise for the long code but i am completely stuck. I keep getting a Compile Error: Case Else Outside Select Case @ the final Case Else statement. I've been mucking around with the code for 2 hours and i cannot remedy, Please help
Code:
''Routine to generate a file and folder
Private Sub GenerateReport_Click()
Dim wsRegister As Worksheet
Dim wsLookUp As Worksheet
Dim wsCorrelation As Worksheet
Dim Btmrow As Integer
Dim fpathtemplateExcel As String
Dim fnametemplateExcel As String
Dim fpathtemplateWord As String
Dim fnametemplateWord As String
Dim FileID As String
Set wsRegister = ThisWorkbook.Sheets(Register)
Set wsLookUp = ThisWorkbook.Sheets(Lookup)
Set wsCorrelation = ThisWorkbook.Sheets(Correlation)
Dim Fpath As String
Dim FVariable As String
Dim Fname As String
Dim Ans As String
Dim objWord As Object
Dim objdoc As Object
'Find and define btmrow (bottom row)
Btmrow = wsRegister.Cells(wsRegister.Rows.Count, "A").End(xlUp).Row
'Statement for jobs that shall not produce a new file
Select Case wsRegister.Cells(Btmrow, 8).Value
Case Is = "COMPLIANCE": MsgBox ("No file generated for compliance NDT")
Exit Sub
Case Is = "SCOPE": MsgBox ("No file generated for scoping documents")
Exit Sub
Case Is = "Vendor": MsgBox ("No file generated for vendor documents")
Exit Sub
Case Is = "NDT" 'then generate an NDT voucher
'1) Define FPath, this is remains constant
Fpath = "\\BWISHARE1\SHARE\ABU MATERIALS AND INSPECTION ENGINEERING\GORGON\EQUIPMENT INDEX\GGP"
'2) Define FVariable for various subfolders
Select Case wsRegister.Cells(Btmrow, 5).Value
Case Is = "PIPELINE": FVariable = "PIPELINE" & "\" & "WO" & wsRegister.Cells(Btmrow, 3).Text
Case Is = "NT": FVariable = "Non-Tagged Equipment" & "\" & "WO" & wsRegister.Cells(Btmrow, 3).Value
Case Is = "Structural": FVariable = "Structural" & "\" & "WO" & wsRegister.Cells(Btmrow, 3).Value
Case Else: FVariable = "GGP-" & wsRegister.Cells(Btmrow, 5).Text & "\" & "GGP-" & wsRegister.Cells(Btmrow, 5).Text & "-" & wsRegister.Cells(Btmrow, 6).Text & "\" & wsRegister.Cells(Btmrow, 7).Text & "\Inspections\Data" & "\" & "WO" & wsRegister.Cells(Btmrow, 3).Text
'3) Define Fname
Fname = wsRegister.Cells(Btmrow, 10).Text
'Create a new folder if it does not already exist.
If Len(Dir(Fpath & "\" & FVariable)) = 0 Then
'Directory does not exist, it must be created
MkDir Trim(Fpath & "\" & FVariable) 'Creates the folder
End If
Ans = MsgBox("A new folder created has been created in" & Fpath & " \ " & FVariable & vbCr & vbCr & _
"Do you wish to proceed and create a voucher?", vbYesNo, "Confirm")
If Ans = vbNo Then Exit Sub
'To create and save the NDT report file
fpathtemplateExcel = Application.VLookup(wsRegister.Cells(Btmrow, 9), Range("tableCorrelation"), 2, False)
fnametemplateExcel = Application.VLookup(wsRegister.Cells(Btmrow, 9), Range("tableCorrelation"), 3, False)
Workbooks.Open Filename:=fpathtemplateExcel & "\" & fnametemplateExcel
ActiveWorkbook.SaveAs Filename:=Fpath & "\" & FVariable & "\" & Fname
End Select
'To create and save the inspection report file
Case Is = "INSPECTION" 'then create an inspection file
'1) Define FPath, this is remains constant
Fpath = "\\BWISHARE1\SHARE\ABU MATERIALS AND INSPECTION ENGINEERING\GORGON\EQUIPMENT INDEX\GGP"
'2) Define FVariable for various subfolders
Select Case wsRegister.Cells(Btmrow, 5).Value
Case Is = "PIPELINE": FVariable = "PIPELINE" & "\" & "WO" & wsRegister.Cells(Btmrow, 3).Text
Case Is = "NT": FVariable = "Non-Tagged Equipment" & "\" & "WO" & wsRegister.Cells(Btmrow, 3).Value
Case Is = "Structural": FVariable = "Structural" & "\" & "WO" & wsRegister.Cells(Btmrow, 3).Value
Case Else: FVariable = "GGP-" & wsRegister.Cells(Btmrow, 5).Text & "\" & "GGP-" & wsRegister.Cells(Btmrow, 5).Text & "-" & wsRegister.Cells(Btmrow, 6).Text & "\" & wsRegister.Cells(Btmrow, 7).Text & "\Inspections\Data" & "\" & "WO" & wsRegister.Cells(Btmrow, 3).Text
'3) Define Fname
Fname = wsRegister.Cells(Btmrow, 10).Text
'Create a new folder if it does not already exist.
If Len(Dir(Fpath & "\" & FVariable)) = 0 Then 'the directory does not exist, it must be created:
Ans = MsgBox("A new folder created has been created in" & Fpath & " \ " & FVariable & vbCr & vbCr & _
"Do you wish to proceed and create a report?", vbYesNo, "Confirm")
If Ans = vbNo Then Exit Sub
End If
MkDir Trim(Fpath & "\" & FVariable) 'Creates the folder
End Select
'To create and save the inspection report file
Select Case Mid(wsRegister.Cells(Btmrow, 7), 10, 2)
Case Is = "DL":
fpathtemplateWord = Application.VLookup((Mid(wsRegister.Cells(Btmrow, 7), 10, 2) & wsRegister.Cells(Btmrow, 9)), Range("tableCorrelation"), 2, False)
fnametemplateWord = Application.VLookup((Mid(wsRegister.Cells(Btmrow, 7), 10, 2) & wsRegister.Cells(Btmrow, 9)), Range("tableCorrelation"), 3, False)
'Activate word
Set objWord = CreateObject("Word.Application")
Set objdoc = objWord.documents.Add
objWord.Visible = True
objWord.documents.Open Filename:=fpathtemplateWord & "\" & fnametemplateWord
With objWord.activedocument
.Bookmarks("Date").Range.Text = wsRegister.Cells(Btmrow, 11).Text
.Bookmarks("ReportNumber").Range.Text = wsRegister.Cells(Btmrow, 10).Text
.Bookmarks("DamageLoop").Range.Text = wsRegister.Cells(Btmrow, 7).Text
.Bookmarks("Unit").Range.Text = wsRegister.Cells(Btmrow, 6).Text
.Bookmarks("Location").Range.Text = wsRegister.Cells(Btmrow, 5).Text
.Bookmarks("WorkOrder").Range.Text = wsRegister.Cells(Btmrow, 3).Text
.Bookmarks("Person").Range.Text = wsRegister.Cells(Btmrow, 12).Text
objWord.activedocument.SaveAs Filename:=Fpath & "\" & FVariable & "\" & Fname
Set objWord = Nothing
Case Else:
fpathtemplateWord = Application.VLookup("V" & wsRegister.Cells(Btmrow, 9), Range("tableCorrelation"), 2, False)
fnametemplateWord = Application.VLookup("V" & wsRegister.Cells(Btmrow, 9), Range("tableCorrelation"), 3, False)
Debug.Print fpathtemplateWord & "\" & fnametemplateWord
'Activate word
Set objWord = CreateObject("Word.Application")
Set objdoc = objWord.documents.Add
objWord.Visible = True
objWord.documents.Open Filename:=fpathtemplateWord & "\" & fnametemplateWord
withobjword.activedocument
.Bookmarks("Date").Range.Text = wsRegister.Cells(Btmrow, 11).Text
.Bookmarks("ReportNumber").Range.Text = wsRegister.Cells(Btmrow, 10).Text
.Bookmarks("EquipmentNumber").Range.Text = wsRegister.Cells(Btmrow, 7).Text
.Bookmarks("Unit").Range.Text = wsRegister.Cells(Btmrow, 6).Text
.Bookmarks("Location").Range.Text = wsRegister.Cells(Btmrow, 5).Text
.Bookmarks("WorkOrder").Range.Text = wsRegister.Cells(Btmrow, 3).Text
.Bookmarks("Person").Range.Text = wsRegister.Cells(Btmrow, 12).Text
objWord.activedocument.SaveAs Filename:=Fpath & "\" & FVariable & "\" & Fname
Set objWord = Nothing
End Select
End Select
End Sub