Share via

Why can VBA code run in the VBA editor but not as a macro in Word?

Zhenyu Zeng 101 Reputation points
2025-08-25T06:44:35.37+00:00

Hello,

I have a sub in VBA:
Sub SayHello()

MsgBox "hello"

End Sub

When I run it in the VBA editor, it is all right. But when I choose it from Macros in the Developers tab, there is an error:

User's image

What is the problem and how should I solve it?If I save it to Norm template, there is no such an error. So can't I save the sub to the another docm file?

Microsoft 365 and Office | Word | For home | Windows
0 comments No comments

5 answers

Sort by: Most helpful
  1. Stefan Blom 342.2K Reputation points MVP Volunteer Moderator
    2025-08-25T11:01:22.92+00:00

    Hm, I realize that my previous reply wasn't 100% clear. Sorry.

    For document-specific macros, you can provide access via the attached template. The document must be based on the template that contains the macro and the template must be in the User Templates folder as defined by the File Locations dialog box (File > Options > Advanced > File Locations).

    Was this answer helpful?

    2 people found this answer helpful.
    0 comments No comments

  2. Stefan Blom 342.2K Reputation points MVP Volunteer Moderator
    2025-08-25T10:08:47.9766667+00:00

    What is the problem and how should I solve it?If I save it to Norm template, there is no such an error. So can't I save the sub to the another docm file?

    You are on the right track here. To safely run a macro, the file containing it must be loaded into memory, and the way to do this without getting error messages or other failures is to store the macro in the Normal template or in another template (*.dotm) which you have added to Word's Startup folder.

    Was this answer helpful?

    2 people found this answer helpful.

  3. Stefan Blom 342.2K Reputation points MVP Volunteer Moderator
    2025-08-25T18:28:41.53+00:00

    It always throws an error: Subroutine or function not defined

    That error message sounds like the sub cannot be found when you try to run it. I am not sure why.

    Generally, you cannot run (from the Macros dialog box, for example) subs or functions that require arguments. Functions cannot run from the Macros dialog box, because they are designed to return a result.

    Was this answer helpful?

    1 person found this answer helpful.
    0 comments No comments

  4. Stefan Blom 342.2K Reputation points MVP Volunteer Moderator
    2025-08-25T11:07:53.71+00:00

    It seems that in the Chinese version of Word, using VBA in a non-Normal template always throws an error.

    This might depend on locations and if they are defined as secure locations for macro code.

    I don't speak Chinese so you'd have to translate any error message for me. :-)

    Was this answer helpful?


  5. Zhenyu Zeng 101 Reputation points
    2025-08-25T11:03:49.9466667+00:00

    It seems that in the Chinese version of Word, using VBA in a non-Normal template always throws an error.

    Was this answer helpful?

    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.