vba userform position throws run time error

vba userform position throws run time error

ThatWorksForMe
Advocate Advocate
2,540 Views
10 Replies
Message 1 of 11

vba userform position throws run time error

ThatWorksForMe
Advocate
Advocate

Hey,

I have created a small userform in vba.

Now I want to position the form with the following code:

Private Sub UserForm_Initialize()
    With UserForm1
        .StartUpPosition = 0
        .Left = Application.Left + (0.5 * Application.Width) - (0.5 * .Width)
        .Top = Application.Top + (0.5 * Application.Height) - (0.5 * .Height)
    End With

...
End Sub

Everytime I ran the code I get a run time error 424 with "Object is required".

I have no clue what to do.

Maybe today is not my day.

PDM/CAD Administrator
Using Inventor/Vault 2024.3
0 Likes
Accepted solutions (1)
2,541 Views
10 Replies
Replies (10)
Message 2 of 11

frederic.vandenplas
Collaborator
Collaborator

Hi, the problem is that you use 

UserForm_Initialize()

and in your code you use 

 

With UserForm1

So it should be UserForm1_Initialize or With UserForm 

If you think this answer fullfilled your needs, improved your knowledge or leads to a solution,
please feel free to "kudos"
0 Likes
Message 3 of 11

ThatWorksForMe
Advocate
Advocate

I am sorry, that was not the solution.

Any other idea?

 

*EDIT*

Here is the code. Maybe this will help.

PDM/CAD Administrator
Using Inventor/Vault 2024.3
0 Likes
Message 4 of 11

bradeneuropeArthur
Mentor
Mentor

remove the above code from your userform and put it in the subroutine, like below:

Private Sub UserForm_Initialize()
'    With UserForm1
'        .StartUpPosition = 0
'        .Left = Application.Left + (0.5 * Application.Width) - (0.5 * .Width)
'        .Top = Application.Top + (0.5 * Application.Height) - (0.5 * .Height)
'    End With
End Sub

put it here:

Public Sub x()
UserForm1.Show
Dim a As Application
Set a = ThisApplication
 With UserForm1
        .StartUpPosition = 0
        
        '.Left = Application.Left + (0.5 * Application.Width) - (0.5 * .Width)
        .Left = a.Left + (0.5 * a.Width) - (0.5 * .Width)
        .Top = a.Top + (0.5 * a.Height) - (0.5 * .Height)
    End With
End Sub

Regards,

Arthur Knoors

Autodesk Affiliations & Links:
blue LinkedIn LogoSquare Youtube Logo Isolated on White Background


Autodesk Software:Inventor Professional 2025 | Vault Professional 2024 | Autocad Mechanical 2024
Programming Skills:Vba | Vb.net (Add ins Vault / Inventor, Applications) | I-logic
Programming Examples:
Drawing List!|
Toggle Drawing Sheet!|
Workplane Resize!|
Drawing View Locker!|
Multi Sheet to Mono Sheet!|
Drawing Weld Symbols!|
Drawing View Label Align!|
Open From Balloon!|
Model State Lock!
Posts and Ideas:
My Ideas|
Dimension Component!|
Partlist Export!|
Derive I-properties!|
Vault Prompts Via API!|
Vault Handbook/Manual!|
Drawing Toggle Sheets!|
Vault Defer Update!

! For administrative reasons, please mark a "Solution as solved" when the issue is solved !


 


EESignature

0 Likes
Message 5 of 11

ThatWorksForMe
Advocate
Advocate

Hey bradeneurope,

thanks for your reply, but that change doesnt work either.

I attached all my files again including a screenshot from the userform settings.

It seems like the vars StartUpPosition,Left and Top arent writeable. If I change the settings "0-Manual" to "1-CenterOwner" the window position changes. Changing the position via code doesnt take place.

 

For explanation. I got 2 screens. On the left (main screen) is Inventor running and on the other screen is the vba editor running. If both application are running on the main screen the code works.

