from vba to vb.net

from vba to vb.net

^_^clovis^_^
Advocate Advocate
831 Views
2 Replies
Message 1 of 3

from vba to vb.net

^_^clovis^_^
Advocate
Advocate

Hello guys,

I've written this small function and I'd like to transpose it to vb.net.

'm planning to move all my functions to vb.net and want to use this one as a first howto.

A link to a howto would also be great! Where could I find some howto?

 

Thanks

 

 

Public Sub WP_Off()
'Visible Work Plan in parts OFF
    Dim invDoc As Document
    Set invDoc = ThisApplication.ActiveDocument
    
    If invDoc.DocumentType = kPartDocumentObject Then
 
        Dim pcd As PartComponentDefinition
        Set pcd = invDoc.ComponentDefinition
    
        Dim wpls As WorkPlanes
        Set wpls = pcd.WorkPlanes
        
        Dim owas As WorkAxes
        Set owas = pcd.WorkAxes
        
        Dim owpos As WorkPoints
        Set owpos = pcd.WorkPoints
        
        Dim owa As WorkAxis
        Dim owp As WorkPlane
        Dim owpo As WorkPoint
        
        For Each owp In wpls
            If owp.Visible = True Then
               owp.Visible = False
            End If
        Next
        For Each owa In owas
            If owa.Visible = True Then
               owa.Visible = False
            End If
        Next
        For Each owpo In owpos
            If owpo.Visible = True Then
               owpo.Visible = False
            End If
        Next
        
        
        Else: Exit Sub
    End If

End Sub

 

 

 

0 Likes
Accepted solutions (1)
832 Views
2 Replies
Replies (2)
Message 2 of 3

ekinsb
Alumni
Alumni
Accepted solution

This Sub will be quite simple to convert but there are a few things you'll need to fix VB.NET program.  The first step is to just copy and paste it into a VB.NET program.  You'll see a bunch of problems highlighted inside Visual Studio.  For example, the Set statement is not longer needed or even supported so you need to delete those.  (I think depending on the version of Visual Studio is might do this automatically.)  Where you declare variables that are an Inventor type you'll need to either full declare them or add a an Imports statement at the top of your program.  For example:

 

   Dim wpls As WorkPlanes

 

becomes

 

   Dim wpls As Inventor.WorkPlanes

 

Or you can add "Imports Inventor" at the very top of the code and then the original statement will still work.

 

VB.NET also requires that enum values be fully qualified so

 

   If invDoc.DocumentType = kPartDocumentObject Then

 

becomes

 

   If invDoc.DocumentType = DocumentTypeEnum.kPartDocumentObject Then

 

Intellisense can help with these.  Just type in the equals again and it will display a list of valid enums and picking one will give you the fully qualified name.

 

As I said, a lot of the problems Visual Studio will point out to you and they're all easy to fix when you know what to do.  There are some other differences that are harder to find.  I presented a class at AU a few years ago about converting VBA programs to Add-Ins and discussed this a bit in there.  I've attached the paper I wrote for that class.  Hopefully it will help.

 

I think it's best to recognize up front, which it seems you already have, that VBA and VB.NET are different languages and it takes a little bit to get used to but once you do it's difficult to go back.


Brian Ekins
Inventor and Fusion 360 API Expert
Mod the Machine blog
Message 3 of 3

^_^clovis^_^
Advocate
Advocate

thanks a lot!!

0 Likes