PDA

View Full Version : [SOLVED:] Help to paste without table formatting (learning, the hard way)



mellowest
03-13-2018, 07:26 AM
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

gmaxey
03-13-2018, 10:30 AM
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

mellowest
03-13-2018, 12:17 PM
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.

gmaxey
03-13-2018, 03:02 PM
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

mellowest
03-15-2018, 02:20 AM
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.

mellowest
03-15-2018, 06:00 AM
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.