Can you run the form on your system? Is there the same effect?

Thanks

PDM/CAD Administrator
Using Inventor/Vault 2024.3
0 Likes
Message 6 of 11

bradeneuropeArthur
Mentor
Mentor
Accepted solution

 

Public Sub x()
UserForm.Show
Dim a As Application
Set a = ThisApplication
With UserForm
'MsgBox a.ActiveView.Left
'.StartUpPosition = 0
.Left = a.ActiveView.Left + (0.5 * a.ActiveView.Width) - (0.5 * a.ActiveView.Width)
.Top = a.ActiveView.Top + (0.5 * a.ActiveView.Height) - (0.5 * a.ActiveView.Height)
End With
End Sub

With this is works correct...

 

Regards,

Autodesk Software: Inventor Professional 2018 | Vault Professional 2018 | Autocad Mechanical 2018
Programming Skills: Vba | Vb.net (Add ins Vault / Inventor, Applications) | I-logic
Dimension Component! | Partlist Export! | Derive I-properties! | Vault Prompts Via API! | Vault Handbook/Manual!
Drawing Toggle Sheets! | Vault Defer Update!

! For administrative reasons, please mark a "Solution as solved" when the issue is solved !

 

Regards,

Arthur Knoors

Autodesk Affiliations & Links:
blue LinkedIn LogoSquare Youtube Logo Isolated on White Background


Autodesk Software:Inventor Professional 2025 | Vault Professional 2024 | Autocad Mechanical 2024
Programming Skills:Vba | Vb.net (Add ins Vault / Inventor, Applications) | I-logic
Programming Examples:
Drawing List!|
Toggle Drawing Sheet!|
Workplane Resize!|
Drawing View Locker!|
Multi Sheet to Mono Sheet!|
Drawing Weld Symbols!|
Drawing View Label Align!|
Open From Balloon!|
Model State Lock!
Posts and Ideas:
My Ideas|
Dimension Component!|
Partlist Export!|
Derive I-properties!|
Vault Prompts Via API!|
Vault Handbook/Manual!|
Drawing Toggle Sheets!|
Vault Defer Update!

! For administrative reasons, please mark a "Solution as solved" when the issue is solved !


 


EESignature

0 Likes
Message 7 of 11

ThatWorksForMe
Advocate
Advocate
Public Sub x()

    UserForm.Show
    Dim a As Application
    Set a = ThisApplication
    With UserForm
        .StartUpPosition = 0
        If a.Left < 0 Then
            .Left = (0.5 * a.ActiveView.Width) - (0.5 * .Width)
        Else
            .Left = a.Left + (0.25 * a.ActiveView.Width) - (0.5 * .Width)
        End If
        .Top = (0.5 * a.ActiveView.Height) - (0.5 * .Height) - (0.5 * .Height)
    End With
    
End Sub

Thanks for your help again. 

As you can see my code is a bit more complex. I dont know at the moment why this is necessary for my system.

This codes works perfect if my Inventor is running on the main screen. When I push the window to the other screen the calculation shows the right result, but the window isnt positioned correct.

When I change the resolution of the screen, the userform is positioned on an other position like before. There is something not working well. I trie to figure out what it is.

 

PDM/CAD Administrator
Using Inventor/Vault 2024.3
0 Likes
Message 8 of 11

bradeneuropeArthur
Mentor
Mentor

the application does not know which screen you are working on...
So you better use the activeview......

Regards,

Autodesk Software: Inventor Professional 2018 | Vault Professional 2018 | Autocad Mechanical 2018
Programming Skills: Vba | Vb.net (Add ins Vault / Inventor, Applications) | I-logic
Dimension Component! | Partlist Export! | Derive I-properties! | Vault Prompts Via API! | Vault Handbook/Manual!
Drawing Toggle Sheets! | Vault Defer Update!

! For administrative reasons, please mark a "Solution as solved" when the issue is solved !

Regards,

Arthur Knoors

Autodesk Affiliations & Links:
blue LinkedIn LogoSquare Youtube Logo Isolated on White Background


Autodesk Software:Inventor Professional 2025 | Vault Professional 2024 | Autocad Mechanical 2024
Programming Skills:Vba | Vb.net (Add ins Vault / Inventor, Applications) | I-logic
Programming Examples:
Drawing List!|
Toggle Drawing Sheet!|
Workplane Resize!|
Drawing View Locker!|
Multi Sheet to Mono Sheet!|
Drawing Weld Symbols!|
Drawing View Label Align!|
Open From Balloon!|
Model State Lock!
Posts and Ideas:
My Ideas|
Dimension Component!|
Partlist Export!|
Derive I-properties!|
Vault Prompts Via API!|
Vault Handbook/Manual!|
Drawing Toggle Sheets!|
Vault Defer Update!

! For administrative reasons, please mark a "Solution as solved" when the issue is solved !


 


EESignature

0 Likes
Message 9 of 11

ThatWorksForMe
Advocate
Advocate

Your right. I will change that again.

Have you any idea why the calculated position isnt the same like the window position?

The resolution of the screen is 2560x1440 px. The calculated x-position is 1360 px, but the userform is positioned on the right screen. So the real x-position is definitely wrong.

PDM/CAD Administrator
Using Inventor/Vault 2024.3
0 Likes
Message 10 of 11

bradeneuropeArthur
Mentor
Mentor
maybe the activeview is the working area of inventor!

Regards,

Arthur Knoors

Autodesk Affiliations & Links:
blue LinkedIn LogoSquare Youtube Logo Isolated on White Background


Autodesk Software:Inventor Professional 2025 | Vault Professional 2024 | Autocad Mechanical 2024
Programming Skills:Vba | Vb.net (Add ins Vault / Inventor, Applications) | I-logic
Programming Examples:
Drawing List!|
Toggle Drawing Sheet!|
Workplane Resize!|
Drawing View Locker!|
Multi Sheet to Mono Sheet!|
Drawing Weld Symbols!|
Drawing View Label Align!|
Open From Balloon!|
Model State Lock!
Posts and Ideas:
My Ideas|
Dimension Component!|
Partlist Export!|
Derive I-properties!|
Vault Prompts Via API!|
Vault Handbook/Manual!|
Drawing Toggle Sheets!|
Vault Defer Update!

! For administrative reasons, please mark a "Solution as solved" when the issue is solved !


 


EESignature

0 Likes
Message 11 of 11

ThatWorksForMe
Advocate
Advocate

I thought of this also. I made 2 Screenshots.

001 - main screen

002 - second screen

the resolution is the same on both screens. as you can see the x-position isnt the same.

I tried 3 or 4 different codes to place the userform. Now I will center the userform on every screen independent on how the active application is positioned. I know this is not the best way, but it works for me.

Here is the code:

Option Explicit
Public Declare PtrSafe Function GetSystemMetrics Lib "user32.dll" (ByVal index As Long) As LongPtr
Public Const sm_cxscreen = 0
Public Const sm_cyscreen = 1
Public Sub x()

    UserForm.Show
    Dim xr As Integer
    Dim yr As Integer
    Dim a As Application
    Set a = ThisApplication
    With UserForm
        .StartUpPosition = 0
        If a.Left < 0 Then
            xr = (0.5 * GetSystemMetrics(sm_cxscreen)) - (1.5 * .Width)
        Else
            xr = GetSystemMetrics(sm_cxscreen) + (0.25 * GetSystemMetrics(sm_cxscreen)) - (1.5 * .Width)
        End If
        yr = (0.5 * GetSystemMetrics(sm_cyscreen)) - (0.5 * .Height)
        .Left = xr
        .Top = yr
    End With        
End Sub

The above code is for 64bit windows version.

PDM/CAD Administrator
Using Inventor/Vault 2024.3
0 Likes