Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 01-28-2012, 07:03 PM
macropod's Avatar
macropod macropod is offline How to select the first row of the current table Windows 7 64bit How to select the first row of the current table Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 22,381
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

Hi Jennifer,

Your code could be simplified a bit. In particular, there's no need to select the whole table before working on it. Aside from executing faster, not selecting it has the advantage of leaving the selected range unchanged:


Code:
Sub MyTableSettings()
Const MyName = "MyTableSettings"
Dim SettingBreakOld                 'Old setting
Dim SettingAutoFitOld               'Old setting
Dim SettingHeaderRowOld As Boolean  'Old setting
Dim Msg
'Abort if the cursor is not in a table
If Selection.Information(wdWithInTable) = False Then
  MsgBox "The cursor is not in a table", vbOKOnly, MyName
  Exit Sub
End If
'Save old settings
With Selection.Tables(1)
  SettingBreakOld = (.Rows.AllowBreakAcrossPages = True)
  SettingAutoFitOld = (.AllowAutoFit = True)
  SettingHeaderRowOld = (.Rows(1).HeadingFormat = True)
  'Set the new ones
  .Rows.AllowBreakAcrossPages = False                  'Stop rows from breaking
  .AllowAutoFit = False
  'Ask if they want a header row?
  Select Case MsgBox("Set row 1 as header?", vbYesNoCancel, MyName)
    Case vbYes
      .Rows(1).HeadingFormat = True
    Case vbNo
      .Rows(1).HeadingFormat = False
    Case vbCancel
      MsgBox "Aborted", vbOKOnly, MyName
      Exit Sub
  End Select
  'Report the results
  Msg = "Autofit = True (was " & SettingAutoFitOld & "), " & vbCrLf & _
      "Break = True (was " & SettingBreakOld & "), " & vbCrLf & _
      "Header row = " & (.Rows(1).HeadingFormat = True) & " (was " & SettingHeaderRowOld & ")"
  MsgBox Msg, vbOKOnly, MyName
End With
End Sub
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #2  
Old 01-28-2012, 08:12 PM
Jennifer Murphy's Avatar
Jennifer Murphy Jennifer Murphy is offline How to select the first row of the current table Windows XP How to select the first row of the current table Office 2007
Competent Performer
How to select the first row of the current table
 
Join Date: Aug 2011
Location: Silicon Valley
Posts: 234
Jennifer Murphy is on a distinguished road
Default

Paul,

Thanks for the suggestions.

The reason I selected the whole table was because I wanted the "Do not break" option to be applied to all rows. From your code, it appears that the

Code:
With Selection.Tables(1)
 
   ...
 
End With
acts as if the table were selected within that With block. Is that correct? This is indeed much better code.

The other change that I noticed is you changed my test from "<> True" to "= False". Is there a reason for that? I wrote it my way in case there is some other possibility besides True and False, such as Null or Empty. I have been burned before, especially with VBA, when the application (Word) returns odd results. I want to do something if it's true only.
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to select the first row of the current table How to call current PC date and/or current PC year KIM SOLIS Excel 2 11-04-2011 06:09 PM
How to select the first row of the current table Select Text in Table but Table Gets Selected Too RBusiness Word 1 06-07-2011 04:26 PM
How to select the first row of the current table Access to the property of the current table b0x4it Word VBA 2 05-26-2011 06:25 AM
Table - Check Boxes - Create Select All lajohn1963 Word Tables 2 09-25-2010 11:18 AM
How to select the first row of the current table Auto insert current month's name and current year Styler001 Word 4 01-25-2010 06:40 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 09:25 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2025, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft