Replacing hyphens with en dashes

Jun 6, 2011 at 11:21 PM


When creating my bibliography list I used hyphens between page numbers, ie. 234-235
And now I was told to change all hyphens to longer en dashes -> 234–235

So I started changing all sources one by one but unfortunately my changes are not saved. They are saved in the left (main) list of sources but in the right list they remain with hyphens after reopening the file. So the main list contains pages with en dashes and the right one with hyphens. I also tried to copy some items from the left list and overwrite corresponding items in the right list but after reopening the file there are still hyphens on the right side.

Is there any solution to implement such changes?




Jun 11, 2011 at 1:30 PM

The fastest way is probably by abusing a bug in the Word API. Normally directly setting fields in a source shouldn't work, but if they exist, it works. So you could use the following macro:

Sub ChangePageSeparator()    
    Dim s As Source
    For Each s In ActiveDocument.Bibliography.Sources
        If InStr(s.xml, "<b:Pages>") > 0 Then
            s.field("Pages") = Replace(s.field("Pages"), "-", "–")
        End If
    Next s
End Sub 


Jun 11, 2011 at 2:23 PM

Thanks yves!

I forgot to mention that I don't use the Pages field. It was much faster for me to paste the page ranges together with the title in the Title field.
So in most cases I save everything in the Title Field like this: Adiponectin in youth: relationship to visceral adiposity, insulin sensitivity, and β-cell function. Diabetes Care 2004; 27: 547-552

So I guess it won't be so easy now to change page separators from hyphens to en dashes :-(

Jun 12, 2011 at 8:34 AM

Just add some more logic. Check the character before and after the hyphen and if they are numbers, then change it to an n-dash.

On top of my head, that would give you something like:

Sub ChangePageSeparator()
    Dim s As Source
    Dim title As String
    Dim offset As Integer
    Dim previousChar As String
    Dim nextChar As String
    For Each s In ActiveDocument.Bibliography.Sources
        If InStr(s.xml, "<b:Title>") > 0 Then
            ' Get the title.
            title = s.field("Title")
            ' Get the first dash.
            offset = InStr(1, title, "-", vbTextCompare)
            ' Walk over all dashes.
            While offset <> 0
                ' This will crash around corner cases (first or last character of your title being a dash)
                previousChar = Mid(title, offset - 1, 1)
                nextChar = Mid(title, offset + 1, 1)
                ' Change dash to n-dash if surrounded by numbers.
                If previousChar >= "0" And previousChar <= "9" And nextChar >= "0" And nextChar <= "9" Then
                    title = Mid(title, 1, offset - 1) & "–" & Mid(title, offset + 1)
                End If
                ' Get the next dash.
                offset = InStr(offset + 1, title, "-", vbTextCompare)
            ' Set the title.
            s.field("Title") = title
        End If
    Next s
End Sub
Jun 12, 2011 at 10:27 AM

Thank you so much!

The code works perfectly. But as I see it changes all separators only in the document and not in the source list. So after updating the bibliography the dashes are again replaced by hyphens.

Jun 12, 2011 at 3:53 PM

They shouldn't be. As long as you don't edit a source, there should be no connection between the source in your document and its counterpart in your master list. If there would be, you wouldn't be able to open a document on another computer. Sources between a master list and document are kept in sync by looking for a source with the same tag but a different guid code. The source with the most recent guid wins and overwrites the other version(s).

Anyway, you can run the macro for all sources in your master list as well. Just change

For Each s In ActiveDocument.Bibliography.Sources


For Each s In Application.Bibliography.Sources


Jun 12, 2011 at 4:34 PM

Indeed, I want to change page separators in both lists: in the current and in the master list. Actually, both lists contain the same bibliography items.

I used both macros and have to say that the lists are untouched. The first macro correctly changes all page separators in the document (text) but after opening the Bibliography module there are still hyphens in both lists. So after updating the Bibliography I will always loose my changes.

After running the second macro and opening the Bibliography module I saw that all hyphens were changed to dashes but only in the master list. Then I closed Word and after reopening I found hyphens in both lists.

I'm not sure but maybe the reason is that the master list is saved as a plain text to the xml file and hyphens/dashes are not recognised.