Sub ReLink()
Dim DrawingDoc As DrawingDocument
Dim DrawingSheets As drawingSheets
Dim DrawingSheet As drawingSheet
Dim DrawingView As DrawingView
Dim DrawingTexts As DrawingTexts
Dim Text As DrawingText
Dim Factory As Factory2D
Dim Point As Point2D
Dim Line As Line2D
Dim Circle As Circle2D
Dim Selection As Selection
Dim GeometricElements As GeometricElements
Dim PartName As String
Dim PartName2 As String
Dim PartFile As String
Dim DrawingName As String
Dim FilePath As String
Dim Documents As Documents
Set Documents = CATIA.Documents
Dim PartDocument As PartDocument
Dim Product As ProductDocument
Set DrawingDoc = CATIA.ActiveDocument
Set DrawingSheets = DrawingDoc.Sheets
Set Selection = DrawingDoc.Selection
Set DrawingSheet = DrawingSheets.ActiveSheet
Set DrawingView = DrawingSheet.Views.ActiveView
Set DrawingTexts = DrawingView.Texts
Set Factory = DrawingView.Factory2D
Set GeometricElements = DrawingView.GeometricElements
Set MyDrawingDoc = CATIA.ActiveDocument
MyDrawingDoc.Sheets.Item(1).Activate
Dim NumberOfViews As Integer
Dim Windows As Windows
Set Windows = CATIA.Windows
DrawingName = CATIA.ActiveWindow.Name
NumberOfViews = CATIA.ActiveDocument.Sheets.ActiveSheet.Views.Count
If NumberOfViews > 2 Then
' Display file open dialog
FilePath = CATIA.FileSelectionBox("Choose the file you want to relink to this drawing", "*.*", CatFileSelectionModeOpen)
If FilePath = "" Then Exit Sub
Dim a As Integer
Dim b As Integer
a = InStr(FilePath, "CATProduct")
b = InStr(FilePath, "CATPart")
If a > 0 Then
Product = Documents.Open(FilePath)
End If
If b > 0 Then
Set PartDocument = Documents.Open(FilePath)
End If
Dim NumberOfWindows As Integer
NumberOfWindows = Windows.Count
Dim WindowsArray()
ReDim Preserve WindowsArray(NumberOfWindows)
For i = 1 To NumberOfWindows
WindowsArray(i) = Windows.Item(i).Name
If WindowsArray(i) = DrawingName Then
Dim SpecsAndGeomWindow As SpecsAndGeomWindow
Set SpecsAndGeomWindow = Windows.Item(WindowsArray(i))
SpecsAndGeomWindow.Activate
Component_Display = "Ok"
End If
Next
For i = 3 To NumberOfViews
Set DrawingView = CATIA.ActiveDocument.Sheets.ActiveSheet.Views.Item(i)
' access links collection
Dim GenerativeLinks As DrawingViewGenerativeLinks
Dim LinkedDocument
DrawingView.GenerativeLinks.RemoveAllLinks
If a > 0 Then
DrawingView.GenerativeLinks.AddLink Product.Product
End If
If b > 0 Then
DrawingView.GenerativeLinks.AddLink PartDocument.Product
End If
'DrawingView.GenerativeLinks.AddLink
Next
Else
MsgBox ("There are no views to change the links for")
End If
End Sub