View Full Version : Solved: Inserting Quotations in Word Table on each Row
paella
07-24-2012, 08:41 AM
Hello everyone,
 
I have very large WORD tables in a document.  I am looking for help in writing a macro that will insert quotations around the text in the 4th cell of EACH row in the table.
 
I have a macro to insert the quotations, but I have to select the cell myself (and with upwards of 300 rows, this is very time consuming).  I thought I had figured out how to run through each row, but alas, I don't. 
 
Your help, as always, is so very very much appreciated.  
 
Thanks!
gmaxey
07-24-2012, 05:23 PM
Sub ScratchMacro()
'A quick macro scratch pad created by Greg Maxey
Dim i As Long
Dim oRng As Word.Range
For i = 1 To ActiveDocument.Tables(1).Rows.Count
  Set oRng = ActiveDocument.Tables(1).Cell(i, 4).Range
  oRng.MoveEnd 1, -1
  If Len(oRng.Text) > 0 Then
    oRng.Text = Chr(34) & oRng.Text & Chr(34)
  End If
Next i
End Sub
macropod
07-24-2012, 05:33 PM
Safer, as it preserves any existing formatting:
Sub ScratchMacro()
    Dim i As Long
    Dim oRng As Word.Range
    For i = 1 To ActiveDocument.Tables(1).Rows.Count
        Set oRng = ActiveDocument.Tables(1).Cell(i, 4).Range
        oRng.MoveEnd 1, -1
        If Len(oRng.Text) > 0 Then
            oRng.InsertAfter Chr(34)
            oRng.InsertBefore Chr(34)
        End If
    Next i
End Sub
gmaxey
07-24-2012, 05:44 PM
If we are adding safety features then we can add polish too ;-)
Don't add quotes if quotes already there.
Sub ScratchMacro()
    Dim i As Long
    Dim oRng As Word.Range
    For i = 1 To ActiveDocument.Tables(1).Rows.Count
        Set oRng = ActiveDocument.Tables(1).Cell(i, 4).Range
        oRng.MoveEnd 1, -1
        If Len(oRng.Text) > 0 Then
           If Not oRng.Characters.Last = Chr(34) Then
             oRng.InsertAfter Chr(34)
           End If
           If Not oRng.Characters.First = Chr(34) Then
            oRng.InsertBefore Chr(34)
           End If
        End If
    Next i
End Sub
macropod
07-24-2012, 05:57 PM
Hi Greg,
 
Some of the shine might come off if the content includes a quote that, say, ends the text but doesn't start it (eg: He said "Stop it."):whip
gmaxey
07-24-2012, 06:10 PM
That is true and only illustrates how often someone can ask for "x" and really want "X" or "XYZ"
paella
07-25-2012, 08:38 AM
WELL! You folks have provided me with exactly what I asked for and then some (with a side plate of humor - does that cost extra?!) - and for that I am very grateful ;)
 
Thank you very much.
 
Now, if I were to ask for "X" instead of "x" it would be to not add quotations where the cell is empty :P - NEVERMIND - I see that it actually does this already...I had a 'space' in the cell and thought it was empty.  Der. 
 
A million thanks again
Powered by vBulletin® Version 4.2.5 Copyright © 2025 vBulletin Solutions Inc. All rights reserved.