

Private Sub CheckBox1_Click()
frmFinancialAnalysis.Show
End Sub

Private Sub cmdTurbochargers_Click()
 
    Me.Hide
    frmActiveDocTurbo.Show
    
        
End Sub

Private Sub cmdCancel_Click()
    Unload Me
    
End Sub


Private Sub cmdCustomers_Click()
    Me.Hide
    frmActiveDocCust.Show
    
End Sub





Private Sub UserForm_Click()

End Sub
--------------------------------------------------------------------------
Private Sub submitActiveDoc_Click()
    If ComboBox1.ListIndex = -1 Then
        MsgBox "Please select a document and try again.", vbExclamation
    Else
        Me.Hide
        frmCust.Show
       
    End If
     
End Sub




Private Sub UserForm_Initialize()
    Dim doc As Inventor.Document
    Dim i As Integer

    For i = 1 To ThisApplication.Documents.Count
        Set doc = ThisApplication.Documents(i)
        If doc.DocumentType = kPartDocumentObject Then
            ComboBox1.AddItem doc.DisplayName
        End If
    Next i
End Sub
--------------------------------------------------------------------------
Option Explicit



Private Sub submitActiveDoc_Click()
    If ComboBox1.ListIndex = -1 Then
        MsgBox "Please select a document and try again.", vbExclamation
    Else
        Me.Hide
        frmTurbo.Show
       
    End If
     
End Sub




Private Sub UserForm_Initialize()
    Dim doc As Inventor.Document
    Dim i As Integer

    For i = 1 To ThisApplication.Documents.Count
        Set doc = ThisApplication.Documents(i)
        If doc.DocumentType = kPartDocumentObject Then
            ComboBox1.AddItem doc.DisplayName
        End If
    Next i
End Sub
--------------------------------------------------------------------------
Private Sub CommandButton1_Click()
    Dim baseFolderPath As String
    Dim code As String
    Dim folderPath As String

    ' basic path
    baseFolderPath = "C:\Users\orspa\Desktop\PROJECT\Customers Part\"

    
    code = Me.TextBox1.Value

    ' Ensure the box is not empty
    If code = "" Then
        MsgBox "Please enter a code.", vbExclamation
        Exit Sub
    End If


    folderPath = baseFolderPath & code & "\"

    If CreateDirectoryIfNotExist(folderPath) Then
        MsgBox "Folder created successfully: " & folderPath, vbInformation
    Else
        MsgBox "Failed to create folder: " & folderPath, vbCritical
    End If


    uFolderPath = folderPath

    
    projectCode1 = TextBox1.Text
    projectCode2 = TextBox2.Text
    projectCode3 = TextBox3.Text
    projectCode4 = TextBox4.Text
    projectCode5 = TextBox5.Text
    

    
    

    
    Me.Hide
    frmCust2.Show
End Sub

Function CreateDirectoryIfNotExist(ByVal folderPath As String) As Boolean
    On Error Resume Next
    If Dir(folderPath, vbDirectory) = "" Then
        MkDir folderPath
    End If
    CreateDirectoryIfNotExist = (Dir(folderPath, vbDirectory) <> "")
    On Error GoTo 0
End Function





Private Sub Label11_Click()

End Sub

Private Sub Label3_Click()

End Sub

Private Sub Label6_Click()

End Sub

Private Sub UserForm_Click()

End Sub
--------------------------------------------------------------------------

Option Explicit

Const wdAlignParagraphCenter As Long = 1
Const wdAlignParagraphLeft As Long = 0
Const msoTrue As Long = -1
Private Sub MultiPage1_Change()

End Sub



Private Sub UserForm_Initialize()
    CheckBox1.Value = False ' final drawings
    CheckBox2.Value = False ' Suppliers
    CheckBox3.Value = False ' Machining Material
    CheckBox4.Value = False ' CAM
    CheckBox5.Value = False ' Routing
    CheckBox6.Value = False ' Photos
    CheckBox7.Value = False ' General Project Remarks
    CheckBox8.Value = False ' Part Analysis
    CheckBox9.Value = False ' Scan Point Cloud
    CheckBox10.Value = False ' Reverse eng. software files
    CheckBox11.Value = False ' Remarks
    CheckBox12.Value = False ' CAD STEP
    CheckBox13.Value = False ' CMM
    CheckBox14.Value = False ' Screenshot Photos
    CheckBox15.Value = False ' ISO Certificates
    CheckBox16.Value = False ' Quality Control Files
End Sub

Function CreateDirectoryIfNotExist(ByVal folderPath As String) As Boolean
    On Error Resume Next
    If Dir(folderPath, vbDirectory) = "" Then
        MkDir folderPath
    End If
    CreateDirectoryIfNotExist = (Dir(folderPath, vbDirectory) <> "")
    On Error GoTo 0
End Function

