Share via

Basic VBA Code for Word using .Paste Won't Work

Anonymous
2016-03-26T07:52:50+00:00

Windows 10 Pro Version 1511 OS Build 10586.164

Office 365 Version 16.0.6741.2017

This problem showed up when I used code written for Word 2003. I hadn't used it since switching to Office 365. When I did run it, it would not work. This much simpler code demonstrates the problem.

Sub Test_Paste()

Dim doc As Document

Set doc = ActiveDocument

With Selection

.Copy

.Collapse Direction:=wdCollapseEnd

.TypeParagraph

.Paste

End With

End Sub

When I run it, I get this error:

Run-time error "4605"

This command is not available.

and it selects ".Paste"

Any help would be greatly appreciated.

Microsoft 365 and Office | Word | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

9 answers

Sort by: Most helpful
  1. Anonymous
    2016-03-30T08:01:29+00:00

    I have 6741.2021 and have added no add-ins. As I say, something is broken somewhere. Thanks for all your input.

    Was this answer helpful?

    2 people found this answer helpful.
    0 comments No comments
  2. Anonymous
    2016-03-26T15:28:17+00:00

    Hi Doug,

    Thank you very much for your reply. I have always had something selected as I have tried to solve this. The original version of this code used a cut, and I had error handling in case of the empty selection issue. So, even after adding your suggested lines, the problem still occurs.

    Since it worked with your system, that says to me I have something broken in my setup. The question now is to figure out what and how to fix it.

    If you have any suggestions about that, I am all ears.

    Thanks,

    Bill Martz

    Was this answer helpful?

    1 person found this answer helpful.
    0 comments No comments
  3. Anonymous
    2016-03-29T10:31:18+00:00

    Thanks, Doug, for your suggestion.

    First, something is broken somewhere: I cannot use Paste in any way. I removed and reinstalled Office 365 four times without eliminating the problem.

    I tried this code, which I expected would do the actual replacement I want:  

    Sub Test_Paste2()

    Dim rng As Range

    Selection.End = Selection.End - 1

    Set rng = Selection.Range

    Selection.Style = ActiveDocument.Styles("Normal")

    rng.InsertAfter & rng.FormattedText

    End Sub

    But the line, "rng.InsertAfter & rng.FormattedText" turns red as soon as I type it. I want to put the formatted text back into the same location. I put vbCr back in, and, as expected, it pastes the text, but unformatted, into the new line.

    Bill

    Was this answer helpful?

    0 comments No comments
  4. Doug Robbins - MVP - Office Apps and Services 323.1K Reputation points MVP Volunteer Moderator
    2016-03-26T21:51:38+00:00

    Use the following:

    Dim rng As Range

    Set rng = Selection.Range

    rng.InsertAfter vbCr & rng.FormattedText

    or

    Selection.Range.InsertAfter vbCr & Selection.Range.FormattedText

    or

    With Selection.Range

        .InsertAfter vbCr & .FormattedText

    End With

    It doesn't matter then if there is nothing selected.

    Was this answer helpful?

    0 comments No comments
  5. Doug Robbins - MVP - Office Apps and Services 323.1K Reputation points MVP Volunteer Moderator
    2016-03-26T08:37:26+00:00

    It works fine as long as the selection contains something.

    The full text of the error message "The method or property is not available because no text is selected", but here in the same version of Word, it is the Copy command that throws the error.

    To prevent the error from occurring, using the following construction

    With Selection

        If .Type <> wdSelectionIP Then

            .Copy

            .Collapse Direction:=wdCollapseEnd

            .TypeParagraph

            .Paste

        End If

    End With

    Was this answer helpful?

    0 comments No comments