Fix invalid namespace in VSTA (C# or VB.NET) SOLIDWORKS macro

Edit ArticleEdit Article
Comments
See more issues and resolutions

Symptoms

Example for SOLIDWORKS VSTA macro (C# or VB.NET) is copied from the SOLIDWORKS API Help documentation or from any source as a code. Number of compilation error are displayed:

  • MacroName.SolidWorksMacro doesn't contain a definition for 'SwApp'
  • The name 'Main' doesn't exist in the current context

Compile error when code is copied from the example into the VSTA macro
Compile error when code is copied from the example into the VSTA macro

Cause

VSTA macro is based on multiple connected files which must reside in the same namespace. When new macro is created the namespace might not be equal to the one used in the example source code.

namespace MacroName.csproj
{
  ...
}

Resolution

Change the namespace in the SolidWorksMacro.cs file to match the default namespace

  • Open the project properties page

VSTA macro project properties
VSTA macro project properties

  • Copy the value in the Default Namespace field of the Application tab

Default namespace of the VSTA project
Default namespace of the VSTA project

  • Rename the namespace to the copied value

Renamed namespace to match the default namespace
Renamed namespace to match the default namespace

  • Rebuild the macro

Notifications

All articles and code at CodeStack are now open-source and hosted on GitHub. If you want to contribute by modifying existing articles and code snippets, submitting new ones, reporting errors and bugs etc. please follow this blog post for more information. We appreciate any contribution.


Product of Xarial Product of Xarial