Private Sub SubmitCust1_Click()
    Dim oDoc As Inventor.Document
    
    Set oDoc = GetSelectedDocument()

    Dim folderPath As String
    Dim selectedFolders As Collection
    Dim i As Integer

    folderPath = uFolderPath

    Set selectedFolders = New Collection

    ' Add folders based on the checkbox values
    If CheckBox1.Value = True Then
        selectedFolders.Add folderPath & "ENGINEERING DRAWINGS"
        selectedFolders.Add folderPath & "ENGINEERING DRAWINGS\Engineering Drawings"
    End If

    If CheckBox2.Value = True Or CheckBox3.Value = True Or CheckBox4.Value = True Or CheckBox5.Value = True Or CheckBox6.Value = True Or CheckBox7.Value = True Then
        selectedFolders.Add folderPath & "MANUFACTURING FILES"
        If CheckBox2.Value = True Then
            selectedFolders.Add folderPath & "MANUFACTURING FILES\01 SUPPLIERS"
        End If
        If CheckBox3.Value = True Then
            selectedFolders.Add folderPath & "MANUFACTURING FILES\02 MACHINING MATERIAL"
        End If
        If CheckBox4.Value = True Then
            selectedFolders.Add folderPath & "MANUFACTURING FILES\03 CAM"
        End If
        If CheckBox5.Value = True Then
            selectedFolders.Add folderPath & "MANUFACTURING FILES\04 ROUTING"
        End If
        If CheckBox6.Value = True Then
            selectedFolders.Add folderPath & "MANUFACTURING FILES\05 PHOTOS"
        End If
        If CheckBox7.Value = True Then
            selectedFolders.Add folderPath & "MANUFACTURING FILES\06 REMARKS"
        End If
    End If

    If CheckBox8.Value = True Or CheckBox9.Value = True Or CheckBox10.Value = True Or CheckBox11.Value = True Or CheckBox12.Value = True Or CheckBox13.Value = True Or CheckBox14.Value = True Then
        selectedFolders.Add folderPath & "REVERSE ENGINEERING FILES"
        If CheckBox8.Value = True Then
            selectedFolders.Add folderPath & "REVERSE ENGINEERING FILES\01 PART ANALYSIS"
        End If
        If CheckBox9.Value = True Then
            selectedFolders.Add folderPath & "REVERSE ENGINEERING FILES\02 SCAN POINT CLOUD"
        End If
        If CheckBox10.Value = True Then
            selectedFolders.Add folderPath & "REVERSE ENGINEERING FILES\03 REVERSE ENG. SOFTWARE FILES"
        End If
        If CheckBox11.Value = True Then
            selectedFolders.Add folderPath & "REVERSE ENGINEERING FILES\04 REMARKS"
        End If
        If CheckBox12.Value = True Then
            selectedFolders.Add folderPath & "REVERSE ENGINEERING FILES\05 CAD-STEP"
        End If
        If CheckBox13.Value = True Then
            selectedFolders.Add folderPath & "REVERSE ENGINEERING FILES\06 CMM"
        End If
        If CheckBox14.Value = True Then
            selectedFolders.Add folderPath & "REVERSE ENGINEERING FILES\07 SCREENSHOT - PHOTOS"
        End If
    End If

    If CheckBox15.Value = True Or CheckBox16.Value = True Then
        selectedFolders.Add folderPath & "QUALITY CONTROL FILES"
        If CheckBox15.Value = True Then
            selectedFolders.Add folderPath & "QUALITY CONTROL FILES\01 ISO CERTIFICATES"
        End If
        If CheckBox16.Value = True Then
            selectedFolders.Add folderPath & "QUALITY CONTROL FILES\02 QUALITY CONTROL FILES"
        End If
    End If

    ' Create the selected folders
    For i = 1 To selectedFolders.Count
        CreateDirectoryIfNotExist selectedFolders(i)
    Next i

    'Check and write folder status
    Call CheckAndWriteFolderStatus(folderPath)
    
    ' Check if CAD-STEP checkbox is selected
    If CheckBox12.Value = True Then
        Dim filePath As String
        Dim fileName As String
        fileName = Replace(oDoc.DisplayName, ".ipt", "")
        filePath = folderPath & "REVERSE ENGINEERING FILES\05 CAD-STEP\" & fileName & ".ipt"

        oDoc.SaveAs filePath, True
        MsgBox "Document saved"
        
        Call CreateAndSaveDrawing(oDoc, folderPath, fileName) ' Calls the sub for creating drawings
    Else
        MsgBox "CAD-STEP checkbox is not selected. No file saved in CAD STEP folder."
    End If

    ' Check if REMARKS checkbox is selected
    If CheckBox11.Value = True Then
        CreateWordDocument folderPath, code, projectCode1, projectCode2, projectCode3, projectCode4, projectCode5, projectCode6, projectCode7, projectCode8, projectCode9, projectCode10
    Else
        MsgBox "REMARKS checkbox is not selected. No Word document saved in REMARKS folder."
    End If

    Me.Hide
End Sub
Private Sub CheckAndWriteFolderStatus(folderPath As String)
    Dim xlApp As Object
    Dim xlBook As Object
    Dim xlSheet As Object
    Dim lastRow As Long
    Dim excelFilePath As String
    Dim sheetName As String

    excelFilePath = "C:\Users\orspa\Desktop\PROJECT\Customer Parts.xlsx"
    sheetName = "Sheet1" ' Make sure this matches exactly with the sheet name

    ' Check if the file exists
    If Dir(excelFilePath) = "" Then
        MsgBox "Excel file not found: " & excelFilePath, vbExclamation
        Exit Sub
    End If
    
    Set xlApp = CreateObject("Excel.Application")
    xlApp.Visible = False
    
    ' Open the existing Excel file
    Set xlBook = xlApp.Workbooks.Open(excelFilePath)
    
    ' Try to set the worksheet, and handle error if the sheet name is incorrect
    On Error GoTo ErrHandler
    Set xlSheet = xlBook.Sheets(sheetName)
    On Error GoTo 0
    
    ' Find the last row in column C
    lastRow = xlSheet.Cells(xlSheet.Rows.Count, "C").End(xlUp).row
    If lastRow < 2 Then lastRow = 1
    
    ' Write project codes to the Excel file
    xlSheet.Cells(lastRow + 1, 3).Value = projectCode1
    xlSheet.Cells(lastRow + 1, 4).Value = projectCode2
    xlSheet.Cells(lastRow + 1, 6).Value = projectCode3
    xlSheet.Cells(lastRow + 1, 7).Value = projectCode4
    xlSheet.Cells(lastRow + 1, 8).Value = projectCode5
    

    ' Check and write the status for each folder and add hyperlinks
    AddHyperlinkedStatus xlSheet, lastRow + 1, 17, folderPath & "FINAL DRAWINGS", True
    AddHyperlinkedStatus xlSheet, lastRow + 1, 18, folderPath & "MANUFACTURING FILES"
    AddHyperlinkedStatus xlSheet, lastRow + 1, 16, folderPath & "REVERSE ENGINEERING FILES"
    AddHyperlinkedStatus xlSheet, lastRow + 1, 19, folderPath & "QUALITY CONTROL FILES"
    
    xlBook.Save
    xlBook.Close False
    
    Set xlSheet = Nothing
    Set xlBook = Nothing
    xlApp.Quit
    Set xlApp = Nothing
    
    Exit Sub
    
ErrHandler:
    MsgBox "Sheet '" & sheetName & "' not found in the workbook.", vbExclamation
    xlBook.Close False
    Set xlSheet = Nothing
    Set xlBook = Nothing
    xlApp.Quit
    Set xlApp = Nothing
End Sub

Private Sub AddHyperlinkedStatus(sheet As Object, row As Long, col As Long, folderPath As String, Optional isFinalDrawings As Boolean = False)
    Dim status As String
    If isFinalDrawings Then
        status = IIf(Dir(folderPath, vbDirectory) <> "", "DRW-" & projectCode1, "NO")
    Else
        status = IIf(Dir(folderPath, vbDirectory) <> "", "YES", "NO")
    End If
    sheet.Cells(row, col).Value = status
    If status <> "NO" Then
        sheet.Hyperlinks.Add Anchor:=sheet.Cells(row, col), Address:=folderPath, TextToDisplay:=status
    End If
End Sub




