Macro: Remove Spacing Between Table Cells

I received a document that had hundreds of tables, all with a nice 3D border. Then somebody said that the border looked ugly so I started writing a macro to change all the borders to something more plain. Changing the borders was simple, but the cells had space between them which resulted in a double border. Getting rid of this space was not so simple.
Finally, I found this little macro at the Egghead Cafe:
With Dialogs(wdDialogTableTableOptions)
.AllowSpacing = 0
.Execute
End With
 
A second method (that I did not try) is to use this:

Selection.Tables(1).Spacing = -1
 
The full macro follows:
Sub remove3dBorder()
'
' remove3dBorder Macro
'
'
Dim myT As Table

For Each myT In ActiveDocument.Tables

myT.Select

'Selection.Rows.HeadingFormat = True
With Selection.Tables(1)
With .Borders(wdBorderLeft)
.LineStyle = wdLineStyleSingle
.LineWidth = wdLineWidth050pt
.Color = wdColorAutomatic
End With
With .Borders(wdBorderRight)
.LineStyle = wdLineStyleSingle
.LineWidth = wdLineWidth050pt
.Color = wdColorAutomatic
End With
With .Borders(wdBorderTop)
.LineStyle = wdLineStyleSingle
.LineWidth = wdLineWidth050pt
.Color = wdColorAutomatic
End With
With .Borders(wdBorderBottom)
.LineStyle = wdLineStyleSingle
.LineWidth = wdLineWidth050pt
.Color = wdColorAutomatic
End With
With .Borders(wdBorderHorizontal)
.LineStyle = wdLineStyleSingle
.LineWidth = wdLineWidth050pt
.Color = wdColorAutomatic
End With
With .Borders(wdBorderVertical)
.LineStyle = wdLineStyleSingle
.LineWidth = wdLineWidth050pt
.Color = wdColorAutomatic
End With
.Borders(wdBorderDiagonalDown).LineStyle = wdLineStyleNone
.Borders(wdBorderDiagonalUp).LineStyle = wdLineStyleNone
.Borders.Shadow = False

.TopPadding = CentimetersToPoints(0)
.BottomPadding = CentimetersToPoints(0)
.LeftPadding = CentimetersToPoints(0)
.RightPadding = CentimetersToPoints(0)
With Dialogs(wdDialogTableTableOptions)
.AllowSpacing = 0
.Execute
End With

.AllowPageBreaks = True
.AllowAutoFit = True
End With

' turn on header row
myT.Cell(1, 1).Select
Selection.Rows.HeadingFormat = True

Next myT
End Sub

[Source Egghead Cafe and Google Answers] 

Word: Random Lorem Ipsum Text

Did you ever need to insert some random text into a document? Here are two magic ways to create random text in Word 2007 & 2010:
Random Text: 
Type “=rand(x,y)” in your document and then hit Enter.
Lorem Ipsum:
Type “=lorem(x,y)” in your document and then hit Enter.
x is the number of paragraphs.
y is the number of sentences per paragraph.

Macro: Repetitive Replacing of Table Cells

Here is today’s time saver:
I had some tables (actually 1000 pages of tables!) where I needed to replace cells containing various text (denoted by comments from the SME) with a standard word (which was Reserved in this case). Normally, I would triple-click in the cell and then control-v to paste in the text (5 clicks/keys), but there were too many places that I needed to make this change.
Using the following macro this task was reduced to two clicks, once in the cell and then once on the button for the macro. There was one other requirement for this macro, which was that if the cell was in bold, it received slightly different text.

Sub makeCellReserved()
   Selection.SelectCell

   If Selection.Range.Bold = True Then
       Selection.Text = “RESERVED”
   Else
       Selection.Text = “Reserved”
   End If
End Sub

Filename Field Code in Word

Many times I’ll use a field code to insert the filename into the footer of a document. The field code looks like this:

{ FILENAME \*MERGEFORMAT}
The result of the field code looks like this: foobar.doc

The filename has a “.doc” suffix. When I create the PDF, the filename in the footer still has the “.doc” suffix, and not “.pdf”.

What I found is that the field code uses the filename exactly as it appears in the Windows Explorer window. This may sound trivial, but to solve the problem we just need to hide the extension in Windows Explorer.
To change the setting follow these steps:
  1. Open Windows Explorer
  2. Click Tools -> Folder Options
  3. Click on the View tab
  4. Check Hide extensions for known file types
Now, after updating the field code, we get foobar instead of foobar.doc.

[Source Wordribbon.Tips.net]

Selecting a Text Block

I just came across the shortcut of the year for MS Word over at TechRepublic.  Normally, when selecting text, you select it in a continuous line.  You can expand the selection forwards or backwards.
But, did you know that you can select a text block?  This is one of the powerful features of Vim for those of you who have used Unix computers.
Just hold the ALT key while you click.

For example, if I have the following text:

REM First  line
REM Second line

REM Third  line

You can hold the ALT key and then select the first three letters of each sentence:
REM First  line
REM Second line

REM Third  line

You can also cut and paste a block of text. In the above example, one could cut the selection and then paste it in the middle of the first line, resulting in:

 First REM line
 SecondREM line

 Third REM line

Note that selection of text blocks does not work inside of tables.

[Source TechRepublic]

Too Much Style

Police Officer: You in the red Corvette! Pull over immediately.
Xander Cage: Yeah, yeah. These monkeys are following me because I just took this car. Obviously the car doesn’t belong to me, it’s not my style
If you are overwhelmed by the number of styles in Word 2007, you can configure Word to show only the styles that are currently being used in the document:
  1. Alt + Ctrl + Shift + s  (opens the style box)
  2. Click on Options on the bottom right
  3. Under Select styles to show choose In use

Now, in the panel on the right you only see what is currently being used in the document.  The ribbon at the top still gives you access to all the styles.  If you apply a style that is not currently being used, then it gets added to the panel on right.

You can also use Ctrl + Shift + s to pop up a window where you can type in a style name. This window has auto complete, so just start typing and the closest match to the style name will fill in.

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

Saving images from MS Word 2007

What’s the easiest way to save images that are in an MS Word 2007 file?  Word 2007 changed the file format from .doc to .docx, which is really just a zip file (Like Java jar files).  Just change the file extension from .docx to .zip and then extract it with any Zip program (like Winzip).  Inside the extracted directory there is a sub-directory with all the graphics in the file along with XML files that describe the document.

[Tip from Graham Mayor]