Office Development – Live Coding

SDN Cast - Live Coding - Office Development 1

Last two years I have been webcasting every Thursday at 8PM GMT+1/CET on our SDN Cast with my buddies Marcel Meijer, Fanie Reynders and recently added as fourth member Gerald Versluis. In this webcast we on a weekly base discuss the news, events on Software Development and added some demos. Usually the webcast was in Dutch but when we had international guests we shifted to English.

Running the webcast is fun, informal and sometimes high level on discussions when talking about Software Development. Last couple of weeks Fanie Reynders and I have been researching to extend our webcasts with actual live coding sessions. Fanie is focusing on his expertise dotNET Core and I am focusing on Office Development.

Hopefully we will also bring in Gerald Versluis coding on Xamarin and related stuff.

So, starting last Tuesday I started (and planned to do this every next Tuesday in the upcoming weeks) my first Live Coding session on Office Development. Basically I started with where I started blogging about the ‘new’ Office Add-ins

Older Office Development Blog Posts starting here:

https://maartenvanstam.wordpress.com/2017/07/20/office-development-2017/

A lot of time went by, and what I liked to do is looking back on my blogposts and see if they still can stand the shift in time all the way to 2019.

In my first Live Coding session I immediately jumped in to building the first Word Add-in in two ways, one in Visual Studio 2019 (Preview!) and one using Yeoman. I did realize however that I probably should have discussed a bit of starting information first, just like I did in my blog posts. I might jump back a bit in the next Live Coding session next Tuesday February 5 (Click <- to see when this will be in your location)

SDN Cast - Live Coding - Office Development 1 YouTube 

You can find the recording of my first Live Coding session here on YouTube:
https://www.youtube.com/edit?video_id=QivUh9Kyyos

Also you can find another recording on Twitch:
https://www.twitch.tv/videos/371899703##

If you want to follow our Live Coding events (and other SDN Cast webcasts) make sure you subscribe on YouTube or follow me on Twitch!

My weekly Live Coding – Office Development webcasts will always be in English (as far as my English goes that is, pretty sure you’ll get the Dutch dialect for free Smile) so everyone in theory should be able to follow what I am doing. It is still work in progress.

Fanie and I are still researching and working on the streaming tools, there is a lot going on producing live webcasts so don’t kill us if we get a crappy chatroom or other bad widgets in the first couple of weeks Winking smile.

Hope to see you in our next Live Coding sesions!

Advertisement

SDN Event: Bill Ayers – Welcome to the Brave New World of SharePoint and Office 365 Development!

BillAyersI800

Second session in our series of October SDN Event recordings we released the first presentation by Bill Ayers from the event where he informs you about the evolution of SharePoint and Office 365 Development and where we are today with regards to these products.

Where to find:

https://youtu.be/MIGySwrI6tg

Abstract:

If your software development journey ever took you into the dark misty forest that is SharePoint development, there are two possibilities: you became a SharePoint developer or (more likely) you vowed never to go there again because of the dragons. But the forest is changing, the dragons are slain (mostly), and there is a new world of SharePoint and Office 365 development. Instead of building full-trust solutions that run in-process on the SharePoint server, we are moving to a model of client-side or remote-server development using a variety of technologies. Starting with SharePoint 2013 and Office 365 we have REST endpoints to support the rich client-side solutions our users want. In this talk we are going to see how far we can go using JavaScript and client-side development for Windows, web and mobile applications, and using ASP.NET MVC and other web development platforms. It’s time to take another look at SharePoint and Office 365 development. We can build sophisticated solutions that take advantage of the powerful back-end services that deliver business solutions through websites, desktop or mobile applications.

Don’t forget to subscribe to our YouTube Channel so you won’t miss the next sessions that we are about to post soon!
https://www.sdncast.nl/subscribe

SDN Cast 66 – XlPublish with Mr. Moffat and Mr. Hoppe

SDNCast 66-800

Today we had our 66th SDN Cast, our 100th published video. Today with Mr. Moffat and Mr. Hoppe showing their new Excel tool XlPublish an Add-In for Microsoft Excel to better integrate Excel data and Microsoft’s Power BI’s Dashboarding product and make it available to every Excel user everywhere.

Usually the SDN Cast in Dutch but today the webcast is in English as Dick Moffat is from the other half of the globe from Canada. My connection was a bit crappy this time, probably my family was busy streaming Netflix eating all the bandwidth instead of watching daddy in his webcast Confused smile.

XlPublish is a very interesting product, making your work as Power BI person a lot easier if you have your data in Excel or even in external data that you can expose through Excel!

Where can you find our 66th episode of our SDN Cast?
https://youtu.be/KE6bhvdLLnI

Did you subscribe our Dutch SDN Cast channel already?

Subscribe your YouTube channel if you don’t want to miss a single broadcast. Usually we will broadcast on Thursday evenings at 8 PM but there can be a change of plans where we need to move to another time or day. So to be sure Subscribe to our channel on YouTube or go to http://www.sdncast.nl to see when the next episode is due.

Other news

Announcing TypeScript 2.5
https://blogs.msdn.microsoft.com/typescript/2017/08/31/announcing-typescript-2-5/

Events

Events this Week

Google Developer Days Europe – September 5-6, Krakow, Poland
https://developers.google.com/events/gdd-europe/

Wazug – September 7 2017, Hilversum
https://www.wazug.nl/Event/Details/2059

Upcoming Events

Microsoft Edge Web Summit 2017 – September 13, Online
https://summit.microsoftedge.com/

MixUG – Development Event – 13 September, Gouda
https://www.eventbrite.nl/e/tickets-mixug-development-evenement-34890959800

Xamarin Dev Days – 16 September – Amsterdam
https://ti.to/xamarin/dev-days-amsterdam-2017

DevIntersection Europe, September 18-20 – Stockholm
https://www.devintersectioneurope.com/#!

.NET Conf – 19-21 September
http://www.dotnetconf.net/

Building high performance web applications with Preact – September 21th- .NET Zuid – Rotterdam
http://www.dotnetzuid.nl/Meeting/20/building-high-performance-web-applications-with-preact

Microsoft Envision – September 25-27 – Orlando
https://www.microsoft.com/en-us/envision/default.aspx

Ignite 2017 – September 25-29 – Orlando
https://ignite.microsoft.com
https://myignite.microsoft.com/sessions

Hybrid IT Event – September 26 – Maarssen, NL
http://www.fujitsu.com/nl/microsites/hybrid-it/event/index.html

4DotNet Events – Xamarin Event – 28 September, Nieuwegein
https://events.4dotnet.nl/xamarin-event

MixUG – Eindgebruikers Evenement
https://www.eventbrite.nl/e/tickets-mixug-eindgebruikers-evenement-34890983872

SDN Event 3 – October 6, Zeist, NL
https://www.sdn.nl/EVENTS/6-oktober-2017

TechDays October 12-13, 2017 RAI Amsterdam
http://www.techdays.nl/
https://techdays.msnlevents.net/content/eventselection.aspx?eventid=26152

SharePoint Saturday Belgium 2017, October 21, Brussels BE
http://www.spsevents.org/city/brussels/brussels2017

SharePoint Unite – October 24-26, 2017, Haarlem
https://sharepointunite.com/

Dev Intersection, October 30- November 2, Las Vegas
https://www.devintersection.com/#!/

Firebase Dev Summit – Amsterdam – October 31
https://firebase.google.com/dev-summit-17/

Web Summit – 6-9 November 2017, Lisbon, Portugal
https://websummit.com/

Visual Studio Live! November 12-17 2017 Orlando, FL
https://live360events.com/events/orlando-2017/vslive.aspx

As usual we are always looking for presenters for our own events and Meetup’s so if you want to share something about hot technologies or just about something you implemented at work or in an App that could be interesting to our audience let us know so we can schedule you for one or our next SDN events or Dutch .NET Group Meetups.

This off course also goes for if you want to write a great article for us to publish in our SDN Magazines or at our SDN Website. The downside of publishing a magazine article is that there can be sometime between writing the article and publishing the magazine. So, if it is time critical sometimes the best option is to publish it on the www.sdn.nl website to get more speedy attention to the article.

As you see, options enough to address your audience with top notch information. You can even, if you like, be our guest in our SDN Cast shows to discuss the projects you are working on or other topics you master. We will instruct you how to connect with us and what it is you need to join the show.

Hopefully we will see you at our weekly web cast, for the schedule go to www.sdncast.nl for next week’s announcement to find out the day and time of our next broadcast or go to www.sdncast.nl/youtube to watch old episodes and videos that we recorded at our events. Don’t forget to subscribe our YouTube channel www.sdncast.nl/subscribe or go directly to YouTube not to miss any of our shows!

Office Development–How to reach the Office Extensibility Platform team

StackOverflow

A long time ago, I answered a lot of questions in the so called Microsoft Forums assisting the community with VSTO issues. The forums still exist (https://social.msdn.microsoft.com/Forums/vstudio/en-US/home?forum=vsto), but are not the main access point anymore if you want to reach the Office Extensibility Platform team with your questions or issues.

These days https://www.stackoverflow.com is the center of the Universe (well… not really but if you have developer related questions it is … sort of …).

StackOverflow is a huge knowledge base of information formed out of an even bigger stack of questions from the community answered by not only the community but also from many of the ‘real’ product groups. If you are building your Add-ins using OfficeJS your can go to:

https://stackoverflow.com/questions/tagged/office-js

This “office-js” tagged group of questions is actively monitored by the Office Extensibility Platform team and you might recognize many of the team members as being the ones answering your questions (or apologizing if you reached an issue). By asking your questions in there the information around it is automatically functioning as future reference item.

You’ll notice, if you Google for a specific issue or question, that you’ll hit StackOverflow simply because you often won’t be the only one with that question. Big chance that the answer is already available and that will save you the time to submit the question.

Remember, that by submitting your question, you are not only helping yourself but you’ll help many others arriving at a later time with the same question.

And … while you are at it, try to answer some of the open questions if you know the answer. It will provide you ‘reputation’ points that may help you even more. The more reputation points you get the more you are ‘credited’ with features. If your reputation is high enough you’ll be able to moderate questions with a group of peer moderators, and if it is really high you can moderate without confirmation of other moderators. You basically build trust by doing good.

BTW, the office-js tag is not the only tag where the team lives, other examples and my favorite tags:

image

Note that VSTO is also in there:
https://stackoverflow.com/questions/tagged/vsto
so if you have questions on the good old VSTO tools this is your go to place Winking smile

Office Development – SharePoint Framework Extensions now RC0

Office Development - SharePoint Extensions RC0

In June I blogged about the SharePoint Framework Extensions Developer Preview and last night the SharePoint Framework Extensions moved a step forward, they are now available as Release Candidate 0:

https://dev.office.com/blogs/sharepoint-framework-extensions-rc0-now-available

Not completely ready so to say, my guess there will be at least two more RCs but at least they moved out of Preview.

If you don’t know what the SharePoint Framework Extensions are, just jump back to my June post to read all about it.

Have Fun!

Office Development – Minimal Code in practice

MinimalCode

Returning on my last ‘minimal code’ blog post, I promised to get back on this to see how this works in practice. One of my question marks I added to the post was that I wondered why the Yeoman boilerplate was using run instead of RequestContext where run passed a context parameter as opposed to the RequestContext where actively a context is requested before accessing the Word object model.

I think I found an answer to that, but will save this to a later date as I first want to make sure the minimal code I provided would work and I found some issues in the original posted code that didn’t seem to be correct.

Here is the updated code that should work in real life:

(() => {

   async function MinimalWordmethod() {

      // Create the client request context. You’ll do this for al Word add-ins.

      var ctx = new Word.RequestContext();

      // Do your things here, such as ..

      var range = ctx.document.getSelection();

      range.insertText("Test MinimalWordMethod", "After");

      await ctx.sync();

   }

   MinimalWordmethod();

})();

How to get the minimal code running

If you go back to the Yeoman project instructions in this post (and fixed the certificate issues in post Office Development – Fixing Trusted Authority – Word Client and https://maartenvanstam.wordpress.com/2017/08/07/office-development-fixing-certificate-issue-with-openssl-certificate/) you should already have a working Word Add-in.

Open the project folder (or if you already deleted it quickly create a new one following my instructions from my earlier blogposts) and open the file app.ts in the src source folder.

Replace all the code from this file with the code above, save it and start your Add-in ‘server’ by running npm start in the root of your project.

Opening Word, with the installed Add-in (again, if you already dropped it follow my other blogposts earlier this month to install it) will show you the Add-in button:

addin7

By clicking the button the Taskpane is showing, just like we’ve seen before as we haven’t changed the Taskpane ‘web’ code, but now you should see the words “Test MinimalWordMethod” arriving in your document:

MinimalCode2

As you can see the code worked! Here is how:

MinimalWordmethod();

This line is automatically triggered when the code is running on startup of the Add-in. By doing so inside the MinimalWordmethod a context is requested in this line:

var ctx = new Word.RequestContext();

If the context is returned correctly you now can access the Word object model to access parts of the document. In this case we access the selected Range object and insert the text right after the Range:

var range = ctx.document.getSelection();
range.insertText("Test MinimalWordMethod", "After");

Instead of having this executed immediately like we’ve seen in VBA or VSTO, the text is only inserted after calling the sync method on the context. In the JavaScript APIs for Office calls to Office are handled asynchronously and to work with that the await and async keywords can be found in the code.

Very simple and basic code, but there are some drawbacks you need to keep in mind. For instance, because things are async, how do you know what the right order is how things are processed. And what about batch processing of boatloads of instructions? Will this all be handled correctly, and what if there are dependencies between the batched instructions? All of these questions arise when looking at this little piece of code, and we need to address all of that in later posts.

To be continued … It’s a never ending story Smile

Office Development – Installing Script Lab

Yesterday in my blog a short introduction to Script Lab, the Microsoft Garage project to work in some sort of a playground to develop your JavaScript for Office scripts so you can test-drive your code that you plan to use in your Add-in.

Today I will go over the installation process of Script Lab. Step by step I’ll follow the instructions and see what this looks like. How hard can it be ‘ey?

To start installing navigate to https://aka.ms/getscriptlab. It looks like this:

clip_image002

Click the Add button to arrive at the guided installation page:

clip_image004

To stay in the mood, we select Word again. I’ll will be using other clients like Excel or PowerPoint soon, but to be consistent I’ll select Word again. This will provide you with a comparable environment and maybe you get used to it. Now, to open in Word click the “Open in Word” option. This will ‘warn’ you that you are about to switch applications:

clip_image006

As this is the plan, you want to move into Word instead of staying in Microsoft Edge. Select ‘Yes’ to go on. This will launch Microsoft Word 2016 (assuming you have Office 2016 installed):

clip_image008

A document is opened, but not without warning. Because you open the document from the dark and dangerous Internet you are provided with a warning and start in the Protected View of Office:

clip_image010

In this case we tend to trust Microsoft in this we continue our journey by pressing “Enable Editing” but now we run into a second warning because the document also contained a Task pane that wants to run the Script Lab Add-in.

clip_image012

Again to make you feel safe, you don’t want any add-in to kick in without your confirmation, you get the option to “Trust this add-in” or “See Details”.

clip_image014

If you click “See Details” they bring you back to base. The installer page is provided where you can read information about the add-in. I guess you already did that, otherwise you wouldn’t get this far in the installation process (unless you just trusted my simple introduction and went ahead ). For now, not all that interesting, so we close this and leave it for what it is.

clip_image016

We go ahead, and decide to trust this add-in. This will bring us the Script Lab tab:

clip_image018

Showing you a balloon with the text “Code, Run and share your Add-in snippets directly from Office” and a “Got It” button to confirm:

clip_image020

Click the Script Lab and you’ll see a ribbon appear with the options:

Code, Run, Tutorial, Help, Reference Docs and Ask the Community

clip_image022

The most obvious thing to do would probably be to go through the Tutorial, at least that was what I picked as a starting point. The interesting thing is however that the Tutorial brings you to an Excel environment to explain you what you can do with the Add-in:

clip_image024

You basically go through all the same steps again, but ending up in Excel showing you all the same options. Open a script-lab-tutorial.xlsx from script-lab.azureedge.net :

clip_image026

This is an Excel starter document with more information about the Add-in and guiding you through the options:

clip_image028

Again the little Protected View warning and the Enable Editing button.

clip_image030

It didn’t provide me with the Script Lab Add-in tab though, only the instructions that you also saw when running the Word installer, except now for Excel.

clip_image032

This wasn’t really what I was looking for, probably useful if I just started from Excel but I didn’t. I went through the GitHub issues and noticed that getting a Word Tutorial is still in planning. Some patience here.

For now, at least the Word Script Lab Add-in is installed and ready to use.

To be continued

Office Development – Fixing Certificate Issue with OpenSSL Certificate

clip_image002

Yesterday we’ve developed our first Add-in for Word, that is – the web ‘site’ that represents the Add-in. We did however run into an issue where the certificate for the website wasn’t recognized due to the different naming on the certificate. Also, the certificate wasn’t trusted so even if we had a correct named certificate we needed to add it to the trusted root certificates.

Let’s fix the naming issue first. To do so we need to create a new certificate with the right name on it. There are a couple of options to do this. For now, I will be using OpenSSL to create this new certificate.

To do so you’ll need to install some tooling to support OpenSSL on your Windows machine. You can download it here:

https://slproweb.com/products/Win32OpenSSL.html

From the downloads, I installed the Win64 OpenSSL v1.1.0f version. By the time you read this it may have been updated to a newer version. Just check the list of downloads to see what the current version is.

Installing Win64 OpenSSL v1.1.0f

clip_image004

Installing is fairly trivial, just follow the instructions, click Next

clip_image006

Accept the agreement (or don’t but you won’t get the software then ) and click Next again.

clip_image008

Select the path where you want to install the software. Initially it opts to install it in the root of the C:\ drive, but I prefer not to put it in the root of my drive so I’ve changed this to put it in my data folder. Click Next.

clip_image010

Accept the default and click Next.

clip_image012

Again, I prefer not to install directly into my system directory so I changed the default to “The OpenSSL binaries (/bin) directory. Click Next.

clip_image014

Click Install to install the software

clip_image016

Optionally you are asked to support the Windows OpenSSL software by donating an amount of money. Feel free to what you think it is worth and click Finish.

Now you can find the installed software in the location specified in step 3 of the installation process.

To create the certificate in your Word Add-in projects you need to create a subfolder in the root of your Add-in project:

Create a .\certs folder in the project

Copy openssl.cnf from (in my case) C:\Data\Projects\Software\OpenSSL-Win64\bin\cnf into the certs folder you just created in the previous step

Open the .\certs\openssl.cnf file and add the following to the end of the file:

[ SAN ]

subjectAltName=DNS:localhost

Open a command window and run the command from the .\certs folder:

C:\Data\Projects\Software\OpenSSL-Win64\bin\openssl genrsa -des3 -out server.key 2048

This will generate the private key. In this process, you need to enter a password twice:

clip_image018

Next you will be generating the certificate request:

C:\Data\Projects\Software\OpenSSL-Win64\bin\openssl req -new -sha256 -key server.key -out server.csr -subj /CN=localhost -reqexts SAN -config openssl.cnf

You will need to enter the previously inserted password again here.

clip_image020

Now generate the certificate based on the request

C:\Data\Projects\Software\OpenSSL-Win64\bin\openssl x509 -req -days 3650 -in server.csr -signkey server.key -out server.crt -extensions SAN -extfile openssl.cnf

clip_image022

Again, enter pass phrase …

Now copy the password protected server key:

copy server.key server.key.copy

Create an unprotected private key:

C:\Data\Projects\Software\OpenSSL-Win64\bin\openssl rsa -in server.key.copy -out server.key

At this time you’ll have an RSA key with the necessary subject and subjectAltName. Let’s put this into our project by changing the bsconfig.json file in the root of the project.

Replace the line "https": true, with the following:

"https": {

"key": "./certs/server.key",

"cert": "./certs/server.crt"

},

Like this:

clip_image024

Don’t forget to save the bsconfig.json file before running npm start as I did, otherwise it still won’t like your certificate as it will point to the old one .

npm start

If all goes well we’ve now fixed the certificate error due to the different naming and our website will run fine (on your local machine on port 3000):

clip_image026

Now we fixed the website we are now ready to run it as a Word Add-in in a Word client. We will go over the steps to do this in my next blogpost.

To be continued

Office Development–The Bad

clip_image002

Visual Studio Tools for Office (VSTO) is a set of development tools available in the form of a Visual Studio add-in (project templates) and a runtime that allows Microsoft Office 2003 and later versions of Office applications to host the .NET Framework Common Language Runtime (CLR) to expose their functionality via .NET.

Quote: https://en.wikipedia.org/wiki/Visual_Studio_Tools_for_Office

As we have seen in my previous blog post Office Development – The Ugly the first option to build your Office extensions using Visual Basic for Applications (VBA) did not win the beauty contest. A very powerful toolset to create beautiful and especially productive customizations. I’m sure that VBA, the tools to set Office to your hand, allowing you to tailor Office in a way that it perfectly fits the needs of your company, is one of the main reasons that made Office such a popular box of applications.

It was however risky – a potential security risk and code management is a pain. In the real world it happened that a ‘new’ version was rolled into production where all the sudden existing features ‘disappeared’ as a result of the developer picking the ‘wrong file’ containing an older version of the solution and continued developing features using the incomplete version.

This needed to change … in the meantime managed code -.NET- appeared at the horizon and the next option at least had to be a) secure and b) needed an improved source control option.

The solution Microsoft put on the table were the:

Microsoft Visual Studio Tools for the Microsoft Office System.

The product name was most likely the longest name available at that time. The Visual Studio Tools for the Microsoft Office System (VSTO) was architected as a bridge between Visual Studio and The Microsoft Office System. On one side the languages team with Visual Studio, on the other side the Office team and in the middle the VSTO team – code named Trinity …

clip_image004

So how is VSTO different compared to VBA?

Applications, add-ins or document customizations, are built using Visual Studio. Initially in a separate Visual Studio .NET 2003 VSTO SKU and after a few version inserted in the Visual Studio 2008 box. The code no longer lived in the documents or other Office files, but from now on lived in an external assembly that would be triggered by the Office host application if the right properties were available in the document.

Loading the assembly was done in a relative complex manner and it was made sure that security wise the pain seen with VBA would not exist with VSTO add-ins.

Here is a schematic overview of the load pattern:

clip_image005

As you can see the Office Application is looking at the Registry to see what add-ins are installed, if found any the Deployment manifest (pointed to by the Registry) is read and following that the Application manifest is read and the assembly loaded.

A real extension to the Office Application … to make this all secure the security is enforced at installation time. There was a whole slew of prereqs that must be covered before the add-in would be installed:

clip_image006

This part made VSTO the “Bad” in The Good, The Bad and The Ugly … it appeared to be a real pain for the IT Pros at the time. By default, VSTO used Click-Once to install the extension on the machine and a lot went wrong … certificates expired, not added to the Trusted Publisher lists, etc. etc. There was another option, to install an add-in using an .msi installer but the average VBA developer had a hard time getting around all of this, jumping all the hoops to make it work.

Was it all Bad?

Not at all! By using managed code, C# or VB.NET – whatever your preference was, your world of Office completely opened. Whatever you could do with .NET (and that is basically everything) you could add to your solution. Calling third party libraries, UI components, later even XAML UI interfaces, sky is the limit.

For me personally this is still my fav option to build Office customizations. I wasn’t awarded nine-year Visual Studio Tools for the Microsoft Office System MVP – I guess the longest MVP title as well, without reason. VSTO controlled my life for the large part of these years (hopefully my wife is not reading this ) did a lot of forum support in this area and built VSTO applications for large enterprise companies. At the time, we still had on Microsoft campus Software Design Review meetings to share feedback and we gave the team a hard time to make sure they released the features that we really needed.

Where to go next

But … the world changed, Windows is no longer the only platform that needs to be supported. Because of that a new mechanism was proposed. By using the common web standards and JavaScript a platform independent system was developed by providing OfficeJS APIs to access the Office object model from web based add-ins.

And this is where we will dive in soon. It’s a relative new world, although development already started years ago, so there is a lot to learn there. Let’s see if we can do the same with OfficeJS as we can do with VSTO – but now on multiple platforms and several clients.

Maybe I will return to VBA and VSTO in separate blog posts just for fun. There is just a ton of information to share on all off these areas.