Sub CreateAndSaveDrawing(oDoc As Inventor.Document, folderPath As String, fileName As String)
    Dim oApp As Inventor.Application
    Dim oDrawDoc As Inventor.DrawingDocument
    Dim oSheet As Inventor.sheet
    Dim oView As Inventor.DrawingView
    Dim oView1 As Inventor.DrawingView
    Dim oView2 As Inventor.DrawingView
    Dim oTG As TransientGeometry

    Set oApp = ThisApplication
    Set oTG = oApp.TransientGeometry

    ' Specify the path to your custom drawing template
    Dim customTemplatePath As String
    customTemplatePath = "C:\Users\Public\Documents\Autodesk\Inventor 2025\Templates\en-US\PROJECT TEMPLATE CUST.idw"

    ' Create new drawing document using custom template
    Set oDrawDoc = oApp.Documents.Add(kDrawingDocumentObject, customTemplatePath, True)
    Set oSheet = oDrawDoc.Sheets.Item(1)

    ' Get the bounding box of the object
    Dim oBox As Box
    Set oBox = oDoc.ComponentDefinition.RangeBox

    ' Calculate the scale factor to fit the object into the drawing sheet
    Dim sheetWidth As Double
    Dim sheetHeight As Double
    sheetWidth = oSheet.width
    sheetHeight = oSheet.height

    Dim objectWidth As Double
    Dim objectHeight As Double
    objectWidth = oBox.MaxPoint.X - oBox.MinPoint.X
    objectHeight = oBox.MaxPoint.Y - oBox.MinPoint.Y

    Dim scaleX As Double
    Dim scaleY As Double
    scaleX = (sheetWidth / 3) / objectWidth * 0.8
    scaleY = (sheetHeight / 2) / objectHeight * 0.8

    Dim scaleFactor As Double
    scaleFactor = IIf(scaleX < scaleY, scaleX, scaleY)

    ' Define points for the views
    Dim oPoint1 As Point2d
    Dim oPoint2 As Point2d
    Dim oPoint3 As Point2d
    Set oPoint1 = oTG.CreatePoint2d(sheetWidth / 6, sheetHeight / 2) ' Left view
    Set oPoint2 = oTG.CreatePoint2d(sheetWidth / 2, sheetHeight / 2) ' Center view
    Set oPoint3 = oTG.CreatePoint2d(5 * sheetWidth / 6, sheetHeight / 2) ' Right view

    ' Add views
    Set oView = oSheet.DrawingViews.AddBaseView(oDoc, oPoint1, scaleFactor, kFrontViewOrientation, kHiddenLineRemovedDrawingViewStyle)
    Set oView1 = oSheet.DrawingViews.AddBaseView(oDoc, oPoint2, scaleFactor, kRightViewOrientation, kHiddenLineRemovedDrawingViewStyle)
    Set oView2 = oSheet.DrawingViews.AddBaseView(oDoc, oPoint3, scaleFactor * 0.5, kIsoBottomRightViewOrientation, kShadedDrawingViewStyle)
    
    AddHorizontalChainDimensionSet oSheet
    Call ChgTitleBlkDef(oDrawDoc)

    ' Save the drawing
    Dim drawingFilePath As String
    drawingFilePath = folderPath & "REVERSE ENGINEERING FILES\05 CAD-STEP\" & fileName & ".idw"
    oDrawDoc.SaveAs drawingFilePath, True
    MsgBox "Drawing saved as " & drawingFilePath
End Sub
Sub AddHorizontalChainDimensionSet(oSheet As Inventor.sheet)
    ' Set a reference to the transient geometry and object collections
    Dim oTransGeom As TransientGeometry
    Set oTransGeom = ThisApplication.TransientGeometry
    Dim oIntentCollection As ObjectCollection
    Set oIntentCollection = ThisApplication.TransientObjects.CreateObjectCollection

    ' Iterate through all drawing views to find the one with edges
    Dim oDrawingView As DrawingView
    Dim oViewWithEdges As DrawingView
    For Each oDrawingView In oSheet.DrawingViews
        If oDrawingView.DrawingCurves.Count > 0 Then
            Set oViewWithEdges = oDrawingView
            Exit For
        End If
    Next

    ' Check if a view with edges was found
    If oViewWithEdges Is Nothing Then
        MsgBox "No drawing view with edges found."
        Exit Sub
    End If

    ' Iterate through all drawing curves in the found drawing view
    Dim oDrawingCurve As DrawingCurve
    For Each oDrawingCurve In oViewWithEdges.DrawingCurves
        Dim oDimIntent As GeometryIntent
        Set oDimIntent = oSheet.CreateGeometryIntent(oDrawingCurve)
        Call oIntentCollection.Add(oDimIntent)
    Next

    ' Determine the placement point (adjust the Y coordinate based on your view)
    Dim oPlacementPoint As Point2d
    Set oPlacementPoint = oTransGeom.CreatePoint2d(oViewWithEdges.Left, oViewWithEdges.Center.Y - 8)

    ' Set a reference to the chain dimension sets collection
    Dim oChainDimSets As ChainDimensionSets
    Set oChainDimSets = oSheet.DrawingDimensions.ChainDimensionSets

    ' Create a horizontal dimension set
    Dim oChainDimSet As ChainDimensionSet
    Set oChainDimSet = oChainDimSets.Add(oIntentCollection, oPlacementPoint, kHorizontalDimensionType)
End Sub
Sub ChgTitleBlkDef(oDrawDoc As DrawingDocument)
    Dim colTitleBlkDefs As TitleBlockDefinitions
    Set colTitleBlkDefs = oDrawDoc.TitleBlockDefinitions

    Dim objTitleBlkDef As TitleBlockDefinition
    Dim found As Boolean
    found = False

    
    Debug.Print "Available Title Block Definitions:"
    For Each objTitleBlkDef In colTitleBlkDefs
        Debug.Print objTitleBlkDef.Name
        If objTitleBlkDef.Name = "custome title book" Then
            found = True
            Exit For
        End If
    Next objTitleBlkDef

    If Not found Then
        MsgBox "custom title block TitleBlockDefinition not found!"
        Set colTitleBlkDefs = Nothing
        Set objTitleBlkDef = Nothing
        Exit Sub
    End If

    
    Dim objDrwSketch As DrawingSketch
    Call objTitleBlkDef.Edit(objDrwSketch)

    Dim colTextBoxes As TextBoxes
    Set colTextBoxes = objDrwSketch.TextBoxes

    Dim objTextBox As TextBox
    For Each objTextBox In colTextBoxes
        If objTextBox.Text = "<PROJECT ID>" And projectCode1 <> "" Then
            objTextBox.Text = projectCode1
        ElseIf objTextBox.Text = "<ENGINEER>" And projectCode5 <> "" Then
            objTextBox.Text = projectCode5
        ElseIf objTextBox.Text = "<TYPE>" And projectCode3 <> "" Then
            objTextBox.Text = projectCode3
        


        End If
    Next objTextBox

    Call objTitleBlkDef.ExitEdit(True)

    ' Clean up
    Set colTitleBlkDefs = Nothing
    Set objTitleBlkDef = Nothing
    Set objDrwSketch = Nothing
    Set colTextBoxes = Nothing
    Set objTextBox = Nothing

    Beep
End Sub
Sub CaptureDrawingViewAsImage(imgFilePath As String)
    ' Declare variables
    Dim oApp As Application
    Dim oView As View
    Dim oOptions As NameValueMap

    ' Set the application
    Set oApp = ThisApplication

    ' Get the active view
    Set oView = oApp.ActiveView

    oApp.ActiveView.Update

    ' Create a new NameValueMap Object for options
    Set oOptions = oApp.TransientObjects.CreateNameValueMap

    ' Export the view as an image
    Call oView.SaveAsBitmapWithOptions(imgFilePath, 800, 600, oOptions) ' Adjusted size to 800x600
End Sub
Sub CreateWordDocument(ByVal folderPath As String, ByVal code As String, ByVal projectCode1 As String, ByVal projectCode2 As String, ByVal projectCode3 As String, ByVal projectCode4 As String, ByVal projectCode5 As String, ByVal projectCode6 As String, ByVal projectCode7 As String, ByVal projectCode8 As String, ByVal projectCode9 As String, ByVal projectCode10 As String)
    Dim wordApp As Object
    Dim wordDoc As Object
    Dim filePath As String
    Dim remarksFolderPath As String
    Dim imgFilePath As String
    Dim oDoc As Inventor.Document
    Set oDoc = GetSelectedDocument()
    

    ' Folder to save the Word document
    remarksFolderPath = folderPath & "REVERSE ENGINEERING FILES\04 REMARKS\"

    ' Check if the folder exists
    If Dir(remarksFolderPath, vbDirectory) = "" Then
        MsgBox "REMARKS folder does not exist. No Word document saved in REMARKS folder.", vbExclamation
        Exit Sub
    End If

    ' Path and name of the Word document
    filePath = remarksFolderPath & projectCode1 & ".docx"
    'filePath = "C:\Users\orspa\Desktop\Document.docx"
    imgFilePath = Environ("TEMP") & "\DrawingViewImage.bmp"
    
    ' Capture the drawing view as an image
    Call CaptureDrawingViewAsImage(imgFilePath)
    
    ' Create a new instance of Word application
    On Error Resume Next
    Set wordApp = CreateObject("Word.Application")
    On Error GoTo 0

    If wordApp Is Nothing Then
        MsgBox "Word application is not available. Please ensure Microsoft Word is installed."
        Exit Sub
    End If

    ' Make the Word application visible
    wordApp.Visible = True
    
    ' Create a new Word document
    Set wordDoc = wordApp.Documents.Add
    
  ' Get the bounding box dimensions using your provided logic
    Dim oBox As Box
    Set oBox = oDoc.ComponentDefinition.RangeBox
    Dim minX As Double, minY As Double, minZ As Double
    Dim maxX As Double, maxY As Double, maxZ As Double
    minX = oBox.MinPoint.X
    minY = oBox.MinPoint.Y
    minZ = oBox.MinPoint.Z
    maxX = oBox.MaxPoint.X
    maxY = oBox.MaxPoint.Y
    maxZ = oBox.MaxPoint.Z
    Dim length As Double, width As Double, height As Double
    length = (maxX - minX) * 10 ' Convert cm to mm
    width = (maxY - minY) * 10 ' Convert cm to mm
    height = (maxZ - minZ) * 10 ' Convert cm to mm
    
    ' Add text content to the Word document first
    With wordDoc.Content
        .Paragraphs.Add
        .Paragraphs.Last.Range.ParagraphFormat.Alignment = wdAlignParagraphLeft
        .Text = "Project Code: " & projectCode1 & vbCrLf & _
                "Engineer: " & projectCode2 & vbCrLf & _
                "Maker: " & projectCode3 & vbCrLf & _
                "Type: " & projectCode4 & vbCrLf & _
                "Part: " & projectCode5 & vbCrLf & _
                "Spec: " & projectCode6 & vbCrLf & _
                "Part NR: " & projectCode7 & vbCrLf & _
                "Origin: " & projectCode8 & vbCrLf & _
                "Condition: " & projectCode9 & vbCrLf & _
                "Scope: " & projectCode10 & vbCrLf & _
                "Bounding Box Dimensions (mm): " & vbCrLf & _
                "Length: " & length & " mm" & vbCrLf & _
                "Width: " & width & " mm" & vbCrLf & _
                "Height: " & height & " mm" & vbCrLf & _
                "FINANCIAL ANALYSIS" & vbCrLf & _
                "Material Cost: " & materialCostGlobal & " Euro" & vbCrLf & _
                "Reverse Engineering Cost: " & reverseEngineeringCostGlobal & " Euro" & vbCrLf & _
                "Set Up Cost: " & setUpCostGlobal & " Euro" & vbCrLf & _
                "Machining Cost: " & machiningCostGlobal & " Euro" & vbCrLf & _
                "Total Cost: " & totalCostGlobal & " Euro"
    End With
    ' save
    On Error GoTo ErrorHandler
    wordDoc.SaveAs2 fileName:=filePath

    ' Insert the image after text
    Dim oShape As InlineShape
    With wordDoc.Content
        .Paragraphs.Add
        .Paragraphs.Last.Range.ParagraphFormat.Alignment = wdAlignParagraphCenter ' Center the image
        Set oShape = .InlineShapes.AddPicture(fileName:=imgFilePath, LinkToFile:=False, SaveWithDocument:=True, Range:=.Paragraphs.Last.Range)
        oShape.LockAspectRatio = msoTrue
        oShape.width = wordApp.CentimetersToPoints(15)
        
    End With

    ' Save the document again
    wordDoc.Save

   
    wordDoc.Close

    wordApp.Quit
    Set wordDoc = Nothing
    Set wordApp = Nothing

    MsgBox "Word document created and saved as " & filePath
    Exit Sub

ErrorHandler:
    MsgBox "An error occurred while saving the Word document: " & Err.Description
    If Not wordDoc Is Nothing Then wordDoc.Close False
    If Not wordApp Is Nothing Then wordApp.Quit
    Set wordDoc = Nothing
    Set wordApp = Nothing
End Sub

--------------------------------------------------------------------------
Option Explicit

Private Sub Label3_Click()

End Sub

Private Sub Label5_Click()

End Sub

Private Sub SavebuttonFinance_Click()
  Dim materialPrice As Double
    Dim reverseEngineeringTime As Double
    Dim setUpTime As Double
    Dim machiningTime As Double
    Dim tools As Double
    Dim totalCost As Double

    Dim materialCost As Double
    Dim reverseEngineeringCost As Double
    Dim setUpCost As Double
    Dim machiningCost As Double

    ' Validate and convert input data
    On Error GoTo ErrorHandler
    materialPrice = CDbl(Finance1.Text)
    reverseEngineeringTime = CDbl(Finance2.Text)
    setUpTime = CDbl(Finance3.Text)
    machiningTime = CDbl(Finance4.Text)
    tools = CDbl(Finance5.Text)

    ' Perform calculations
    materialCost = materialPrice * 1.15
    reverseEngineeringCost = reverseEngineeringTime * 60
    setUpCost = setUpTime * 30
    machiningCost = machiningTime * 30
    totalCost = materialCost + reverseEngineeringCost + setUpCost + machiningCost + tools

    ' Update global variables
    materialCostGlobal = materialCost
    reverseEngineeringCostGlobal = reverseEngineeringCost
    setUpCostGlobal = setUpCost
    machiningCostGlobal = machiningCost
    totalCostGlobal = totalCost

   
    
    MsgBox "Financial data saved successfully!", vbInformation
    Unload Me
    Exit Sub

ErrorHandler:
    MsgBox "Please enter valid numbers for all fields.", vbCritical
End Sub


