April 9, 2024
Contribute to the Microsoft 365 and Office forum!
Click here to learn more 💡
May 10, 2024
Word Forum Top Contributors:
VBA to delete carriage return at end of selection
Report abuse
Thank you.
Reported content has been submitted
If you work with the current selection, you can use
If Selection.Tables.Count = 0 Then
MsgBox "Please select a table!", vbExclamation
Exit Sub
End If
Selection.Tables(1).Select
After this, the Selection will consist of one table only.
Best wishes, HansV
https://www.eileenslounge.com
Report abuse
Thank you.
Reported content has been submitted
Was this reply helpful?
Sorry this didn't help.
Great! Thanks for your feedback.
How satisfied are you with this reply?
Thanks for your feedback, it helps us improve the site.
How satisfied are you with this reply?
Thanks for your feedback.
If you work with the current selection, you can use
If Selection.Tables.Count = 0 Then
MsgBox "Please select a table!", vbExclamation
Exit Sub
End If
Selection.Tables(1).Select
After this, the Selection will consist of one table only.
Thanks so much but this does not solve the problem exactly. I really appreciate the good code (I can use it on other things but not this).
Report abuse
Thank you.
Reported content has been submitted
Was this reply helpful?
Sorry this didn't help.
Great! Thanks for your feedback.
How satisfied are you with this reply?
Thanks for your feedback, it helps us improve the site.
How satisfied are you with this reply?
Thanks for your feedback.
Best wishes, HansV
https://www.eileenslounge.com
Report abuse
Thank you.
Reported content has been submitted
Was this reply helpful?
Sorry this didn't help.
Great! Thanks for your feedback.
How satisfied are you with this reply?
Thanks for your feedback, it helps us improve the site.
How satisfied are you with this reply?
Thanks for your feedback.
Can you explain why it doesn't solve your problem?
I actually used the following code sent by Greg Maxey. Yours was on the right track but I did not have the expertise to make it work so used Greg's. I am sure there is nothing wrong with your code, the problem was mine. Thanks so much!
Sub Marcro()
Dim oRng As Word.Range
Dim oTbl As Word.Table
Set oRng = Selection.Range
On Error GoTo Err_NoTable
Set oTbl = oRng.Tables(1)
Do While Not oRng.Characters.Last.InRange(oTbl.Range)
oRng.MoveEnd wdCharacter, -1
Loop
oRng.Select
With Selecion
'Your code
End With
Exit Sub
Err_NoTable:
MsgBox "Please select a table"End Sub
Report abuse
Thank you.
Reported content has been submitted
Was this reply helpful?
Sorry this didn't help.
Great! Thanks for your feedback.
How satisfied are you with this reply?
Thanks for your feedback, it helps us improve the site.
How satisfied are you with this reply?
Thanks for your feedback.
The following would work equally well:
Sub Demo()
Dim Rng As Range
Set Rng = Selection.Range
With Rng
If .Tables.Count = 0 Then
MsgBox "Please select a table!", vbExclamation
Exit Sub
Else
While .Characters.Last.Information(wdWithInTable) = False
.MoveEnd wdCharacter, -1
Wend
.Tables(1).ConvertToText Separator:=wdSeparateByTabs, Nestedtables:=True
End If
.Select
End With
End Sub
Note that it is impossible to delete the last paragraph break in a document. Also, the While ... Wend block isn't needed for the conversion; it only reduces the selected range so that all you're left with as the selection is the converted table.
Paul Edstein
(Fmr MS MVP - Word)
Report abuse
Thank you.
Reported content has been submitted
Was this reply helpful?
Sorry this didn't help.
Great! Thanks for your feedback.
How satisfied are you with this reply?
Thanks for your feedback, it helps us improve the site.
How satisfied are you with this reply?
Thanks for your feedback.
Cari/Hans
I deleted my code almost immediately after posting after I realized that Hans' got to the same point with much less code by simply adding a new line With Selection ;-)
Glad I could help just the same.
***
Death smiles at us all, but all a man can do is smile back.
For more help with Word visit:
http://gregmaxey.com/word_tips.html
Report abuse
Thank you.
Reported content has been submitted
Was this reply helpful?
Sorry this didn't help.
Great! Thanks for your feedback.
How satisfied are you with this reply?
Thanks for your feedback, it helps us improve the site.
How satisfied are you with this reply?
Thanks for your feedback.
Report abuse
Thank you.
Reported content has been submitted
Was this reply helpful?
Sorry this didn't help.
Great! Thanks for your feedback.
How satisfied are you with this reply?
Thanks for your feedback, it helps us improve the site.
How satisfied are you with this reply?
Thanks for your feedback.
I apologize for not recognizing the simply genius in your code. I tried it again after Greg's post and have it working now. It works great and solved a big problem for me. Thanks so much!Can you explain why it doesn't solve your problem?I actually used the following code sent by Greg Maxey. Yours was on the right track but I did not have the expertise to make it work so used Greg's. I am sure there is nothing wrong with your code, the problem was mine. Thanks so much!Sub Marcro()
Dim oRng As Word.Range
Dim oTbl As Word.Table
Set oRng = Selection.Range
On Error GoTo Err_NoTable
Set oTbl = oRng.Tables(1)
Do While Not oRng.Characters.Last.InRange(oTbl.Range)
oRng.MoveEnd wdCharacter, -1
Loop
oRng.Select
With Selecion
'Your code
End With
Exit Sub
Err_NoTable:
MsgBox "Please select a table"End Sub
Report abuse
Thank you.
Reported content has been submitted
Was this reply helpful?
Sorry this didn't help.
Great! Thanks for your feedback.
How satisfied are you with this reply?
Thanks for your feedback, it helps us improve the site.
How satisfied are you with this reply?
Thanks for your feedback.
Carl
I apologize for calling you Cari :-(
Just for completeness, here is another version. It has a few more lines of code than Hans' original, but will hopefully illustrate that errors in VBA aren't always bad. We expect a table to be selected. When one isn't or if the cursor isn't even in a
table, we use the error resulting from that fact to provide useful information to the user.
Sub Demo()
On Error GoTo Err_NoTable
Selection.Tables(1).Select
With Selection
'Your code.
End With
Err_ReEntry:
Exit Sub
Err_NoTable:
MsgBox "Please select or place the cursor in the table you want to process"
Resume Err_ReEntry
End Sub
***
Death smiles at us all, but all a man can do is smile back.
For more help with Word visit:
http://gregmaxey.com/word_tips.html
Report abuse
Thank you.
Reported content has been submitted
Was this reply helpful?
Sorry this didn't help.
Great! Thanks for your feedback.
How satisfied are you with this reply?
Thanks for your feedback, it helps us improve the site.
How satisfied are you with this reply?
Thanks for your feedback.
Question Info
Last updated October 5, 2021 Views 1,419 Applies to: