Microsoft Office Forums

Go Back   Microsoft Office Forums > Microsoft Word > Word VBA

Reply
 
LinkBack Thread Tools Display Modes
  #1  
Old 04-21-2012, 07:43 AM
tinfanide tinfanide is offline Windows 7 64bit Office 2010 32bit
Expert
 
Join Date: Aug 2011
Posts: 251
tinfanide is on a distinguished road
Default Word VBA: Cannot Edit Range (Delete characters except the first in a table cell)

CannotEditRange.zip

Code:
Sub t()
''' Retain every first character of each cell in Table 1


Dim oRange As Range

With Selection
    Set oRange = .Range
    With oRange
        .Start = .Start - 1
        .Delete ''' Run-time error 5904: Cannot edit Range
    End With
End With

End Sub
There's a table with 3 cells / rows.
Each row has a string like "abcde".
I want to delete all the characters except the first one.
I don't know why my codes above only show errors when the strings are inside a table cell. (If "abcde" is in a paragraph, it works)
Reply With Quote
  #2  
Old 04-21-2012, 01:56 PM
tinfanide tinfanide is offline Windows 7 64bit Office 2010 32bit
Expert
 
Join Date: Aug 2011
Posts: 251
tinfanide is on a distinguished road
Default

Code:
Dim str As String
Dim oRange As Range

With ActiveDocument
    With .Tables(1)
        For Each Cell In .Range.Cells
            With Selection
                Set oRange = Cell.Range
                With oRange

''' It seems that for table cells .Start cannot function
''' First we should .End = .End - 1 first

                    .End = .End - 1
                    .Start = .Start + 1
                    .Delete
                End With
            End With
            
        Next
    End With
End With
See the comments above.
Reply With Quote
  #3  
Old 04-24-2012, 03:59 AM
macropod macropod is online now Windows 7 64bit Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 10,027
macropod is a jewel in the roughmacropod is a jewel in the roughmacropod is a jewel in the roughmacropod is a jewel in the rough
Default

Hi tinfanide,

Try:
Code:
Sub Demo()
Dim oCel As Cell
With Selection
  If .Information(wdWithInTable) = False Then Exit Sub
  For Each oCel In .Tables(1).Range.Cells
    oCel.Range.Text = oCel.Range.Characters.First
  Next
End With
End Sub
__________________
Cheers,
Paul Edstein
[MS MVP - Word]
Reply With Quote
  #4  
Old 04-27-2012, 09:48 AM
tinfanide tinfanide is offline Windows 7 64bit Office 2010 32bit
Expert
 
Join Date: Aug 2011
Posts: 251
tinfanide is on a distinguished road
Default

Quote:
Originally Posted by macropod View Post
Hi tinfanide,

Try:
Code:
Sub Demo()
Dim oCel As Cell
With Selection
  If .Information(wdWithInTable) = False Then Exit Sub
  For Each oCel In .Tables(1).Range.Cells
    oCel.Range.Text = oCel.Range.Characters.First
  Next
End With
End Sub
Yes, I was just confusing myself when doing all the best in Range. It should have been avoided. Your approach is simpler.
Reply With Quote
Reply
Please reply to this thread with any new information or opinions.

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Set range for merged Word table cells? tinfanide Word VBA 1 02-06-2012 04:57 AM
How can I delete the content of a cell in column if the cell value is more than 1000? Learner7 Excel 2 06-27-2011 05:44 AM
Can't delete range - error 5904 expatriate Word 1 06-03-2011 12:12 AM
Edit number of characters per page after writing whole paper dea Word 1 01-04-2011 01:24 AM
Auto-populate an MS Word table cell with text from a diff cell? dreamrthts Word Tables 0 03-20-2009 01:49 PM


All times are GMT -7. The time now is 06:03 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
SEO by vBSEO ©2011, Crawlability, Inc.
MSOfficeForums.com is not affiliated with Microsoft