Converting the History of the Universe into an eBook using Visual Basic for Applications

I don’t think it’s possible to produce eBooks, especially ones with embedded images, without a great deal of technical work. For example, I’m trying to produce a PDF of my new History of the Universe ebook to send to reviewers, and I find the images are too large. There are dozens of them. How to resize them all? There is only one way: write a macro using the Visual Basic for Applications (VBA) language. Luckily I’m something of an expert in this. I taught several courses at Coventry University. But what would I do without such knowledge?

The same goes for producing effective Kindle eBooks. To make the NCX (I’ll explain what that is some other time) I had to write a Visual Basic application. It’s just not easy to do this stuff! The author-publisher needs such a huge range of skills I’d say it’s almost impossible to possess them all. In my case, for example, it’s the marketing skills I lack.

But luckily I think I have all the writing skills needed!

And for those who might be interested in such things, here is my current version of the VBA code:

Sub reSizeImages()


‘ 07/05/2012 by PhilBrown
‘ Adjust scale factor of images so not wider than preset maximum

Dim doubleScaleFactor As Double
Dim iMaxWidth As Integer

‘ set max width in points
‘ convert inches to points @ 72 per inch
iMaxWidth = 4.7 * 72

‘ Go to top of document
Selection.HomeKey Unit:=wdStory

‘ find next image
Selection.find.ClearFormatting
With Selection.find
.Text = “^g”
.Replacement.Text = “”
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.find.Execute

Do While Selection.InlineShapes.Count > 0
Debug.Print “Width =” & Selection.InlineShapes(1).Width
If Selection.InlineShapes(1).Width > iMaxWidth Then
doubleScaleFactor = iMaxWidth / Selection.InlineShapes(1).Width
Debug.Print “Old ScaleHeight =” & Selection.InlineShapes(1).ScaleHeight
Selection.InlineShapes(1).ScaleHeight = doubleScaleFactor * Selection.InlineShapes(1).ScaleHeight
Selection.InlineShapes(1).ScaleWidth = doubleScaleFactor * Selection.InlineShapes(1).ScaleWidth
Debug.Print “New ScaleHeight =” & Selection.InlineShapes(1).ScaleHeight
End If
‘ move past that image otherwise endless loop!
Selection.MoveRight wdCharacter, 1, False
Selection.find.Execute
Loop

End Sub


Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>