VBA to delete carriage return at end of selection

I have VBA to convert tables to text but it crashes if the user selects the table with the carriage return after the table. Can someone please provide the code to delete the carriage return at the end of a selection please? I searched the database and have not been able to find it. Thanks in advance!

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

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).
Carl

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?
---
Best wishes, HansV
https://www.eileenslounge.com

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



Carl

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.

Cheers
Paul Edstein
(Fmr MS MVP - Word)

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.

Greg Maxey
***
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

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.

Thanks so much. That explains why I could not find your reply to answer. I took another look at the code by Hans and used that instead. I really appreciate your help.
Carl

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



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!
Carl

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

Greg Maxey
***
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

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: