Macro inserts a body into a new part, gives part a specified template, & inserts a best fit bounding box. VBA contains an enter sendkeys command - any clue why this could occur. also it appears to clear my copy/paste clipboard when ran.
If anyone can see the same results, point out an issue in the below code, it would be appreciated. If anyone can run it, and doesn't get these errors, that at least points me to look elsewhere outside the macro for my issue.
thanks!
//
'04/03/25 - CNP Dim sw_App As Object Dim sw_Model As SldWorks.ModelDoc2 Dim sw_Body As SldWorks.Body2 Dim sw_Doc_Type As Variant Dim Bool_Stat As Boolean Dim sw_Init_Model_Name As String Dim sw_User_Spcfd_Sffx As String Dim sw_Slctn_Type As Long Dim sw_Parent_Path As String Dim sw_Path_Step_1 As String Dim sw_Path_Step_2 As String Dim sw_Export_Path As String Dim sw_Slctn_Mngr As SldWorks.SelectionMgr Dim sw_Slctn_Mngr_Data As SldWorks.SelectData Dim Nmbr_Slctd As Long Dim Long_Stat As Long Dim sw_Feature As SldWorks.Feature Dim sw_FT_Mngr As SldWorks.FeatureManager Dim sw_Enity As SldWorks.Entity Dim sw_Wrnngs As Long Dim sw_Errors As Long ' Sub Export_Mllwrk_Body_To_Part_File() Set sw_App = Application.SldWorks Set sw_Model = sw_App.ActiveDoc Set sw_Slctn_Mngr = sw_Model.SelectionManager Set sw_Entity = sw_Slctn_Mngr.GetSelectedObject6(1, -1) Nmbr_Slctd = sw_Model.SelectionManager.GetSelectedObjectCount sw_Doc_Type = sw_Model.GetType '001_S ___________________________________________________________________________________________ 'Check if file is an assembly. if not, message user error and end macro If sw_Doc_Type <> swDocPART Then MsgBox "!!! Must run macro in a part/sldprt document. !!!" End 'End macro Else 'Do nothing End If 'Check if number of selections is = to 1 If Nmbr_Slctd > 1 Then 'Message error to user & end macro MsgBox "!!! SELECT ONLY ONE, SINGULAR BODY FOR EXPORT. !!!" End 'Message error to user & end macro ElseIf Nmbr_Slctd < 1 Then MsgBox "!!! NO BODY SELECTED. !!!" End 'Do nothing End If '001_E ___________________________________________________________________________________________ '// '002_S ___________________________________________________________________________________________ 'Check if selection is a body feature. If sw_Entity.GetType <> swSOLIDBODIES Then MsgBox "!!! SELECTION IS NOT A BODY ENTITY." & vbNewLine & vbNewLine & "SELECT A BODY FROM THE CUT LIST FOLDER. !!!" End Else 'Do nothing End If 'Modify path name and body name to generate an export path. sw_Parent_Path = sw_Model.GetPathName Set sw_Body = sw_Slctn_Mngr.GetSelectedObject6(1, -1) Part_Name_Prompt: Prompt = "Type Part # Suffix - Examples 'W04', 'W27', 'S01'. " sw_User_Spcfd_Sffx = InputBox(Prompt) If sw_User_Spcfd_Sffx = "" Then GoTo Part_Name_Prompt Else sw_Path_Step_1 = Replace(sw_Parent_Path, ".sldprt", "") sw_Path_Step_2 = Replace(sw_Path_Step_1, ".SLDPRT", "") sw_Export_Path = sw_Path_Step_2 & "-" & sw_User_Spcfd_Sffx & ".sldprt" End If 'Export body into a new part. Bool_Stat = sw_Model.SaveToFile3(sw_Export_Path, 1, swCutListTransferOptions_None, True, "C:\SWVault\Templates\Macros\INSERT TO STD SWOOD PRT.PRTDOT", sw_Errors, sw_Wrnngs) 'Set New Part As Active Set sw_Model = sw_App.ActiveDoc sw_App.ActivateDoc2 sw_Export_Path, False, Long_Stat 'Add Best Fit Bounding Box To Part. Set sw_FT_Mngr = sw_Model.FeatureManager Dim sw_Feat As Object Dim sw_FT_Data As Object Set sw_FT_Data = sw_FT_Mngr.CreateDefinition(swFeatureNameID_e.swFmBoundingBox) sw_FT_Data.IncludeHiddenBodies = False sw_FT_Data.IncludeSurfaces = False sw_FT_Data.ReferenceFaceOrPlane = 1 Set sw_Ft = sw_FT_Mngr.CreateFeature(sw_FT_Data) sw_Model.ClearSelection2 True 'Turn of Display Bounding Boxes In Display Settings. Bool_Stat = sw_Model.SetUserPreferenceToggle(swUserPreferenceToggle_e.swViewDispGlobalBBox, False) 'Sendkeys sends ENTER press to confirm feature name on box creation SendKeys "{ENTER}", True End Sub