Word: Show Me My Property

Word documents have many user-accessible properties, such as Author, Title, etc.  One can find them by clicking on the Word Button -> Prepare -> Properties.  These properties can also be accessed by a macro using the BuiltInDocumentProperties method.
This post will show you which properties are available and how to access them using macros.

To access individual properties, use this:
Sub showProperty()
    Set myProp = ActiveDocument.BuiltInDocumentProperties(wdPropertyAuthor)
    ‘MsgBox (“Value of this property ->” + myProp + “<-")
End Sub
(I like to enclose my displayed values with some character, like an angle bracket, to make it easy to see any preceding or trailing spaces)
In place of wdPropertyAuthor, you can use any of the following properties:
  • wdPropertyAppName
  • wdPropertyAuthor
  • wdPropertyBytes
  • wdPropertyCategory
  • wdPropertyCharacters
  • wdPropertyCharsWSpaces
  • wdPropertyComments
  • wdPropertyCompany
  • wdPropertyFormat
  • wdPropertyHiddenSlides
  • wdPropertyHyperlinkBase
  • wdPropertyKeywords
  • wdPropertyLastAuthor
  • wdPropertyLines
  • wdPropertyManager
  • wdPropertyMMClips
  • wdPropertyNotes
  • wdPropertyPages
  • wdPropertyParas
  • wdPropertyRevision
  • wdPropertySecurity
  • wdPropertySlides
  • wdPropertySubject
  • wdPropertyTemplate
  • wdPropertyTimeCreated
  • wdPropertyTimeLastPrinted
  • wdPropertyTimeLastSaved
  • wdPropertyTitle
  • wdPropertyVBATotalEdit
  • wdPropertyWords
To list all properties in the document, run this macro.  It will put them at the end of the document.
Sub ListProperties()
    Dim rngDoc As Range
    Dim proDoc As DocumentProperty

    Set rngDoc = ActiveDocument.Content

    rngDoc.Collapse Direction:=wdCollapseEnd

    For Each proDoc In ActiveDocument.BuiltInDocumentProperties
        With rngDoc
            .InsertParagraphAfter
            .InsertAfter proDoc.Name & “= “
            On Error Resume Next
            .InsertAfter proDoc.Value
        End With
    Next
End Sub

Back From the Future

Lou: You gonna order something, kid?
Marty McFly: Ah, yeah… Give me – Give me a Tab.
Lou: Tab? I can’t give you a tab unless you order something.
Marty McFly: All right, give me a Pepsi Free.
Lou: You want a Pepsi, PAL, you’re gonna pay for it.  
Here is a simple tip that can save a lot of time. If you click on a link in a PDF file, how to you go back to where you started?
The answer: Alt + Left arrow
Example: If you are on page 2  and you click a link that takes you to page 155, Alt + left arrow takes you back to page 2.
If you want to go back to the future, uh, I mean back to the page that you went to before, then use Alt + Right arrow.

Preventing Emails with Blank Subject Lines in Outlook

Here is a nice fix to prevent Outlook from sending emails without anything in the subject:
Save yourself the annoyance by following the next steps.
  1. Open the Visual Basic Editor (in Outlook, press ALT+F11).
  2. On the left-hand side of the screen, expand the ‘Project1 (go into ‘Microsoft Office Outlook Objects’ -> ‘ThisOutlookSession’).
  3. Double click on ‘ThisOutlookSession’
  4. Paste the following lines of code in the right-hand pane of the screen:
Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)
    If Item.Subject = “” Then
    Cancel = MsgBox(“This message does not have a subject.” & vbNewLine & “Do you wish to continue sending anyway?”, vbYesNo + vbExclamation, “No Subject”) = vbNo
    End If
End Sub

Save the project (‘File’ -> ‘Save’) and return to Outlook. From now on, the code will be called every time you press the send-button after you composed an email. Outlook will pop up a warning when you try sending an email with an empty subject line.

Macros: Moving the Selection

I taught myself how to use macros (through lots of web searching and testing lots of bad code in Word), and therefore, there are some basics that were left out along the way.  Today’s post is about one of those missing pieces: moving the selection.
Once something is selected, the selection can be changed by issuing a command with the following parameters:

  1. Move type
  2. Move unit
  3. Count
  4. Extend 

 “Move type” is one of MoveLeft/Right/Up/Down.  This gives us a direction to use as a reference for the rest of the commands.  The move will start with the end of the selection.
“Move unit” specifies the units for the count parameter.
“Count” specifies how many times we repeat the move.
“Extend” can either be wdExtend or wdMove.  Using wdMove removes the selection (not the text, just the selection highlighting) and places the cursor at the end of the selection.


Now let’s put it all together. I’ll use the example below to demonstrate some of the possibilities. I have some text where there is a special word (Phlogtastic) that I want to change the first five characters to italics (so that it will be Phlogtastic).

Here is the sample macro:

Sub makeFirstPartofWordItalic()
‘ fix the headings that don’t have italics

For Each myWord In ActiveDocument.Words
    If myWord = “Phlogtastic ” Then
        myWord.Select
   
        ‘Option 1: Using MoveLeft
        Selection.MoveLeft Unit:=wdCharacter, Count:=7, Extend:=wdExtend
       
        ‘Option 2: Using MoveRight
        Selection.MoveRight Unit:=wdCharacter, Count:=-7, Extend:=wdExtend
       
        ‘Option 3: Using Combinations
        Selection.MoveLeft Unit:=wdWord, Count:=1
        Selection.MoveRight Unit:=wdCharacter, Count:=5, Extend:=wdExtend

        Selection.Font.Italic = True
    End If
Next myWord

End Sub

The macro searches the text until it finds that word that we want.  Next, I give three  different ways to selection the desired part of the text.  All three options use “Phlogtastic ” as the initial selection (including the trailing space).
Option 1: We start at the end of the selection.  Next, move the end of the selection to the left by 6 characters, resulting in “Phlogtastic “.
Option 2: We start at the end of the selection.  Next, move the end of the selection to the right by negative 6 characters, resulting in “Phlogtastic”.  Note that a negative move to the right is a positive move to the left.

Option 3: Note that in the previous cases we used the wdExtend parameter which changed the what is selected.  In this case, we are going to create a whole new selection.  First, the move done without the wdExtend parameter so the cursor is placed at the end of the selection and then moved.  The move unit is in words instead of characters, so moving to the left by one word give us |Phlogtastic” with the cursor at the beginning of the word (before the ‘P’).  Next, we expand the selection by moving five characters to the right, resulting in Phlogtastic”.  

Lastly, we change the font to italics.

Happy Moving 🙂

Super Copyrights

Today I changed a term in one of my documents to use a fancy, new term which has a copyright.  For some reason (which I’ll ignore for now) when I replaced the term, the copyright symbol was not put into superscript. So, macros to the rescue:

Sub copyrightToSuperscript()

‘ copyrightToSuperscript Macro

‘ Change all copyright symbols to superscript

    Selection.Find.ClearFormatting
    Selection.Find.Replacement.ClearFormatting
    With Selection.Find.Replacement.Font
        .Superscript = True
        .Subscript = False
    End With
    With Selection.Find
        .Text = “©”
        .Replacement.Text = “©”
        .Forward = True
        .Wrap = wdFindContinue
        .Format = True
        .MatchCase = False
        .MatchWholeWord = False
        .MatchKashida = False
        .MatchDiacritics = False
        .MatchAlefHamza = False
        .MatchControl = False
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
    End With
    Selection.Find.Execute Replace:=wdReplaceAll

End Sub

I originally thought that I would need to use the ASCII code for the symbol, but the macro works just fine as written above.