Update: Added instructions for VS2015 (2016-07-16)
The last post was the introduction to the series of XLW related posts. Today I will describe how to set up a project with XLW. XLW comes with an installer that installs the project templates for your development environment. It is a quick way to start, but you don’t have much control over the process and the project template might not match your expectation on how project should be arranged.
XLW consists of the following components:
- Header files
- Static libraries (debug, release)
- InterfaceGenerator - an executable that takes a C++ header file that
contains special directives and creates a
.cpp
file with code that interfaces your functions with Excel.
Project Setup
I will show how to set up for 32 bit Excel in Visual Studio 2013 Pro (and Visual Studio 2015). Set up for other environments should be similar.
1. Create a new workspace
-
Select menu:
File
->New
->Project...
:\
-
Select
Other Project Types
->Visual Studio Solutions
->Blank Solution
. Set your workspace location, type namemylib
and clickOK
button :\
2. Create a new project in the workspace
-
Right click on the
mylib
workspace in the Solution Explorer and selectAdd
->New Project...
:\
-
Select
Visual C++
->Win32
->Win32 Console Application
, set project name toxlladdin
and clickOK
:\
-
On the Overview page of the Win32 Application Wizard dialog box choose the
Next
button. -
On the Application Settings, under Application type, select
DLL
and checkEmpty project
and pressFinish
button:\
3. Download most recent version of XLW and add it to the project
-
Right click on the solution in the Solution Explorer and select
Open Folder in File Explorer
:\
-
Create
thirdparty
folder in themylib
solution folder. Then createxlw
folder inmylib\thirdparty
. Createinclude
andlib
folders inmylib\thirdparty\xlw
. Finally createxlw
folder inmylib\thirdparty\xlw\include\xlw
:\
-
Head to XLW download page and download the most recent version of XLW, but do not install it.
-
Use 7z to open the downloaded file as an archive:\
-
Copy header files from the archive’s
$_OUTDIR\xlw
folder tomylib\thirdparty\xlw\include\xlw
. -
Copy library files from the archive’s
xlw\lib
folder tomylib\thirdparty\xlw\lib
. Select library files that correspond to your environment. In the case of Visual Studio 2013 you need to choose files that start withxlw-vc120-mt
: *xlw-vc120-mt-5_0_2f0.lib
*xlw-vc120-mt-gd-5_0_2f0.lib
*xlw-vc120-mt-gd-5_0_2f0.pdb
-
At this moment the distribution does not contain libraries for Visual Studio 2015. You can download 5.0.2 version from here (together with Visual Studio 2013 libraries and include files and
InterfaceGenerator.exe
). -
Rename these library files to remove version (
-5_0_2f0
) from their names. This will enable you to update to a new version by just replacing the files without modifying solution build settings. I also create file with name corresponding to library version, so it is easier to track versions. I.e. I put5_0_2f0.txt
inlib
folder. -
Finally copy
InterfaceGenerator.exe
from the archive’sxlw\build\vc12\Release
folder tomylib\thirdparty\xlw
.
4. Configure build for All Configurations
Right click on xlladdin
in the Solution Explorer, select Properties
:\
\
Then select All Configurations:\
-
On the Configuration Properties/General sheet set
Target Extension
to.xll
:\
-
On the Configuration Properties/Debugging sheet set:\
-
Command
toOFFICE_PATH\excel.exe
(whereOFFICE_PATH
is a path to your installation of Microsoft Office (in my case it isC:\Program Files (x86)\Microsoft Office\Office15\EXCEL.EXE
) -
Command Arguments
to$(TargetPath)
-
-
On the C/C++ sheet (Note: if you don’t have this sheet add an empty
mylib.cpp
file to your project) setAdditional Include Directories
to$(SolutionDir)thirdparty\xlw\include;%(AdditionalIncludeDirectories)
:\
-
On the Linker sheet set
Additional Library Directories
to$(SolutionDir)thirdparty\xlw\lib\;%(AdditionalLibraryDirectories)
:\
5. Configure build for Debug
Select Debug configuration:\
\
Add xlw-vc120-mt-gd.lib
(xlw-vc140-mt-gd.lib
for VS2015) to the
Additional Dependencies
on the Linker->Input sheet:\
6. Configure build for Release
Select Release configuration:\
\
Add xlw-vc120-mt.lib
(xlw-vc140-mt.lib
for VS2015) to the
Additional Dependencies
on the Linker->Input sheet:\
7. Add initial content
-
Create three empty files in the
xlladdin
project folder:mylib.h
mylib.cpp
xlwmylib.cpp
-
Add the following content to
mylib.h
:#ifndef _mylib_h_ #define _mylib_h_ #include <xlw/xlw.h> //<xlw:libraryname=MyLibrary double //Square root of an argument MySqrt(double x //argument ); #endif
-
Add the following content to
mylib.cpp
:#include "mylib.h" #include <math.h> double MySqrt(double x) { if( x < 0. ) { throw("#Negative argument"); } return sqrt(x); }
8. Set up custom build step to generate xlwmylib.cpp
-
In the Solution Explorer right click on
mylib.h
and selectProperties
-
In the properties dialog box set configuration to
All Configurations
:\
-
On General sheet set
Item Type
toCustom Build Tool
and hitApply
:\
-
On Custom Build Tool sheet (which appears after you hit
Apply
in the previous step) set the following (also make sure that the configuration is still set toAll Configurations
):\
-
Command Line
to$(SolutionDir)thirdparty\xlw\InterfaceGenerator.exe %(FullPath)
-
Description
toGenerating xlw%(Filename).cpp
-
Outputs
to$(ProjectDir)xlw%(Filename).cpp;%(Outputs)
-
Execute Before
toPreBuildEvent
(this might be grayed out. Look at next step)
-
-
If you were not able to set
Execute Before
toPreBuildEvent
on the Custom Build Tool page for themylib.h
file open Properties dialog for thexlladdin
project (right click on it and selectProperties
), and then head to the Custom Build Step sheet. SetExecute Before
toPreBuildEvent
:\
9. Test addin creation
-
Set to reload files in Visual Studio automatically. Click on main menu ‘TOOLS’ -> ‘Options…’ and check
Auto-load changes, if saved
on the Environment/Document sheet:\
-
Press
F5
- the addin will be compiled and loaded in Excel (accept Excel’s security warnings by pressingEnable the add-in for this session only
). If you see an error that the file’s content is not recognized - you are trying to open your 32 bit addin in 64 bit Excel. -
Create a new spreadsheet and go to the Funciton wizard:\
-
Find MyLibrary:\
-
Select MySqrt function and put
3
as an argument. -
Save spreadsheet and exit Excel.
10. Debugging
-
Press
F5
- Excel will start and load the addin. -
Open the spreadsheet saved in previous section.
-
Put a breakpoint on
if(x < 0.)
line inmylib.cpp
-
Select a cell with
MySqrt
function and pressEnter
to run it. -
The code will stop on the breakpoint.
11. Happy coding!
If you don’t have patience to go over these steps, just
download the mylib
solution
(or VS2015 version), open it in
Visual Studio and press F5
key. If your Excel is not installed in
C:\Program Files (x86)\Microsoft Office\Office15\EXCEL.EXE
you will need
to adjust the path as described in
Section 4.