Share via

Working with Selection.Text and Selection.PasteAndFormat (wdFormatPlainText)

Anonymous
2011-11-10T20:46:34+00:00

Hello All -

The goal is to get a cleaned-up text string to the clipboard for pasting into another application.  The text has an autonumber field within it, the value of which I want to carry along.  If I Dim S as string, and

S = Selection.Text

The autonumber field is lost at the assignment.  On the other hand, if I create a scratch document into which I Selection.PasteAndFormat(wdFormatPlainText) the autonumber carries along.

Using the first way, and Data Objects I can (almost) get where I want in eight lines of code.  Using the scratch document takes 21 lines, and is slower.  This is my first way:

   Dim DataObject As New MSForms.DataObject

   Dim S       As String

   S = Selection.Text

   S = VBA.Replace$(S, vbCr, "", 1, , vbBinaryCompare)

   S = VBA.Replace$(S, vbLf, "", 1, , vbBinaryCompare)

   S = VBA.Trim$(S)

   DataObject.SetText S

   DataObject.PutInClipboard

This is the second:

   Selection.Copy

   Documents.Add DocumentType:=wdNewBlankDocument

   Selection.PasteAndFormat (wdFormatPlainText)

   Selection.Find.ClearFormatting

   Selection.Find.Replacement.ClearFormatting

   With Selection.Find

      .Text = "^p"

      .Replacement.Text = ""

      .Forward = True

      .Wrap = wdFindContinue

      .Format = False

      .MatchCase = False

      .MatchWholeWord = False

      .MatchWildcards = False

      .MatchSoundsLike = False

      .MatchAllWordForms = False

   End With

   Selection.Find.Execute Replace:=wdReplaceAll

   Selection.WholeStory

   Selection.Copy

   Application.ActiveWindow.Close SaveChanges:=wdDoNotSaveChanges

They are almost equivalent except for the handling of the autonumber field.

What am I missing as I try to align my universe?

Thanks,

Mick

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

Answer accepted by question author

Anonymous
2011-11-15T02:19:29+00:00

Sorry, I was referring to Autonumbering paragraph format, not {AUTONUM} fields.

With a regular paste, the {DOCVARIABLE} field is rendered null because the variable does not exist in the new document.

You need to unlink fields before copying, in order convert them to their equivalent values. After selecting text In the original Word document:

Selection.Fields.Unlink 'Convert to field results

Selection.Copy

ActiveDocument.Undo 'Restore fields

In case you just opened the source document to copy text from, you'll be able to close it without a "Save?" prompt using this statement:

ActiveDocument.Saved = True

Then switch to the temp document and use a regular Selection.Paste to preserve the format.

Was this answer helpful?

0 comments No comments

11 additional answers

Sort by: Most helpful
  1. Anonymous
    2011-11-12T14:31:55+00:00

    Hi John -

    Thanks, but when I add these 2 lines of code:

    Debug.Print Selection.Range.ListFormat.ListString

    Debug.Print Len(Selection.Range.ListFormat.ListString)

    First no text prints, then 0 (zero) prints, and PasteClean()'s result is no different.

    And if it were there, it would be out of place is S, I'd think.  But you've given me another place to look.  I really to like the data object approach.  I'd like it better if I could get it to work. ;-)

    ...Mick

    Was this answer helpful?

    0 comments No comments
  2. Anonymous
    2011-11-12T14:19:11+00:00

    Hi Daniel --

    Thank you.  I can appreciate the reuse of the "ClearFindAndReplaceParameters()" sub and have put it in my toolbox.  Selection.Paste does copy over the autonum field, but in the new document, it gets a new number, and it leaves behind a docvariable field ;-(.  Selection.PasteAndFormat (wdFormatPlainText) captures both, which is what I want. The formatting I'm happy to lose.

    No surprise, I'd guess, is that  Selection.PasteAndFormat (wdPasteDefault) is the same as Selection.Paste.

    I'm learning a lot. Thank you.

    ...Mick

    Was this answer helpful?

    0 comments No comments
  3. Anonymous
    2011-11-12T10:08:58+00:00

    Hi Mick:

    Here you go:

    Sub PasteClean()

    Dim DataObject As New MSForms.DataObject

    Dim S       As String

    S = Selection.Range.ListFormat.ListString & " " & Selection.Text

    S = VBA.Replace$(S, vbCr, "", 1, , vbBinaryCompare)

    S = VBA.Replace$(S, vbLf, "", 1, , vbBinaryCompare)

    S = VBA.Trim$(S)

    DataObject.SetText S

    DataObject.PutInClipboard

    End Sub

    VBA is a literal-minded beast.  Yu can't get the number from the selection text because the number is not in the selection text, it's in the ListFormat :-)

    Cheers

    Was this answer helpful?

    0 comments No comments
  4. Anonymous
    2011-11-11T03:14:36+00:00

    Over here, if the clipboard contains "{AUTONUM} plus formatted text," doing a **Selection.PasteAndFormat (wdFormatPlainText)**eliminates all formatting and converts the field to a number. Instead, if I do a simple Selection.Paste, is pastes in full.

    I recommend you add this routine, which you'll be using frequently:

    Sub ClearFindAndReplaceParameters()

    With Selection.Find

    .ClearFormatting

          .Replacement.ClearFormatting

    .Text = ""

    .Replacement.Text = ""

          .Forward = True

          .Wrap = wdFindContinue

          .Format = False

          .MatchCase = False

          .MatchWholeWord = False

          .MatchWildcards = False

          .MatchSoundsLike = False

          .MatchAllWordForms = False

       End With

    End Sub

    Then, your code becomes:

    Selection.Copy

       Documents.Add DocumentType:=wdNewBlankDocument

       Selection.Paste

       ClearFindAndReplaceParameters

    Selection.Find.Text = "^p"

       Selection.Find.Execute Replace:=wdReplaceAll

       Selection.WholeStory

       Selection.Copy

       Application.ActiveWindow.Close SaveChanges:=wdDoNotSaveChanges

    It won't be faster, but it is more compact.

    Was this answer helpful?

    0 comments No comments