Hot Stuff

With Donald Trump officially announcing the US withdrawal from the Paris Climate Agreement, I feel compelled to write a post about climate change. I do find it amusing that the earliest possible effective date of the US withdrawal is a day after the 2020 election, but Donald Trump (presumably) will be president until 20 January 2021.

Anyway, brass tacks.

Anthropogenic Global Warming Is Real

I know few who deny anymore that global warming is happening (people like this guy are obvious exceptions). The possibility was raised in 1824. Humans were producing, in 1896, as much CO2 as natural sources (e.g. volcanoes), but the same scientist who calculated this concluded that humans were, through the burning of coal, a major contributor to climate change, and in the global warming direction.

Numerous scientists began to postulate as to why the temperature of the globe seemed to be increasing, even early in the 20th century. Sunspot cycles were proposed in the 20s, but rejected by most scientists by the 30s. Milanković proposed changes in the Earth’s orbit, but these cycles were determined to be far too slow to be the major contributor to what was observed at the time.

I will note that it wasn’t until the late 50s that a majority of scientists in the field were concerned about potentially “radical” effects on the climate. While in 1968, some remained unsure whether moderate cooling effects of particulate pollution (smog) or the warming effects of CO2 emissions would dominate, a mere seven out of 51 articles published around that time predicted global cooling—the rest sided with the warming hypothesis.

1981 seems to mark the turning point where an overwhelming majority of scientists agreed with the idea that, not only was the globe warming, but that humans were the primary cause of that warming.

ExxonMobil has a nice list of documents and publications from the 70s and 80s indicating that they were/are aware of the warming impact of CO2 release. And here’s 104 more publications they contributed to. Every last one of them says increasing atmospheric carbon dioxide is causing global warming, and that humans burning fossil fuels are causing the increase in atmospheric carbon dioxide (to be fully honest, I have only read a few of these, but it gives me some confidence that I’m not entirely mischaracterizing the tenor of these papers).

I was going to make a nice long essay out of this, but I’ll just stick to saying this: It’s happening, people! Do something about it!

Word Crimes

Microsoft Word has many, many, many failings, but there are usually ways to tame it. Most of them require code. And, as is usual with writing computer programs or scripts, one piece of the puzzle is usually rather picky about one thing or another. In this case, it’s spelling, but I’ll give it a pass for that, and there are some simple workarounds which I’ll leave to the user to figure out.

Today’s conundrum: how to create a sequence, then programmatically create hyperlinked references to that sequence. Let’s pretend you want a sequence that progresses like this (and we’ll keep it simple for now):

  • Foo 00001
  • Foo 00002
  • Foo 00003
  • (etc)

Problem number 1 is creating this in the first place. If you aren’t familiar with the Sequence field code, this will be a basic primer.

In your document, press Ctrl+F9. You’ll see a pair of curly braces and your cursor will be in the middle. Note: you cannot type these curly braces! That won’t work! You must insert them with the Ctrl+F9 hotkey! Next, type SEQ Bar \# “‘Foo ‘00000”

You’ll see something like this:

{ SEQ Bar \# “‘Foo ‘00000” }

If you press Shift+F9, hopefully you’ll see your first number! We’re calling this sequence “Bar”. This is important—more important than the number formatting.

To get the next number in the sequence, there are a few options. The simplest is to copy this text and paste it elsewhere. When you do so, it’ll still say “Foo 00001”; just press F9 to update it, and it’ll refresh to the appropriate number. A second option is to use the Ctrl+F9 hotkey and retype that whole thing again. If you just use

{ SEQ Bar }

You’ll end up with “5” or whatever the sequence number happens to be at that point in the document.

But! We’re not to a programming stage. This is all well and good, and may be sufficient for many applications, but what happens when you want to reference Foo 00532 at some arbitrary location in the document? The standard answer is, create a Bookmark reference for each and every one of these Foo number sequences, each called, say, “Foo 00001”. This gets tedious really fast. When you’re creating hundreds of these, it’s not at all practical.

What you would like to be able to do, step one, is be able to insert a cross reference to this as a numbered item.

If you insert a cross reference (References tab, Captions group, Cross-reference button; Insert, Reference, Cross-reference… for Word versions prior to 2007), though, you won’t find Foo or Bar anywhere! This is a problem!

If you don’t want to do any coding, you can use the “Insert Caption” button (References tab, Caption group; or Insert, Reference, Caption… prior to 2007). In the dialog that appears, Click “New Label…” and enter “Bar”. Then click close. You don’t need to do anything more here. Now go to insert a cross reference (see previous paragraph), and, voila, “Bar” is in the Reference Type drop-down! And all sequence numbers are in the list below!

But we want to make this easy on everyone, so now we must turn to the blessing and bane of every Word expert’s existence: VBA. Step 1: add the caption label. This turns out to be simple:

Application.CaptionLabels.Add "Bar"

And, also, nicely enough, if it already exists, it won’t nag you or call you nasty names. Though, if you want to do it right, you might use one of the following methods:

Function AddCaption(label As String) As String
    Dim labeltext as Variant, i as Long
    For labeltext in Application.CaptionLabels
        If labeltext = label Then
            AddCaption = label
            Exit Function
        End If
    Next
    Application.CaptionLabels.Add label
    AddCaption = label
End Function
Function AddCaption(label As String)
    Dim test As String
    On Error Resume Next
    test = Application.CaptionLabels(label)
    If Err.Number = 5941 Then
        Application.CaptionLabels.Add label
    End If
    On Error GoTo 0
End Function

I prefer the first because it doesn’t rely on VBA’s terrible methods of handling errors.

Anyway, next step! How does one programmatically insert the next item in a sequence into the document? Again, it turns out to be fairly simple:

Selection.Collapse wdCollapseStart
ActiveDocument.Fields.Add selection.Range, wdFieldSequence, "Bar \# ""'Foo '00000""", False

First, I “collapse” the selection to the beginning. This step is optional; if a bunch of text is selected before running this code, this forces the cursor to go to the beginning of that selection instead, so whatever text is selected doesn’t get overwritten. Second, note that in VBA, doubling up the quotation marks actually inserts them into the field. Third, we don’t have to enter “SEQ”, because wdFieldSequence  does that for us.

 

One last step, to make our macro suite complete. Cross references! But we don’t want to have to go through that whole rigamarole of References tab, Captions group, Cross-reference button every time, especially when the cross reference dialog contains hundreds of the things! Type it out! Type “Foo 00532”, highlight it, and run the following macro:

Sub ChangeSelectedTextToLink()
    Dim fld As Field
    Dim findref As String
    findref = Trim(Selection.text)
    For Each fld In ActiveDocument.Fields
    If LCase(Left(fld.Code, 8)) = " seq bar" Then
        If fld.Result.text = Selection Then
            Selection.InsertCrossReference "Bar", wdEntireCaption, Int(Val(Right(fld.Result.text, 4))), True, False
            Exit Sub
        End If
    End If
    MsgBox "Can't find a field numbered '" & findref & "'.", vbInformation
End Sub

Be sure to update the appropriate sections (lines 6 and 8, specifically) when you create your named sequence.

I’m sure that this whole thing can be made more generic, but that’s not something I’m up to doing right now, so I’ll leave that as an exercise to the reader.

Some resources to help you out:

Also (and as a separate note), for those interested in making a custom ribbon, you’ll want a reference that shows you what the imageMso values actually look like, and since Microsoft isn’t nice enough to provide one, I finally found someone who is, here.