Public Sub SaveFinancialData(materialPrice As Double, reverseEngineeringTime As Double, setUpTime As Double, machiningTime As Double, tools As Double, materialCost As Double, reverseEngineeringCost As Double, setUpCost As Double, machiningCost As Double, totalCost As Double)
    ' Store the calculated costs in global variables
    totalCostGlobal = totalCost
    materialCostGlobal = materialCost
    reverseEngineeringCostGlobal = reverseEngineeringCost
    setUpCostGlobal = setUpCost
    machiningCostGlobal = machiningCost
End Sub

Private Sub UserForm_Click()

End Sub

--------------------------------------------------------------------------

Option Explicit

Private Sub CommandButton1_Click()
    Dim baseFolderPath As String
    Dim code As String
    Dim folderPath As String

    ' basic path
    baseFolderPath = "C:\Users\orspa\Desktop\PROJECT\Turbochargers Part\"

    
    code = Me.TextBox1.Value

    ' Ensure the box is not empty
    If code = "" Then
        MsgBox "Please enter a code.", vbExclamation
        Exit Sub
    End If


    folderPath = baseFolderPath & code & "\"

    
    If CreateDirectoryIfNotExist(folderPath) Then
        MsgBox "Folder created successfully: " & folderPath, vbInformation
    Else
        MsgBox "Failed to create folder: " & folderPath, vbCritical
    End If


    uFolderPath = folderPath

    projectCode1 = TextBox1.Text
    projectCode2 = TextBox2.Text
    projectCode3 = TextBox3.Text
    projectCode4 = TextBox4.Text
    projectCode5 = TextBox5.Text
    projectCode6 = TextBox6.Text
    projectCode7 = TextBox7.Text
    projectCode8 = TextBox8.Text
    projectCode9 = TextBox9.Text
    projectCode10 = TextBox10.Text

    
    

    
    Me.Hide
    frmTurbo2.Show
End Sub

Function CreateDirectoryIfNotExist(ByVal folderPath As String) As Boolean
    On Error Resume Next
    If Dir(folderPath, vbDirectory) = "" Then
        MkDir folderPath
    End If
    CreateDirectoryIfNotExist = (Dir(folderPath, vbDirectory) <> "")
    On Error GoTo 0
End Function





Private Sub Label11_Click()

End Sub

Private Sub UserForm_Click()

End Sub


--------------------------------------------------------------------------

Option Explicit

Const wdAlignParagraphCenter As Long = 1
Const wdAlignParagraphLeft As Long = 0
Const msoTrue As Long = -1


Private Sub CheckBox1_Click()

End Sub

Private Sub CheckBox10_Click()

End Sub

Private Sub MultiPage1_Change()

End Sub

Private Sub UserForm_Initialize()
    CheckBox1.Value = False ' final drawings
    CheckBox2.Value = False ' Suppliers
    CheckBox3.Value = False ' Machining Material
    CheckBox4.Value = False ' CAM
    CheckBox5.Value = False ' Routing
    CheckBox6.Value = False ' Photos
    CheckBox7.Value = False ' General Project Remarks
    CheckBox8.Value = False ' Part Analysis
    CheckBox9.Value = False ' Scan Point Cloud
    CheckBox10.Value = False ' Reverse Eng. files
    CheckBox11.Value = False ' Remarks
    CheckBox12.Value = False ' CAD STEP
    CheckBox13.Value = False ' CMM
    CheckBox14.Value = False ' Screenshot Photos
    CheckBox15.Value = False ' ISO Certificates
    CheckBox16.Value = False ' Quality Control Files
End Sub

Function CreateDirectoryIfNotExist(ByVal folderPath As String) As Boolean
    On Error Resume Next
    If Dir(folderPath, vbDirectory) = "" Then
        MkDir folderPath
    End If
    CreateDirectoryIfNotExist = (Dir(folderPath, vbDirectory) <> "")
    On Error GoTo 0
End Function

Private Sub SubmitTurbo_Click()
    Dim oDoc As Inventor.Document
    
    Set oDoc = GetSelectedDocument()

    Dim folderPath As String
    Dim selectedFolders As Collection
    Dim i As Integer

    folderPath = uFolderPath

    Set selectedFolders = New Collection

    ' Add folders based on the checkbox values
    If CheckBox1.Value = True Then
        selectedFolders.Add folderPath & "ENGINEERING DRAWINGS"
        selectedFolders.Add folderPath & "ENGINEERING DRAWINGS\Engineering Drawings"
    End If

    If CheckBox2.Value = True Or CheckBox3.Value = True Or CheckBox4.Value = True Or CheckBox5.Value = True Or CheckBox6.Value = True Or CheckBox7.Value = True Then
        selectedFolders.Add folderPath & "MANUFACTURING FILES"
        If CheckBox2.Value = True Then
            selectedFolders.Add folderPath & "MANUFACTURING FILES\01 SUPPLIERS"
        End If
        If CheckBox3.Value = True Then
            selectedFolders.Add folderPath & "MANUFACTURING FILES\02 MACHINING MATERIAL"
        End If
        If CheckBox4.Value = True Then
            selectedFolders.Add folderPath & "MANUFACTURING FILES\03 CAM"
        End If
        If CheckBox5.Value = True Then
            selectedFolders.Add folderPath & "MANUFACTURING FILES\04 ROUTING"
        End If
        If CheckBox6.Value = True Then
            selectedFolders.Add folderPath & "MANUFACTURING FILES\05 PHOTOS"
        End If
        If CheckBox7.Value = True Then
            selectedFolders.Add folderPath & "MANUFACTURING FILES\06 REMARKS"
        End If
    End If

    If CheckBox8.Value = True Or CheckBox9.Value = True Or CheckBox10.Value = True Or CheckBox11.Value = True Or CheckBox12.Value = True Or CheckBox13.Value = True Or CheckBox14.Value = True Then
        selectedFolders.Add folderPath & "REVERSE ENGINEERING FILES"
        If CheckBox8.Value = True Then
            selectedFolders.Add folderPath & "REVERSE ENGINEERING FILES\01 PART ANALYSIS"
        End If
        If CheckBox9.Value = True Then
            selectedFolders.Add folderPath & "REVERSE ENGINEERING FILES\02 SCAN POINT CLOUD"
        End If
        If CheckBox10.Value = True Then
            selectedFolders.Add folderPath & "REVERSE ENGINEERING FILES\03 REVERSE ENG. SOFTWARE FILES"
        End If
        If CheckBox11.Value = True Then
            selectedFolders.Add folderPath & "REVERSE ENGINEERING FILES\04 REMARKS"
        End If
        If CheckBox12.Value = True Then
            selectedFolders.Add folderPath & "REVERSE ENGINEERING FILES\05 CAD-STEP"
        End If
        If CheckBox13.Value = True Then
            selectedFolders.Add folderPath & "REVERSE ENGINEERING FILES\06 CMM"
        End If
        If CheckBox14.Value = True Then
            selectedFolders.Add folderPath & "REVERSE ENGINEERING FILES\07 SCREENSHOT - PHOTOS"
        End If
    End If

    If CheckBox15.Value = True Or CheckBox16.Value = True Then
        selectedFolders.Add folderPath & "QUALITY CONTROL FILES"
        If CheckBox15.Value = True Then
            selectedFolders.Add folderPath & "QUALITY CONTROL FILES\01 ISO CERTIFICATES"
        End If
        If CheckBox16.Value = True Then
            selectedFolders.Add folderPath & "QUALITY CONTROL FILES\02 QUALITY CONTROL FILES"
        End If
    End If

    ' Create the selected folders
    For i = 1 To selectedFolders.Count
        CreateDirectoryIfNotExist selectedFolders(i)
    Next i

    'Check and write folder status
    Call CheckAndWriteFolderStatus(folderPath)
    
    ' Check if CAD-STEP checkbox is selected
    If CheckBox12.Value = True Then
        Dim filePath As String
        Dim fileName As String
        fileName = Replace(oDoc.DisplayName, ".ipt", "")
        filePath = folderPath & "REVERSE ENGINEERING FILES\05 CAD-STEP\" & fileName & ".ipt"

        oDoc.SaveAs filePath, True
        MsgBox "Document saved"
        
        Call CreateAndSaveDrawing(oDoc, folderPath, fileName) ' Calls the sub for creating drawings
    Else
        MsgBox "CAD-STEP checkbox is not selected. No file saved in CAD STEP folder."
    End If

    ' Check if REMARKS checkbox is selected
    If CheckBox11.Value = True Then
        CreateWordDocument folderPath, code, projectCode1, projectCode2, projectCode3, projectCode4, projectCode5, projectCode6, projectCode7, projectCode8, projectCode9, projectCode10
    Else
        MsgBox "REMARKS checkbox is not selected. No Word document saved in REMARKS folder."
    End If

    Me.Hide
