VBA - Opening Word Causes VBA Error

Rex2024

New Member
Joined
Nov 17, 2022
Messages
32
Office Version
  1. 365
Platform
  1. Windows
I have a userform in VBA that reaches into a different application and collects data based on user commands.

When you have the userform open, if you open any Word document, save the file, and then attempt to run the VBA commands I get a:

"Run-time error '3':

Return without GoSub.

Why would saving a Word document cause VBA to fail?

VBA Code:
    sEntry = "NA/" & sEmp
    Response = Sabre1.Entry("", sEntry, False)
 

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
The "Return without GoSub" error message typically occurs when there is a "Return" statement in the VBA code that is not preceded by a corresponding "GoSub" statement. This suggests that there may be an issue with the control flow of the VBA code in your userform.
 
Upvote 0
The "Return without GoSub" error message typically occurs when there is a "Return" statement in the VBA code that is not preceded by a corresponding "GoSub" statement. This suggests that there may be an issue with the control flow of the VBA code in your userform.
So absolutely. I agree with this in general.

But the code works fine until the separate application Microsoft Word is opened, saved, and closed.

Then the VBA code throws the error.
 
Upvote 0
One possible explanation is that by saving the Word document is causing the Word application to take focus away from your userform, and that when your VBA code tries to execute the Sabre1.Entry method, it is not able to access the necessary data or resources because the focus has been moved to the Word application.

One way to test this would be to add some code to your VBA script that explicitly sets the focus back to your userform before attempting to execute the Sabre1.Entry method. You could try adding a line of code like:

VBA Code:
UserForm1.SetFocus


Where "UserForm1" is the name of your userform. This should ensure that the focus is set back to your userform before executing the Sabre1.Entry method.

If this does not resolve the issue, you may need to investigate other potential causes of the error, such as issues with the way that your VBA code is handling events or managing resources.
 
Upvote 0
One possible explanation is that by saving the Word document is causing the Word application to take focus away from your userform, and that when your VBA code tries to execute the Sabre1.Entry method, it is not able to access the necessary data or resources because the focus has been moved to the Word application.

One way to test this would be to add some code to your VBA script that explicitly sets the focus back to your userform before attempting to execute the Sabre1.Entry method. You could try adding a line of code like:

VBA Code:
UserForm1.SetFocus


Where "UserForm1" is the name of your userform. This should ensure that the focus is set back to your userform before executing the Sabre1.Entry method.

If this does not resolve the issue, you may need to investigate other potential causes of the error, such as issues with the way that your VBA code is handling events or managing resources.
This makes sense.

I will attempt this and post if it works! Thanks.
 
Upvote 0

Forum statistics

Threads
1,215,029
Messages
6,122,757
Members
449,094
Latest member
dsharae57

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top