VBA->JS: Conditionals, Loops & Operators 3 (Syntax)

July 20, 2015

In the third part of working with conditionals we’re going to look at some looping operations. Again, the concept is the same as what’s used in VBA, just different syntax. Read the rest of this entry »

VBA->JS: Conditionals, Loops & Operators 2 (Syntax)

July 19, 2015

Now that you’ve familiarized yourself with the logical operators JavaScript uses it’s time to look at the basic conditional constructs. Looping constructs are the topic of the next post. Read the rest of this entry »

VBA->JS: Conditionals, Loops & Operators 1 (Syntax)

July 19, 2015

An important part of most programs we write is testing the values of variables and performing tasks based on the result. Typical VBA constructs that use comparisons are If, For, ForEach and Do…While. JavaScript provides the same functionality, using pretty much the same logic, but the syntax differs.

Before we look at these in the next post, however, it’s first important to learn what operator symbols JavaScript uses, how code blocks are defined and to understand the way the Equal sign ( = ) works. Read the rest of this entry »

VBA->JS: Data type properties and methods (Concept)

July 18, 2015

VBA provides useful functions for working with variables of the data types String, Number and (to a lesser extent) Boolean. For example, if you need information from a String you can use Len(), Left(), Mid() and Right() to determine the number of characters in the string, the characters counting from the beginning of the String, from any given position in the String or from the end (right) of the String, respectively.

JavaScript provides much of the same information, sometimes in the form of properties, sometimes in the form of methods (like VBA functions). The names are different from what we’re familiar with in VBA, but the functionality is all there, with some nice additions we’ve sometimes wished for! Read the rest of this entry »

VBA->JS: JavaScript data types (Concept)

July 18, 2015

Data types determine what can be done with the content in a variable. For example, Numbers can be manipulated arithmetically; Boolean is used to test a variable’s state: does it or does it not meet a specified criterium.

Two of the data types used in VBA are also found in JavaScript and are used in the same way: String and Boolean.

JavaScript also supports the numerical data type, but in contrast to VBA and most other programming languages there’s only the one: Number. It’s not broken down into various integer and decimal types that exist primarily to limit memory storage requirements. So no need to worry about whether the value you’re assigning to a variable will be Integer or Long, Single or Double! JavaScript just goes whole hog and gives you 64-bit double precision floating point…

Then there are three data types not used in VBA: Read the rest of this entry »

VBA-JS: Kodiak JavaScript (Tool)

July 17, 2015

Due to an injury to my horse, I’m spending a lot of time at the stable. It’s much more economic to sit there for a couple of hours rather than drive back-and-forth. To make the most of my time, I try to do some work – learning more about JavaScript – while I’m there. Since I don’t want to lug around the laptop I was looking for a JavaScript IDE for my iPad that works offline and was quite surprised to find one in the App Store: Kodiak JS. Read the rest of this entry »

VBA->JS: Trying out JavaScript (Tool)

July 17, 2015

Up until now, you’ve had to take my word that JavaScript behaves the way I describe. That’s fine, for an article or two, but at some point a developer wants to actually try these things out…

Unlike VB6, VB.NET or C# you can’t create an independent program using JavaScript, by itself. Similarly to VBA it needs to run in a host environment, typically a browser window as part of an HTML page. Of course, if you’re just playing around, setting up an HTML page so that you can see the results of your JavaScript tests is a lot of (too much!) work. What’s more, you’re not going to get much information when things go wrong – for the most part the code will simply stop executing.

So I was very happy when one book I was reading recommended using the “Firebug” add-on for the Mozilla Firefox browser.

This installs into the Firefox browser as a button in the toolbar across the top (circled in red in the screenshot, below). Clicking the button opens a split-pane at the bottom of the browser window. On the right, you type the JavaScript code. When you click on “Run” in the tools across the top of the split-pane the code is executed and the result of the last line is printed. This is similar to using the Immediate Window in the VBA Editor, except you can type lines and lines of code in a normal manner rather than everything having to be on a single line. (Firebug1.png)

Firebug add-on in Firefox

Firebug add-on in Firefox

When you get serious about creating applications, such as a Web Add-in for Word or Excel, then you need a different tool – although this still comes in handy for testing things!

VBA->JS: Case sensitivity (Syntax)

July 17, 2015

When working in VBA it doesn’t matter, for the most part, whether you type capital or small letters. VBA will monitor and compare with its internal list of keywords as well as variables you’ve declared and make corrections as you go. If there are variables that aren’t declared there are no automatic corrections, but VBA doesn’t care. On the whole, you only have to be careful when typing Style names and Find expressions when “Match Case” is in-force. Read the rest of this entry »

VBA->JS: String concatenation (Syntax)

July 16, 2015

The VB languages differentiate between concatenating (“adding”) strings and adding numbers. You’re supposed to use an ampersand (&) to concatenate a string and a plus sign (+) to add numbers. Although VBA will accept a plus sign to concatenate strings it’s frowned on as you can get unexpected results.

JavaScript (like C#) must use the same operator for both operations, the plus sign: Read the rest of this entry »

VBA->JS: Comments (Syntax)

July 16, 2015

Commenting code is important. JavaScript and C# use the same commenting conventions, which differ somewhat from VBA’s. Comments can be on a single line, similar to VBA; they can also extend across multiple lines: Read the rest of this entry »

VBA->JS: Variable declaration & semicolons (Syntax)

July 15, 2015

In both VBA and JavaScript, as with most programming languages, those things with which code works are held in “variables”. Both VBA and JavaScript declare variables and assign values to them.
Comparing how they do it: Read the rest of this entry »

JavaScript for VBA Developers – a short history

July 15, 2015

Those of us who’ve been developing with the Office applications for Windows since they were introduced in the early 1990’s are no strangers to programming language changes. The transition in version 97 from the UI-oriented WordBasic / Excel XLM / AccessBasic languages to the more object-oriented VBA meant not only learning a new language, but making a significant paradigm shift: Read the rest of this entry »

BUG Word 2013: Selection in Word disappears after running code

February 26, 2014

Since the release of Word 2013 we’ve been seeing sporadic complaints in the Word forum from developers that the selection highlighting disappears after their code executes. Only yesterday did someone post steps to reproduce the issue that worked consistently.

It turns out that this is, indeed, “unexpected behavior”. Apparently the screen redraw isn’t firing as it should.

There is a workaround, although it’s not ideal as it causes some screen flicker:

The Application.ScreenRefresh method will make the selection highlighting visible, again.

Changing the zoom factor should also work.

Ribbon / Office Fluent UI: Links to information

February 8, 2014

Until the release of Office 2013 and all the new information and hype about the “Apps for Office” developer interfaces, there was a useful page on MSDN that collected links to all relevant information concerning developing for the Ribbon and Backstage (http://msdn.microsoft.com/en-us/office/aa905530.aspx), also known as the “Office Fluent UI”.

For a number of months you could still find the link, but it was redirected to a page about the new technology, containing no information whatsoever about the Ribbon. Recently, the old page doesn’t show up any more in internet searches, so only those of us with old “Favorites” or bookmarks to the link collection get to enjoy this frustration. But this is no help at all to the developer unfamiliar with what documentation is available nor any idea where to search for it.

In my work as forum moderator I constantly need to refer to, and refer people to, Ribbon documentation. So I’ve collected all the useful links from that page and posted them in the General Office Developer forum here.

I’ve tried to remove any links that are no longer active, but I may have missed some. So if anyone comes across a problem in that listing, please leave a comment here, or in the forum.

(And I also apologize for the lousy formatting of the post. The forum software doesn’t “like” it and refuses to use the correct spacing and bold formatting )

Word: Difficulty processing fields in the footer during printing

February 7, 2014

Once again, a forum discussion has brought to light something new and useful to know when working professionally with Word.

When someone asks how to display different content in the footer for the last page, only, the recommended approach since time immemorial is to use an IF field. The If field tests whether the current page number is also the last page number. When that’s the case, different content is displayed. Basic instructions on how to set this up can be found on the Word MVPs FAQ site. It’s not always simple to set up, but it works.

Except in this case, it didn’t… Read the rest of this entry »

The Open XML Package Editor in VS 2012/2013

January 31, 2014

As more and more developers migrate to Visual Studio 2012 and/or Visual Studio 2013, we’re seeing requests in the Open XML SDK forum for a version of the Open XML Package Editor that works with these new versions of Visual Studio.

(The Open XML Package Editor lets you view the structures and XML content in an Office Open XML zip package and edit it, something the Open XML SDK Productivity Tool does not support.)

Unfortunately, at the time of this writing, Microsoft has not updated the tool – originally written for Visual Studio 2010 – nor released a new version. Luckily for all of us, a resourceful colleage of mine who attended my seminar in LucerneDr. Eckehard Pfeifer – did some research that he said I might share :-) Read the rest of this entry »

Word Programmierung Handbuch (4th ed.) is here!

January 24, 2014

A few days late for my seminar, but it’s finally here! Despite Microsoft Press’s break with O’Reilly, leaving authors in the German market in limbo, the 4th edition of the Word Programmierung Handbuch has been printed, bound and is being distributed this week.

Many thanks to the publisher for managing to accomodate all the new material without needing to throw out as much of the existing as we feared would be necessary. Instead of just over 1000 pages, the 4th edition has 1145, more than 250 completely new! Read the rest of this entry »

Office Trainertage 2014 in Lucerne

January 23, 2014

I just spent a couple of interesting days at the Office Trainertage (training for Office trainers) in Lucerne, Switzerland, hosted by Kuhn Informatik AG. I’d heard about this event, but never had the opportunity to experience it. Read the rest of this entry »

Content Controls and language formatting

January 11, 2014

Long-time Word users, especially those who work on professional documentation in multilingual environments, know that you have to be very careful that the language default in Word matches the default language setting in Windows. If it doesn’t, the Windows language will override the Word language as direct formatting in the document. Documents created in such an environment are difficult to get under control – it’s almost impossible to clean out the imposed Windows language without delving into the Open XML file format. Read the rest of this entry »

Open XML SDK: Does the runtime version matter? YES!

January 8, 2014

There was a discussion in the Open XML SDK forum today that I found interesting. Certain namespaces for Office 2010 weren’t being recognized with a reference to the Open XML SDK version 2.0, even though they should have been. The references were being picked up just fine with a reference to the newer Open XML SDK 2.5, however.

When I tested I had no problems using the namespaces. Luckily, I thought to include the version number of the Open XML SDK runtime in my reply. Turns out that Microsoft replaced the original download with a newer version without any public notification, either about the fact that it was being done or mention about problems with the older version. When the OP downloaded the newer version, the problem disappeared!

So if you need to use the older version of the Open XML SDK and are having problems with any namespaces that belong to DocumentFormat.OpenXml.Office2010.Word.DrawingShape, try uninstalling, downloading a newer version and re-installing. You need a version that starts with v2.0.5###; the problem was ocurring in v2.0.4###.

Downloadable Office 2013 VBA Help files

October 11, 2013

One of the major changes for Office VBA Developers in version 2013 was the removal of on-board help files. If you couldn’t connect to the internet, up until now you had no access to any object model Help.

The Office team heard the complaints and has invested resources in packaging the MSDN VBA Language Reference for Office 2013 in a set of downloadable Help (.chm) files, available from the download site: http://www.microsoft.com/en-us/download/details.aspx?id=40326

Note, please, that these do not hook up to context-sensitive F1 Help; that can access only the on-line version. Also, there will be no automatic update of these files and there’s no way to know if they’ll ever be updated. So in order to view the most current version or provide feedback you still need to use the Internet resource.

But, if you’re travelling or have a slow connection, this at least provides an option for viewing the basic 2013 object model information.

Problem using a URL as index for the Documents collection

September 3, 2013

A poster in the Word for Developers forum has brought a new problem with the Documents collection to light this week. Unlike the one discussed in an earlier blog post, this behavior applies to all current versions of Word, including 2013.

Back when the Internet was comparatively new, Word didn’t know how to handle URLs as paths to files at all. The Open and Save methods of the Document(s) object couldn’t deal with anything that wasn’t a standard local or network path. At some point, this was rectified and Word can quite happily open and save documents to Internet, SharePoint and Skydrive sites.

What Word still can’t handle, however, is a URL as the index value to identify a Document object in the Documents collection. Code such as the following triggers the error 4160 “Bad file name”. Read the rest of this entry »

BUG: Migrating VSTO Word 2008 document to later versions of VSTO

August 29, 2013

I attempted to upgrade a Word 2007 document-level customization, created with VSTO 2008 to VSTO 2012 and Word 2013 the other day. Even after following all the migration steps, I was still getting an error message for the Document Designer:

“Designer failed to load correctly. Do not use the Designer to work on the document until you fix any errors in the error list, then close and re-open the Designer. Some errors can be fixed by rebuilding your project, while other might require code changes.” Read the rest of this entry »

Trapping events on a repeating section content control

August 21, 2013

[This is the follow-up to a previous post about mapping repeating content controls to a custom XML part.]
As a developer, you might want to be notified when the user adds a new set of mapped nodes to a repeating section, or deletes a set. If you look for events for Content Controls to help you with this you won’t find any.

Nor are there any events at the document or application level.

And you can’t re-purpose the entries placed in the context menu for adding or removing a repeating section item.

So, what can you do? Read the rest of this entry »

Repeating section content control in Word 2013

August 20, 2013

Some significant improvements were made to Word’s content controls in Word 2013. You can find an overview here.

One interesting capability is a new kind of content control that brings back some of the functionality lost when XML nodes technology had to be pulled from Word due to the court decision in 2009. In the original release of Word 2007, xml nodes inserted into a table could be configured to repeat automatically when new table rows were inserted. While content controls in Word 2007 and 2010 can take over a lot of what could be done with xml nodes, this – and validation – were not possible “out of the box”.

A large portion of the article linked to above covers the new type of content control, in the section “Supporting repeating content”. You’ll also find a practical introduction, mainly targeting the “power user” on Greg Maxey’s site.

Something that doesn’t really jump out at you in any of the information I’ve found so far is how to

  1. Map a repeating section to a list of data already present in a Custom XML Part so that it all displays in the document
  2. Trap when the user adds or deletes a new data item (the repeating section must be mapped to a Custom XML Part in order for this to work)

Read the rest of this entry »

Open XML SDK: Continuous section break in the middle of a paragraph

July 22, 2013

In my last post I described how to achieve a continuous section break in Word Open XML. In this post, I go into how to force the continuous section break in the middle of a paragraph. Read the rest of this entry »

Open XML SDK and Continuous Section breaks

July 21, 2013

There was a question the other day in the Open XML SDK forum about inserting Continuous section breaks. Inspecting a document.xml file, you’d think it would be as simple as adding a SectionType child element to a SectionProperties element associated with a paragraph’s properties, as can be seen in the following Word Open XML:
<w:p><w:pPr><w:sectPr w:rsidR=”0008038F” w:rsidSect=”00C26E80″>
  <w:type w:val=”continuous”/>
   <w:pgSz w:w=”11906″ w:h=”16838″/>
   <w:pgMar w:gutter=”0″ w:footer=”708″ w:header=”708″ w:left=”1417″ w:bottom=”1134″ w:right=”1417″ w:top=”1417″/>
   <w:cols w:space=”708″/><w:docGrid w:linePitch=”360″/>

If only it were that simple! Read the rest of this entry »

The Future of Office Programming: Update

July 20, 2013

On January 10 2013 I blogged about what I think the future of Office programming technologies would be.

On June 18 2013 Microsoft blogged on this topic under the title “Roadmap for Apps for Office, VSTO, and VBA”. The content basically confirms that all three existing technologies (Open XML is not mentioned) will continue to be supported for the foreseeable future. It also briefly describes what the scenarios, pros and cons of the three are.

Word 2013: Missing object model documentation

May 2, 2013

I think many of us are still trying to come to terms with the fact that the only Help for Office 2013 is on-line. The official information can be found here:
in the section AWS (Assistance and Worldwide Services).

The official line is that this is “better” all around. Help can now be searched on-line and translated. Of course, the fact that this was already the case for almost ten years isn’t mentioned…

Truth be told, the real reason is resources (financial). If Help has to be delivered with the product, then it has to be written before the product is released. This means lots of man-hours just prior to release, then down-time until the next product release. So, to a certain degree, the decision is understandable.

The other side of the coin, however, is that there’s no deadline that forces the documentation to actually be done and finished.

The result at the time of this writing, some months after the official release of Office 2013, is that the object model language reference (Help) hasn’t been reviewed for relevance to Office 2013, and new functionality isn’t fully incorporated. So you can spend literally hours trying to find information in the new, less friendly MSDN interface with the result that
you can’t find what you were looking for because it’s simply not there.

The “final straw” for me was a question about the Word object model in the Word for Developers forum the other day. So I’ve set up a new “page” on this blog to document un-documented behavior and information concerning Office. Everyone is welcome to contribute!

Word 2013: AutoSave and the new property IsInAutoSave

March 17, 2013

In a previous post I wrote about a change in Word’s behavior: Even if the user chooses “Don’t save” when closing a Word document, the DocumentBeforeSave event triggers.

We’ve been thinking this is a bug, but it turns out that it’s not – it’s by design. Read the rest of this entry »