End Sub
Private Sub CheckAndWriteFolderStatus(folderPath As String)
    Dim xlApp As Object
    Dim xlBook As Object
    Dim xlSheet As Object
    Dim lastRow As Long
    Dim excelFilePath As String
    Dim sheetName As String

    excelFilePath = "C:\Users\orspa\Desktop\PROJECT\Turbocharger Parts.xlsx"
    sheetName = "Sheet1" ' Make sure this matches exactly with the sheet name

    ' Check if the file exists
    If Dir(excelFilePath) = "" Then
        MsgBox "Excel file not found: " & excelFilePath, vbExclamation
        Exit Sub
    End If
    
    Set xlApp = CreateObject("Excel.Application")
    xlApp.Visible = False
    
    ' Open the existing Excel file
    Set xlBook = xlApp.Workbooks.Open(excelFilePath)
    
    ' Try to set the worksheet, and handle error if the sheet name is incorrect
    On Error GoTo ErrHandler
    Set xlSheet = xlBook.Sheets(sheetName)
    On Error GoTo 0
    
    ' Find the last row in column C
    lastRow = xlSheet.Cells(xlSheet.Rows.Count, "C").End(xlUp).row
    If lastRow < 2 Then lastRow = 1
    
    ' Write project codes to the Excel file
    xlSheet.Cells(lastRow + 1, 3).Value = projectCode1
    xlSheet.Cells(lastRow + 1, 4).Value = projectCode2
    xlSheet.Cells(lastRow + 1, 6).Value = projectCode3
    xlSheet.Cells(lastRow + 1, 7).Value = projectCode4
    xlSheet.Cells(lastRow + 1, 8).Value = projectCode5
    xlSheet.Cells(lastRow + 1, 9).Value = projectCode6
    xlSheet.Cells(lastRow + 1, 10).Value = projectCode7
    xlSheet.Cells(lastRow + 1, 12).Value = projectCode8
    xlSheet.Cells(lastRow + 1, 13).Value = projectCode9
    xlSheet.Cells(lastRow + 1, 15).Value = projectCode10

    ' Check and write the status for each folder and add hyperlinks
    AddHyperlinkedStatus xlSheet, lastRow + 1, 17, folderPath & "FINAL DRAWINGS", True
    AddHyperlinkedStatus xlSheet, lastRow + 1, 18, folderPath & "MANUFACTURING FILES"
    AddHyperlinkedStatus xlSheet, lastRow + 1, 16, folderPath & "REVERSE ENGINEERING FILES"
    AddHyperlinkedStatus xlSheet, lastRow + 1, 19, folderPath & "QUALITY CONTROL FILES"
    
    xlBook.Save
    xlBook.Close False
    
    Set xlSheet = Nothing
    Set xlBook = Nothing
    xlApp.Quit
    Set xlApp = Nothing
    
    Exit Sub
    
ErrHandler:
    MsgBox "Sheet '" & sheetName & "' not found in the workbook.", vbExclamation
    xlBook.Close False
    Set xlSheet = Nothing
    Set xlBook = Nothing
    xlApp.Quit
    Set xlApp = Nothing
End Sub

Private Sub AddHyperlinkedStatus(sheet As Object, row As Long, col As Long, folderPath As String, Optional isFinalDrawings As Boolean = False)
    Dim status As String
    If isFinalDrawings Then
        status = IIf(Dir(folderPath, vbDirectory) <> "", "DRW-" & projectCode1, "NO")
    Else
        status = IIf(Dir(folderPath, vbDirectory) <> "", "YES", "NO")
    End If
    sheet.Cells(row, col).Value = status
    If status <> "NO" Then
        sheet.Hyperlinks.Add Anchor:=sheet.Cells(row, col), Address:=folderPath, TextToDisplay:=status
    End If
End Sub




Sub CreateAndSaveDrawing(oDoc As Inventor.Document, folderPath As String, fileName As String)
    Dim oApp As Inventor.Application
    Dim oDrawDoc As Inventor.DrawingDocument
    Dim oSheet As Inventor.sheet
    Dim oView As Inventor.DrawingView
    Dim oView1 As Inventor.DrawingView
    Dim oView2 As Inventor.DrawingView
    Dim oTG As TransientGeometry

    Set oApp = ThisApplication
    Set oTG = oApp.TransientGeometry

    ' Specify the path to your custom drawing template
    Dim customTemplatePath As String
    customTemplatePath = "C:\Users\Public\Documents\Autodesk\Inventor 2025\Templates\en-US\PROJECT TEMPLATE.idw"

    ' Create new drawing document using custom template
    Set oDrawDoc = oApp.Documents.Add(kDrawingDocumentObject, customTemplatePath, True)
    Set oSheet = oDrawDoc.Sheets.Item(1)

    ' Get the bounding box of the object
    Dim oBox As Box
    Set oBox = oDoc.ComponentDefinition.RangeBox

    ' Calculate the scale factor to fit the object into the drawing sheet
    Dim sheetWidth As Double
    Dim sheetHeight As Double
    sheetWidth = oSheet.width
    sheetHeight = oSheet.height

    Dim objectWidth As Double
    Dim objectHeight As Double
    objectWidth = oBox.MaxPoint.X - oBox.MinPoint.X
    objectHeight = oBox.MaxPoint.Y - oBox.MinPoint.Y

    Dim scaleX As Double
    Dim scaleY As Double
    scaleX = (sheetWidth / 3) / objectWidth * 0.8
    scaleY = (sheetHeight / 2) / objectHeight * 0.8

    Dim scaleFactor As Double
    scaleFactor = IIf(scaleX < scaleY, scaleX, scaleY)

    ' Define points for the views
    Dim oPoint1 As Point2d
    Dim oPoint2 As Point2d
    Dim oPoint3 As Point2d
    Set oPoint1 = oTG.CreatePoint2d(sheetWidth / 6, sheetHeight / 2) ' Left view
    Set oPoint2 = oTG.CreatePoint2d(sheetWidth / 2, sheetHeight / 2) ' Center view
    Set oPoint3 = oTG.CreatePoint2d(5 * sheetWidth / 6, sheetHeight / 2) ' Right view

    ' Add views
    Set oView = oSheet.DrawingViews.AddBaseView(oDoc, oPoint1, scaleFactor, kFrontViewOrientation, kHiddenLineRemovedDrawingViewStyle)
    Set oView1 = oSheet.DrawingViews.AddBaseView(oDoc, oPoint2, scaleFactor, kRightViewOrientation, kHiddenLineRemovedDrawingViewStyle)
    Set oView2 = oSheet.DrawingViews.AddBaseView(oDoc, oPoint3, scaleFactor * 0.5, kIsoBottomRightViewOrientation, kShadedDrawingViewStyle)
    
    AddHorizontalChainDimensionSet oSheet
    Call ChgTitleBlkDef(oDrawDoc)

    ' Save the drawing
    Dim drawingFilePath As String
    drawingFilePath = folderPath & "REVERSE ENGINEERING FILES\05 CAD-STEP\" & fileName & ".idw"
    oDrawDoc.SaveAs drawingFilePath, True
    MsgBox "Drawing saved as " & drawingFilePath
End Sub
Sub AddHorizontalChainDimensionSet(oSheet As Inventor.sheet)
    ' Set a reference to the transient geometry and object collections
    Dim oTransGeom As TransientGeometry
    Set oTransGeom = ThisApplication.TransientGeometry
    Dim oIntentCollection As ObjectCollection
    Set oIntentCollection = ThisApplication.TransientObjects.CreateObjectCollection

    ' Iterate through all drawing views to find the one with edges
    Dim oDrawingView As DrawingView
    Dim oViewWithEdges As DrawingView
    For Each oDrawingView In oSheet.DrawingViews
        If oDrawingView.DrawingCurves.Count > 0 Then
            Set oViewWithEdges = oDrawingView
            Exit For
        End If
    Next

    ' Check if a view with edges was found
    If oViewWithEdges Is Nothing Then
        MsgBox "No drawing view with edges found."
        Exit Sub
    End If

    ' Iterate through all drawing curves in the found drawing view
    Dim oDrawingCurve As DrawingCurve
    For Each oDrawingCurve In oViewWithEdges.DrawingCurves
        Dim oDimIntent As GeometryIntent
        Set oDimIntent = oSheet.CreateGeometryIntent(oDrawingCurve)
        Call oIntentCollection.Add(oDimIntent)
    Next

    ' Determine the placement point (adjust the Y coordinate based on your view)
    Dim oPlacementPoint As Point2d
    Set oPlacementPoint = oTransGeom.CreatePoint2d(oViewWithEdges.Left, oViewWithEdges.Center.Y - 8)

    ' Set a reference to the chain dimension sets collection
    Dim oChainDimSets As ChainDimensionSets
    Set oChainDimSets = oSheet.DrawingDimensions.ChainDimensionSets

    ' Create a horizontal dimension set
    Dim oChainDimSet As ChainDimensionSet
    Set oChainDimSet = oChainDimSets.Add(oIntentCollection, oPlacementPoint, kHorizontalDimensionType)
End Sub
Sub ChgTitleBlkDef(oDrawDoc As DrawingDocument)
    Dim colTitleBlkDefs As TitleBlockDefinitions
    Set colTitleBlkDefs = oDrawDoc.TitleBlockDefinitions

    Dim objTitleBlkDef As TitleBlockDefinition
    Dim found As Boolean
    found = False

    
    Debug.Print "Available Title Block Definitions:"
    For Each objTitleBlkDef In colTitleBlkDefs
        Debug.Print objTitleBlkDef.Name
        If objTitleBlkDef.Name = "custome title book" Then
            found = True
            Exit For
        End If
    Next objTitleBlkDef

    If Not found Then
        MsgBox "custom title block TitleBlockDefinition not found!"
        Set colTitleBlkDefs = Nothing
        Set objTitleBlkDef = Nothing
        Exit Sub
    End If

    
    Dim objDrwSketch As DrawingSketch
    Call objTitleBlkDef.Edit(objDrwSketch)

    Dim colTextBoxes As TextBoxes
    Set colTextBoxes = objDrwSketch.TextBoxes

    Dim objTextBox As TextBox
    For Each objTextBox In colTextBoxes
        If objTextBox.Text = "<PROJECT ID>" And projectCode1 <> "" Then
            objTextBox.Text = projectCode1
        ElseIf objTextBox.Text = "<ENGINEER>" And projectCode2 <> "" Then
            objTextBox.Text = projectCode2
        ElseIf objTextBox.Text = "<MAKER>" And projectCode3 <> "" Then
            objTextBox.Text = projectCode3
        ElseIf objTextBox.Text = "<TYPE>" And projectCode4 <> "" Then
            objTextBox.Text = projectCode4
        ElseIf objTextBox.Text = "<PART>" And projectCode5 <> "" Then
            objTextBox.Text = projectCode5
        ElseIf objTextBox.Text = "<SPEC>" And projectCode6 <> "" Then
            objTextBox.Text = projectCode6
        ElseIf objTextBox.Text = "<PART NR>" And projectCode7 <> "" Then
            objTextBox.Text = projectCode7
        ElseIf objTextBox.Text = "<ORIGIN>" And projectCode8 <> "" Then
            objTextBox.Text = projectCode8
        ElseIf objTextBox.Text = "<CONDITION>" And projectCode9 <> "" Then
            objTextBox.Text = projectCode9
        ElseIf objTextBox.Text = "<SCOPE>" And projectCode10 <> "" Then
            objTextBox.Text = projectCode10
        End If
    Next objTextBox

    Call objTitleBlkDef.ExitEdit(True)

    ' Clean up
    Set colTitleBlkDefs = Nothing
    Set objTitleBlkDef = Nothing
    Set objDrwSketch = Nothing
    Set colTextBoxes = Nothing
    Set objTextBox = Nothing

    Beep
End Sub
Sub CaptureDrawingViewAsImage(imgFilePath As String)
    ' Declare variables
    Dim oApp As Application
    Dim oView As View
    Dim oOptions As NameValueMap

    ' Set the application
    Set oApp = ThisApplication

    ' Get the active view
    Set oView = oApp.ActiveView

    oApp.ActiveView.Update

    ' Create a new NameValueMap Object for options
    Set oOptions = oApp.TransientObjects.CreateNameValueMap

    ' Export the view as an image
    Call oView.SaveAsBitmapWithOptions(imgFilePath, 800, 600, oOptions) ' Adjusted size to 800x600