Book

Oh, and if you really want to know the ins and outs of VSTO I really recommend you reading the VSTO bibles by Eric Carter and Eric Lippert: Visual Studio Tools for Office 2007: VSTO for Excel, Word and Outlook. It is a huge and heavy piece of reference with over a thousand pages of inside information.

Office Development – The Ugly

clip_image002[4]

In the short introduction in my previous post “Office Development–The Good, The Bad and The Ugly?” I was showing three of the main options to build Office add-ins (again I will use Office Add-ins with the capital A to indicate talking about the ‘new’ Office JS Add-ins as opposed to add-ins in general). In this post, I’m now going into the first of each of the options a bit: how to start and where to look for things.

The first option is Visual Basic for Applications (VBA). If you go back a long time, like I do, this was about your first option to customize your Office environment. None of the other options were around at the time. VBA is hosted within the individual host applications such as Word, Excel, Outlook, PowerPoint, Excel or even some other applications not limited Microsoft to but licensed by Microsoft to these other vendors.

Disabled by default

Straight out of the box you need to activate the option to be able to use VBA from the host application. In this case Word, you need to go into the File menu option, select Customize Ribbon and tick the option on the right under Main Tabs called “Developer”.

clip_image004[4]

After activating the option you will notice that the “Developer” Tab becomes visible:

clip_image006[4]

By selecting the tab, you’ll see options like “Visual Basic”, “Macros”, “Record Macro” and more.

Entering your first code

Ok, now it’s time to enter your first VBA code … we won’t be creating the good old “Hello World” but it sure comes close. Click the “Visual Basic” button to open the VBA Integrated Development Environment (IDE) the environment where you build and (test)run your application.

Let’s dive in now … open the VBA IDE as explained above, and enter the following lines:

Sub test()

   Dim DefinedVariable As String
UndefinedVariable = “test”
MsgBox DefinedVariable

End Sub

Like this:

clip_image008[4]

Running the code with function key F5 or stepping into the code with F8 until End Sub (the yellow line) you will see that Word is showing an empty dialog box where you expected the word “test” to appear if all variables were used correctly:

clip_image010[4]

The Ugly I

You will notice that I defined the variable “DefinedVariable” but I initialized the variable “UndefinedVariable” with the value of “test”. I did this to show you the first “Ugly” in VBA … Although you won’t get an error your application will show an unexpected result (well … if the mistake was made in real life)

What happened here is that VBA didn’t throw an error as you are perfectly allowed to initialize an undefined variable (the one called UndefinedVariable in the code) but you’ll find that the variable was displayed that you did not initialize, called DefinedVariable.

This, especially in larger applications will cause extremely unexpected results and if the variables just look alike it will take you hours to notice that you made a typo somewhere.

To avoid these issues “Option Explicit” was invented. By adding the line, VBA requires you to define all variables used in your application. So, if you use this and runs the code your will now see that an exception will be thrown to point out that you missed something here:

clip_image012[4]

Where is your code stored?

You built your first application, but you wonder … where is it stored. Well that is both the beauty and evil thing about VBA, it travels with the document (template, workbook, presentation … depending on your host application). The beauty because it is very easy to distribute, you just share the document, if it is about Word, to another person in your organization and they can run it. That is also -the risk- of being able to just share it, shady virus writers realized this and used it as a vehicle to spread their viruses in a fairly easy manner. Microsoft tried to solve this issue a little bit by differentiating between file naming. Documents containing VBA code are using extensions with an m like document.docm while ‘plain’ no code documents are named document.docx. It is good to know though, that the old extension .doc still exists.

The Ugly II

Lack of security and the risk of writing faulty code makes VBA the “Ugly” one of the three options. You are perfectly capable of creating the best and most intelligent tools using VBA and there have been some great examples that boosted productivity but it is just not “Enterprise” ready. It is hard to control and can cause a large impact on your support division within your company.

So, should you use it? Of course, you can use it whenever you feel it will help you being productive! I still use it myself … if I need to create some quick ‘n dirty solutions to avoid repetition in my “task for the day” it is really easy to record a few lines of code, edit it to fit your needs and run it.

I deliberately didn’t go into details of the VBA language. The reason for that is that I just wanted to provide a primer showing the three development options and my goal will be to dive into the “Good” (as considered today) option. If your want to know how to proceed with VBA I suggest you dive into the documentation behind the links below or find some introduction sites. The benefit of a toolset that has been around for such a long time is that there is a boatload of information available on the interwebs to get you started. This post is merely an introduction as part of the big picture called “Office Development” or as I called it “Office Development 2017”.

Documentation

https://msdn.microsoft.com/VBA/VBA-Language-Reference

Wikipedia

https://en.wikipedia.org/wiki/Visual_Basic_for_Applications

%d bloggers like this: