Consulting

Results 1 to 6 of 6

Thread: Help to paste without table formatting (learning, the hard way)

  1. #1

    Help to paste without table formatting (learning, the hard way)

    Hello,

    Continuing my haphazard intro to VBA, I was able to write a little brutish sub to copy the content of a table's cell from a document in a file, and paste it into another document. But I can't seem to figure out how to paste it as just text, without the table formatting. I am hoping someone could help with this, and if not asking too much, if they could show me if there was a more efficient way of doing this. Thanks so much!

    Sub CopyTable()
    Dim Osource As Document
    Dim Otarget As Document




    Set Otarget = ActiveDocument
    Set Osource = Documents.Open("C:\documentlocation.docx")


    Osource.Tables(2).Cell(2, 1).Range.Copy
    Otarget.Range.Paste


    End Sub

  2. #2
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,340
    Location
    Sub CopyTable()
      Dim Osource As Document
      Set Osource = Documents.Open("C:\documentlocation.docx")
      Osource.Tables(2).Cell(2, 1).Range.Copy
      ActiveDocument.PasteAndFormat wdFormatPlainText
    End Sub
    Sub CopyTableII()
    Dim Osource As Document
    Dim oRng As Range
      Set Osource = Documents.Open("C:\documentlocation.docx")
      Set oRng = Osource.Tables(1).Cell(2, 1).Range
      oRng.End = oRng.End - 1
      Selection.FormattedText = oRng.FormattedText
    End Sub
    Greg

    Visit my website: http://gregmaxey.com

  3. #3
    Thanks Greg. I was just looking at your website! Was trying to work out how to use method four of "Get Cell Text (Strip End of Cell Mark)", but I couldn't quite make it work yet. I really appreciate this.

  4. #4
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,340
    Location
    The thing is, do just want the "text" or do you want the "formattedtext" If the latter, then you would modify the function to return the actual trimmed range (not just the text):

    Sub DemoMethods()
      Selection.FormattedText = fcnGetCellText4(ActiveDocument.Tables(1).Cell(1, 1).Range).FormattedText
    lbl_Exit:
      Exit Sub
    End Sub
    Function fcnGetCellText4(ByRef oRng As Word.Range) As Range
      oRng.End = oRng.End - 1
      Set fcnGetCellText4 = oRng
    lbl_Exit:
      Exit Function
    End Function
    Greg

    Visit my website: http://gregmaxey.com

  5. #5
    Hi Greg,

    I want just the text, without formatting. In this way, I will apply custom formatting within the destination document. However, I absolutely already see a use for the other approach. Thanks again for your help.

  6. #6
    Hello,

    I am getting a "Run-time error '438': Object doesn't support this property or method" when I run the first snippet of code, FYI.



    Sub CopyTable()
    Dim Osource As Document
    Set Osource = Documents.Open("C:\documentlocation.docx")
    Osource.Tables(2).Cell(2, 1).Range.Copy
    ActiveDocument.PasteAndFormat wdFormatPlainText
    End Sub

    ***eureka***

    I was able to get what I wanted by modifying my earliest submission with the line included below. As it is so often the case, it was a simple fix:


    Otarget.Range.PasteAndFormat wdFormatPlainText

    Thank you again.
    Last edited by mellowest; 03-15-2018 at 07:10 AM. Reason: Additional information

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •