how do I combine these two macros?

T

Tom

I have two macros that I can't quite seem to combine. I want the macro
to add a new cell on the left of all Note style tables, and then to
write the word "noteicon" in the new cell.

This macro adds the text "noteicon" in the first cell of tables that
have the "Note" style.

Sub AddText()

Dim oTbl As Table
For Each oTbl In ActiveDocument.Tables
If oTbl.Cell(1, 1).Range.Style = _
ActiveDocument.Styles("Note") Then
With oTbl
.Cell(1, 1).Range.InsertAfter "noteicon"
End With
End If
Next oTbl
End Sub


This macro adds a new row in the first table.

Sub AddCell()
If Selection.Cells.Count = 1 Then
Selection.InsertCells ShiftCells:=wdInsertCellsShiftRight
End If
End Sub


Could someone show me how to combine these two macros into one? I want
the AddCell macro to only add a cell in "Note" table, and then to write
the word "noteicon" in the new cell. I've been trying to figure this
out for days, but haven't been successful. Thanks for any help.
 
S

Shauna Kelly

Hi Tom

I'm not sure I understand exactly what you want. Is this it:

Go through every table in the document.
If the first cell in the table is in style "Note" then
add a new row to the bottom of the table.
Apply the style "Note" to the first cell in the last row.
Insert the text "noteicon" in the first cell in the last row.
Go to the next table and re-do.

If that's not it, can you let us know just how you want the code to work?

Hope this helps.

Shauna Kelly. Microsoft MVP.
http://www.shaunakelly.com/word
 
G

Graham Mayor

New cell or new column? The following will add a new cell and types in that
cell 'Noteicon' - and effectively does what your two macros do.

It makes no account for the amount of space available for an extra
cell/column - but neither did the original.

Swap the InsertColumns line for the InsertCells line to insert a new column
to the left.

Sub AddText()
Dim oTbl As Table
For Each oTbl In ActiveDocument.Tables
If oTbl.Cell(1, 1).Range.Style = _
ActiveDocument.Styles("Note") Then
oTbl.Cell(1, 1).Range.Select
With Selection
'.InsertColumns
.InsertCells _
ShiftCells:=wdInsertCellsShiftRight
.TypeText Text:="Noteicon"
End With
End If
Next oTbl
End Sub


--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
T

Tom

Thanks Graham and Shauna. Yes, I only need to insert a cell (my table
is only one row, with 2 columns or cells).

Graham, the macro code you typed works perfectly -- except for the
width issues, as you indicated.

How would I define the width of the first and second cell? I'd like the
first cell to be .75 inches and the second cell to be 5.25 inches.
 
T

Tom

I specified the cell width by adding .Cells(1).Width = 22 below.

Sub NoteIcon()
Dim oTbl As Table
For Each oTbl In ActiveDocument.Tables
If oTbl.Cell(1, 1).Range.Style = _
ActiveDocument.Styles("Note") Then
oTbl.Cell(1, 1).Range.Select
With Selection
'.InsertColumns
.InsertCells _
ShiftCells:=wdInsertCellsShiftRight
.TypeText Text:="Noteicon"
.Style = ActiveDocument.Styles("Notetable")
.Cells(1).Width = 22
End With
End If
Next oTbl
End Sub

It should be easy to specify the width of the cell to its right (or to
specify the column widths), but I keep getting errors.

So I have resorted to this work around.

Sub FormatNotes()

Dim oTbl As Table
For Each oTbl In ActiveDocument.Tables
If oTbl.Cell(1, 1).Range.Style = _
ActiveDocument.Styles("Note") Then
With oTbl

.Style = ActiveDocument.Styles("Notetable")
.PreferredWidthType = wdPreferredWidthPercent
.PreferredWidth = 75

End With
End If
Next oTbl
End Sub


This macro sets the length of the entire table.

Is there a more graceful way of doing both of these operations in one
macro?
 
T

Tom

Oh, I think I got it.

Sub NoteIcon()
Dim oTbl As Table
For Each oTbl In ActiveDocument.Tables
If oTbl.Cell(1, 1).Range.Style = _
ActiveDocument.Styles("Note") Then
oTbl.Cell(1, 1).Range.Select
With Selection
'.InsertColumns
.InsertCells _
ShiftCells:=wdInsertCellsShiftRight
.TypeText Text:="Noteicon"
.Style = ActiveDocument.Styles("Notetable")
.Cells(1).Width = 22
End With

oTbl.Cell(1, 2).Range.Select
With Selection
'.InsertColumns
.Cells(1).Width = 378
End With
End If
Next oTbl
End Sub

Thanks for all your help. Sorry that I keep replying to my own posts
like this.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top