Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / September 2006

Tip: Looking for answers? Try searching our database.

Autofilling Pre-Determined Text into Selected Table Cells by a Macro

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
roy - 05 Sep 2006 16:57 GMT
Hello,

I have a simple idea, which I'm hoping someone can help me with.

I have a table with several rows and columns.  I would like to be able
to select a cell or multiple cells and have a macro:

(1)  Delete the contents of the cell(s).
(2)  Autofill the selected cell(s) with a specific, formatted text
(e.g. "TEST").

If anyone can help me, I would be so appreciative!

Thanks,
Roy
Greg Maxey - 05 Sep 2006 17:20 GMT
Not extensively tested and will likely crash in a non-uniform table:

Sub ScratchMacro()
Dim oColS As Long, oRowS As Long, oColE As Long, oRowE As Long
Dim i As Long, j As Long
Dim oTbl As Word.Table
 With Selection
   If .Information(wdWithInTable) Then
     oColS = .Information(wdStartOfRangeColumnNumber)
     oRowS = .Information(wdStartOfRangeRowNumber)
     oColE = .Information(wdEndOfRangeColumnNumber)
     oRowE = .Information(wdEndOfRangeRowNumber)
   Else
     Exit Sub
   End If
 End With
Set oTbl = Selection.Tables(1)
For i = oRowS To oRowE
 For j = oColS To oColE
   oTbl.Cell(i, j).Range.Text = "TEST"
  Next j
Next i
End Sub

> Hello,
>
[quoted text clipped - 11 lines]
> Thanks,
> Roy
roy - 05 Sep 2006 17:33 GMT
Hi Greg,

This is great!  Is there anyway to format the text?  For example,
change the font size and make it bold?

Thanks,
Roy

> Not extensively tested and will likely crash in a non-uniform table:
>
[quoted text clipped - 35 lines]
> > Thanks,
> > Roy
Greg Maxey - 05 Sep 2006 17:51 GMT
Yes.  While you are working with the cell range you can do whatever you
want with the text formatting:

Sub ScratchMacro()
Dim oColS As Long, oRowS As Long, oColE As Long, oRowE As Long
Dim i As Long, j As Long
Dim oTbl As Word.Table
If Not Selection.Information(wdWithInTable) Then
 Exit Sub
Else
 Set oTbl = Selection.Tables(1)
End If
If Not oTbl.Uniform Then
 If MsgBox("This table contains merged or split cells which may result
in erratic results." _
    & " Do you wish to continue?", vbYesNo, "Irregular Table!") =
vbYes Then
   With Selection
     oColS = .Information(wdStartOfRangeColumnNumber)
     oRowS = .Information(wdStartOfRangeRowNumber)
     oColE = .Information(wdEndOfRangeColumnNumber)
     oRowE = .Information(wdEndOfRangeRowNumber)
   End With
 Else
   Exit Sub
 End If
End If
Set oTbl = Selection.Tables(1)
For i = oRowS To oRowE
 For j = oColS To oColE
   With oTbl.Cell(i, j).Range
     .Text = "TEST"
     With .Font
       .Name = "Arrial Narrow"
       .Size = 18
       .Bold = True
     End With
   End With
 Next j
Next i
End Sub

> Hi Greg,
>
[quoted text clipped - 43 lines]
> > > Thanks,
> > > Roy
Greg Maxey - 05 Sep 2006 17:45 GMT
As expected you may encounter erratic results in a non-uniform table
(table with split or merged cells).  I don't know how you could work
around this, but you could add some flags to the macro to make you
awared of them:

Sub ScratchMacro()
Dim oColS As Long, oRowS As Long, oColE As Long, oRowE As Long
Dim i As Long, j As Long
Dim oTbl As Word.Table
If Not Selection.Information(wdWithInTable) Then
 Exit Sub
Else
 Set oTbl = Selection.Tables(1)
End If
If Not oTbl.Uniform Then
 If MsgBox("This table contains merged or split cells which may result
in erratic results." _
    & " Do you wish to continue?", vbYesNo, "Irregular Table!") =
vbYes Then
   With Selection
     oColS = .Information(wdStartOfRangeColumnNumber)
     oRowS = .Information(wdStartOfRangeRowNumber)
     oColE = .Information(wdEndOfRangeColumnNumber)
     oRowE = .Information(wdEndOfRangeRowNumber)
   End With
 Else
   Exit Sub
 End If
End If
Set oTbl = Selection.Tables(1)
For i = oRowS To oRowE
 For j = oColS To oColE
   oTbl.Cell(i, j).Range.Text = "TEST"
 Next j
Next i

> Hello,
>
[quoted text clipped - 11 lines]
> Thanks,
> Roy
roy - 05 Sep 2006 17:47 GMT
Hi Greg,

Thank you for working so hard on this!  I really appreciate it.  I did
figure out the formatting issue (bold, different font size, etc.).

Thanks,
Roy

> As expected you may encounter erratic results in a non-uniform table
> (table with split or merged cells).  I don't know how you could work
[quoted text clipped - 47 lines]
> > Thanks,
> > Roy
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.