Jump to content

Adding A shortcut to VBA code


Zac Davis

Recommended Posts

Hi

have this awesome code found on Autodesk Forum its a vba code to rename parts within assembly , its delete all entities if the desired part if you choose yes

Public Sub Save_Replace_All()
   On Error GoTo err:
   'Checks if open document is assembly
   If (ThisApplication.ActiveDocument.DocumentType <> kAssemblyDocumentObject) Then
       MsgBox "This is NOT an assembly document!", vbExclamation
       Exit Sub
   End If
   
   Dim oPart As ComponentOccurrence
   Dim opartdoc As Document
   Dim NewFileName As String
   Dim NewFilePath As String
   
   Set oPart = ThisApplication.CommandManager.Pick(kAssemblyOccurrenceFilter, "Select the part to save and replace all.")
   If (oPart Is Nothing) Then Exit Sub
   Set opartdoc = oPart.Definition.Document

  'Create a new FileDialog object.
   Dim oFileDlg As FileDialog
   Call ThisApplication.CreateFileDialog(oFileDlg)

   'check file type and set dialog filter
   If opartdoc.DocumentType = kPartDocumentObject Then
       oFileDlg.Filter = "Autodesk Inventor Part Files (*.ipt)|*.ipt"
   ElseIf opartdoc.DocumentType = kAssemblyDocumentObject Then
       oFileDlg.Filter = "Autodesk Inventor Assembly Files (*.iam)|*.iam"
   End If

   oFileDlg.InitialDirectory = ThisApplication.FileLocations.Workspace
   oFileDlg.filename = filename(opartdoc.FullFileName)
   oFileDlg.CancelError = True
   
   On Error Resume Next
   
   Call oFileDlg.ShowSave

   If err Then
       Exit Sub
   ElseIf oFileDlg.filename <> "" Then
       NewFilePath = oFileDlg.filename
   End If

   ThisApplication.SilentOperation = True
   Call opartdoc.SaveAs(NewFilePath, True)
   ThisApplication.SilentOperation = False

   Dim fd As DocumentDescriptor
   For Each fd In ThisApplication.ActiveEditDocument.ReferencedDocumentDescriptors
       If (fd.FullDocumentName = opartdoc.FullFileName) Then fd.ReferencedFileDescriptor.ReplaceReference (NewFilePath)
   Next

   'Comment the following to stop the delete prompt, use a single quote to comment or just delete the damn line really lol
   'To autodelete without asking replace next line with "Kill opartdoc.FullFileName" (no quotes)
   If MsgBox("Delete original part?", vbYesNo + vbQuestion) = vbYes Then Kill opartdoc.FullFileName
   
   Exit Sub
err:
   ThisApplication.SilentOperation = False
   On Error GoTo 0
   Resume
End Sub

Function filename(spth As String)
   'Returns filename with extension from full path
   If spth <> "" Then
       filename = Mid(spth, InStrRev(spth, "\", Len(spth)) + 1, Len(spth))
   Else:
       filename = ""
   End If
End Function

 

now i want to create a shortcut for it , that it will Appear automatically in every session , up to now i was going to tools> Macros> choose down in the dialog box "macros in : all apllication projects" .

then i run the macro

how can i make more efficient and save this settings for future use if ever reinstall Inventor ?

Link to comment
Share on other sites

I would love to help you out Zac but I have never got into Macros. Looks all technical to me and sends my brain to mush!!. When starting out in Autocad I started learning Lisp but gave up. Spent too much time at work playing. I'm sure someone on here will find this post and offer some assistance.

Link to comment
Share on other sites

  • 2 weeks later...

ok , found a way . if someone is intrested

the vb module must be in the apllication projects (default.ivb) , see imageps.PNG

 

 

 

 

then in invetor you go to "Tools" tab> Customize> second tab "Keyboard" rule down and choose "macros" then on the left type the shotrcut sa.jpg

Link to comment
Share on other sites

Cheers for posting the answer Zac, we don't have many active members that are into Inventor's VBA side of of the program but we do have many non members browse this forum every day that you don't see, so I'm sure it will be of help to may that pop in.;)

Link to comment
Share on other sites

This is not an Inventor answer but in Autocad you can use a lisp shortcut to load the VBA program then call the required module. For you I would type SRA

(defun c:sra ()
(vl-vbaload "S:/AutoDESK/VBA/Deault.dvb") (vl-vbarun "Save_replace_all") 
)

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...