End Sub
Sub CreateWordDocument(ByVal folderPath As String, ByVal code As String, ByVal projectCode1 As String, ByVal projectCode2 As String, ByVal projectCode3 As String, ByVal projectCode4 As String, ByVal projectCode5 As String, ByVal projectCode6 As String, ByVal projectCode7 As String, ByVal projectCode8 As String, ByVal projectCode9 As String, ByVal projectCode10 As String)
    Dim wordApp As Object
    Dim wordDoc As Object
    Dim filePath As String
    Dim remarksFolderPath As String
    Dim imgFilePath As String
    Dim oDoc As Inventor.Document
    Set oDoc = GetSelectedDocument()
    

    ' Folder to save the Word document
    remarksFolderPath = folderPath & "REVERSE ENGINEERING FILES\04 REMARKS\"

    ' Check if the folder exists
    If Dir(remarksFolderPath, vbDirectory) = "" Then
        MsgBox "REMARKS folder does not exist. No Word document saved in REMARKS folder.", vbExclamation
        Exit Sub
    End If

    ' Path and name of the Word document
    filePath = remarksFolderPath & projectCode1 & ".docx"
    'filePath = "C:\Users\orspa\Desktop\Document.docx"
    imgFilePath = Environ("TEMP") & "\DrawingViewImage.bmp"
    
    ' Capture the drawing view as an image
    Call CaptureDrawingViewAsImage(imgFilePath)
    
    ' Create a new instance of Word application
    On Error Resume Next
    Set wordApp = CreateObject("Word.Application")
    On Error GoTo 0

    If wordApp Is Nothing Then
        MsgBox "Word application is not available. Please ensure Microsoft Word is installed."
        Exit Sub
    End If

    ' Make the Word application visible
    wordApp.Visible = True
    
    ' Create a new Word document
    Set wordDoc = wordApp.Documents.Add
    
  ' Get the bounding box dimensions using your provided logic
    Dim oBox As Box
    Set oBox = oDoc.ComponentDefinition.RangeBox
    Dim minX As Double, minY As Double, minZ As Double
    Dim maxX As Double, maxY As Double, maxZ As Double
    minX = oBox.MinPoint.X
    minY = oBox.MinPoint.Y
    minZ = oBox.MinPoint.Z
    maxX = oBox.MaxPoint.X
    maxY = oBox.MaxPoint.Y
    maxZ = oBox.MaxPoint.Z
    Dim length As Double, width As Double, height As Double
    length = (maxX - minX) * 10 ' Convert cm to mm
    width = (maxY - minY) * 10 ' Convert cm to mm
    height = (maxZ - minZ) * 10 ' Convert cm to mm
    
    ' Add text content to the Word document first
    With wordDoc.Content
        .Paragraphs.Add
        .Paragraphs.Last.Range.ParagraphFormat.Alignment = wdAlignParagraphLeft
        .Text = "Project Code: " & projectCode1 & vbCrLf & _
                "Engineer: " & projectCode2 & vbCrLf & _
                "Maker: " & projectCode3 & vbCrLf & _
                "Type: " & projectCode4 & vbCrLf & _
                "Part: " & projectCode5 & vbCrLf & _
                "Spec: " & projectCode6 & vbCrLf & _
                "Part NR: " & projectCode7 & vbCrLf & _
                "Origin: " & projectCode8 & vbCrLf & _
                "Condition: " & projectCode9 & vbCrLf & _
                "Scope: " & projectCode10 & vbCrLf & _
                "Bounding Box Dimensions (mm): " & vbCrLf & _
                "Length: " & length & " mm" & vbCrLf & _
                "Width: " & width & " mm" & vbCrLf & _
                "Height: " & height & " mm" & vbCrLf & _
                "FINANCIAL ANALYSIS" & vbCrLf & _
                "Material Cost: " & materialCostGlobal & " Euro" & vbCrLf & _
                "Reverse Engineering Cost: " & reverseEngineeringCostGlobal & " Euro" & vbCrLf & _
                "Set Up Cost: " & setUpCostGlobal & " Euro" & vbCrLf & _
                "Machining Cost: " & machiningCostGlobal & " Euro" & vbCrLf & _
                "Total Cost: " & totalCostGlobal & " Euro"
    End With
    ' save
    On Error GoTo ErrorHandler
    wordDoc.SaveAs2 fileName:=filePath

    ' Insert the image after text
    Dim oShape As InlineShape
    With wordDoc.Content
        .Paragraphs.Add
        .Paragraphs.Last.Range.ParagraphFormat.Alignment = wdAlignParagraphCenter ' Center the image
        Set oShape = .InlineShapes.AddPicture(fileName:=imgFilePath, LinkToFile:=False, SaveWithDocument:=True, Range:=.Paragraphs.Last.Range)
        oShape.LockAspectRatio = msoTrue
        oShape.width = wordApp.CentimetersToPoints(15)
        
    End With

    ' Save the document again
    wordDoc.Save

   
    wordDoc.Close

    wordApp.Quit
    Set wordDoc = Nothing
    Set wordApp = Nothing

    MsgBox "Word document created and saved as " & filePath
    Exit Sub

ErrorHandler:
    MsgBox "An error occurred while saving the Word document: " & Err.Description
    If Not wordDoc Is Nothing Then wordDoc.Close False
    If Not wordApp Is Nothing Then wordApp.Quit
    Set wordDoc = Nothing
    Set wordApp = Nothing
End Sub
--------------------------------------------------------------------------


Option Explicit

Sub ShowSaveOptionsForm()
    FORM1.Show
End Sub

' Module: modDocumentSelection
Public Function GetSelectedDocument() As Inventor.Document
    Dim selectedDocName As String
    Dim doc As Inventor.Document
    Dim i As Integer

    ' Get the selected document name from frmActiveDoc
   If frmActiveDocCust.ComboBox1.ListIndex <> -1 Then
        selectedDocName = frmActiveDocCust.ComboBox1.Value
    ElseIf frmActiveDocTurbo.ComboBox1.ListIndex <> -1 Then
        selectedDocName = frmActiveDocTurbo.ComboBox1.Value
    Else
        MsgBox "No document selected in either form.", vbExclamation
        Exit Function
    End If

    ' Find the document with the matching name
    For i = 1 To ThisApplication.Documents.Count
        If ThisApplication.Documents(i).DisplayName = selectedDocName Then
            Set doc = ThisApplication.Documents(i)
            Exit For
        End If
    Next i

    Set GetSelectedDocument = doc
End Function
--------------------------------------------------------------------------
' Module1
Option Explicit

Global projectCode1 As String
Global projectCode2 As String
Global projectCode3 As String
Global projectCode4 As String
Global projectCode5 As String
Global projectCode6 As String
Global projectCode7 As String
Global projectCode8 As String
Global projectCode9 As String
Global projectCode10 As String
Global uFolderPath As String
Global code As String


Global totalCostGlobal As Double
Global materialCostGlobal As Double
Global reverseEngineeringCostGlobal As Double
Global setUpCostGlobal As Double
Global machiningCostGlobal As Double










