Testing if Word is Running on a Windows or Mac System

Written by Allen Wyatt (last updated November 9, 2024)
This tip applies to Word 2007, 2010, 2013, 2016, 2019, Word in Microsoft 365, and 2021


1

Cleveland is creating a couple of macros that need to open a template that may or may not exist on the system. His problem is that the macro needs to run on both Windows and Mac systems, and the two systems store templates in different locations. He wonders if there is a way, in the macro, to test whether it is running on a Windows system or a Mac and set the path to the templates accordingly.

There are two ways you can determine if your macro is running on a Windows or Mac system. One method is to rely on compiler constants. You can find more about these universal constants at this page:

https://learn.microsoft.com/en-us/office/vba/language/concepts/getting-started/compiler-constants

You access them by using a special variation of the If...Then structure that utilizes a hash mark (#) in front of the keyword. Here's a code snippet that demonstrates how you can do this:

    #If Mac Then
        ' Set your path variable for a Mac system
    #Else
        ' Set your path variable for a Windows system
    #End If

The #If command indicates that a compiler constant is being tested, and Mac is the name of the compiler constant to test. This constant will be True if the macro is running on a Mac system and False if not. Again, you can find more information about compiler constants at the URL previously provided.

The second method to determine the type of system on which your macro is running is to examine the contents of the OperatingSystem property for the System object, in this manner:

    If InStr(System.OperatingSystem, "Mac") Then
        ' Set your path variable for a Mac system
    Else
        ' Set your path variable for a Windows system
    End If

This works because the OperatingSystem property returns a text string that will either be "Macintosh" on a Mac system or contain "Windows" for a Windows system. Note that in this case the hash mark (#) is not required with the If...Then structure because you are not testing a compiler constant.

Note:

If you would like to know how to use the macros described on this page (or on any other page on the WordTips sites), I've prepared a special page that includes helpful information. Click here to open that special page in a new browser tab.

WordTips is your source for cost-effective Microsoft Word training. (Microsoft Word is the most popular word processing software in the world.) This tip (11430) applies to Microsoft Word 2007, 2010, 2013, 2016, 2019, Word in Microsoft 365, and 2021.

Author Bio

Allen Wyatt

With more than 50 non-fiction books and numerous magazine articles to his credit, Allen Wyatt is an internationally recognized author. He is president of Sharon Parq Associates, a computer and publishing services company. ...

MORE FROM ALLEN

Controlling the Display of Toolbars

The various toolbars available in Excel are indispensable when it comes to easily accomplishing tasks. Here is a concise ...

Discover More

Extracting URLs from Hyperlinks

When you add a hyperlink to a worksheet, it consists of a minimum of two parts: display text and URL address. If you have ...

Discover More

Formatting a Cover Page

Formal reports look better when they are set up with an introductory cover page. Here's how you can add a cover page in a ...

Discover More

The First and Last Word on Word! Bestselling For Dummies author Dan Gookin puts his usual fun and friendly candor back to work to show you how to navigate Word 2013. Spend more time working and less time trying to figure it all out! Check out Word 2013 For Dummies today!

More WordTips (ribbon)

Cleaning Up Text in a Macro

Need to remove extraneous characters from a text string? VBA makes it easy through the CleanString method, described in ...

Discover More

Determining if Overtype Mode is Active

Your macro may need to determine if the user has overtype mode turned on. You can find out the overtype status easily by ...

Discover More

Moving the Insertion Point to the End of a Line

When writing a macro to process the text in a document, you may need to move the insertion point to the end of a line. ...

Discover More
Subscribe

FREE SERVICE: Get tips like this every week in WordTips, a free productivity newsletter. Enter your address and click "Subscribe."

View most recent newsletter.

Comments

If you would like to add an image to your comment (not an avatar, but an image to help in making the point of your comment), include the characters [{fig}] (all 7 characters, in the sequence shown) in your comment text. You’ll be prompted to upload your image when you submit the comment. Maximum image size is 6Mpixels. Images larger than 600px wide or 1000px tall will be reduced. Up to three images may be included in a comment. All images are subject to review. Commenting privileges may be curtailed if inappropriate images are posted.

What is seven minus 7?

2024-11-12 19:14:09

Cleveland Thornton

I want to report that the solution provided by Messers Peter Johnson, Michael Avidan (MVP), and Bob Beechey works like a charm and is very elegant. I tried the "#If Mac Then solution," which worked.

I have not tried the" If InStr(System.OperatingSystem, "Mac") Then" solution, but I will.

Thank you all very much for this response. I hope others find it useful, as well.

Cleveland Thornton


This Site

Got a version of Word that uses the ribbon interface (Word 2007 or later)? This site is for you! If you use an earlier version of Word, visit our WordTips site focusing on the menu interface.

Videos
Subscribe

FREE SERVICE: Get tips like this every week in WordTips, a free productivity newsletter. Enter your address and click "Subscribe."

(Your e-mail address is not shared with anyone, ever.)

View the most recent newsletter.