Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

  • Congratulations waross on being selected by the Eng-Tips community for having the most helpful posts in the forums last week. Way to Go!

How To Limit Macro To Currently Opened Product

Status
Not open for further replies.

jzecha

Aerospace
Jan 20, 2016
235
US
I have a Macro that saves the Products/Parts as their in session part number.
It works great except if I have two products open it switches windows and saves both of them.

How do I modify this Macro to only work on the current open product and keep it from switching to the next open document?

Code:
Sub CATMain()

Dim noSymPartName As String
Dim noSymProductName As String
Dim newCharacter As String
newCharacter = "_"

CATIA.DisplayFileAlerts = False

Set oDocs = CATIA.Documents
docPath = oDocs.Item(1).Path
changePath = MsgBox("Current save location is: " & docPath & " Would you like to change file path?", vbYesNo)

If changePath = vbYes Then

Const WINDOW_HANDLE = 0
Const NO_OPTIONS = &H0001
'Const File_Path = "\\GTFS1\Share\Data\"
Const File_Path = 17
Set objShell = CreateObject("Shell.Application") 

'Set objFolder = objShell.BrowseForFolder _
' (WINDOW_HANDLE, "Select a folder:", NO_OPTIONS, " \\GTFS1\Share\Data\") 

Set objFolder = objShell.BrowseForFolder _
(WINDOW_HANDLE, "Select a folder:", NO_OPTIONS, File_Path)

Set objFolderItem = objFolder.Self 
'objPath = objFolderItem.Path
docPath = objFolderItem.Path

MsgBox docPath,,"Save Location"

'Set objFolderItem = CreateObject("Scripting.FileSystemObject")

'docPath = InputBox("Enter new file path", "File path")

End If

CATIA.RefreshDisplay = False

For x = 1 To oDocs.Count

If TypeName(oDocs.Item(x)) = "ProductDocument" Then
Set oDoc1 = oDocs.Item(x)
Set oProduct1 = oDoc1.Product

    noSymProductName = oProduct1.PartNumber 'get the current PartNumber

	noSymProductName = Replace(noSymProductName,".","")
	noSymProductName = Replace(noSymProductName,",","")
	noSymProductName = Replace(noSymProductName,"(","")
	noSymProductName = Replace(noSymProductName,")","")
	noSymProductName = Replace(noSymProductName,"/","")
	noSymProductName = Replace(noSymProductName,"\","")
	noSymProductName = Replace(noSymProductName,"#","")
	noSymProductName = Replace(noSymProductName,"$","")
	noSymProductName = Replace(noSymProductName,"%","")
	noSymProductName = Replace(noSymProductName,"*","")


oDoc1.SaveAs docPath & "\" & noSymProductName & ".CATProduct"
End If
Next 'x

For v = 1 To oDocs.Count

If TypeName(oDocs.Item(v)) = "PartDocument" Then
Set oDoc2 = oDocs.Item(v)
Set oPart1 = oDoc2.Product


    noSymPartName = oPart1.PartNumber 'get the current PartNumber

	noSymPartName = Replace(noSymPartName,".","")
	noSymPartName = Replace(noSymPartName,",","")
	noSymPartName = Replace(noSymPartName,"(","")
	noSymPartName = Replace(noSymPartName,")","")
	noSymPartName = Replace(noSymPartName,"/","")
	noSymPartName = Replace(noSymPartName,"\","")
	noSymPartName = Replace(noSymPartName,"#","")
	noSymPartName = Replace(noSymPartName,"$","")
	noSymPartName = Replace(noSymPartName,"%","")
	noSymPartName = Replace(noSymPartName,"*","")


oDoc2.SaveAs docPath & "\" & noSymPartName & ".CATPart"

On Error Resume Next
End If
Next 'v


CATIA.RefreshDisplay = True

Msgbox "Save Finished!",,"SAVE FINISH!" 

Msgbox "YOU HAVE TO USE SAVE MANAGEMENT NOW!",,"YOU HAVE TO USE SAVE MANAGEMENT NOW!" 

End Sub
 
Replies continue below

Recommended for you

it's because you are asking for ALL document loaded in CATIA session:
Set oDocs = CATIA.Documents
CATIA.ActiveDocument is the currently shown Product/Part...
so you'd need to loop thru your structure and get the correct document out.

regards,
LWolf
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top