PDA

View Full Version : VBA Copy-paste problem



Marzio
01-22-2019, 11:51 PM
I have the same problem


Selection.Find.ClearFormatting
With Selection.Find
.Text = "[#$]"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute
Selection.HomeKey Unit:=wdLine
Selection.TypeParagraph
Selection.MoveUp Unit:=wdLine, Count:=1
' Selection.MoveDown Unit:=wdLine, Count:=1
Selection.Paste


The error appens at the line Selection.Paste, after Debug if i continue the ends regularly

the pasted object is a row in a table

Thanks for your help
Marzio

gmayor
01-23-2019, 10:36 PM
Having tested your code it appears to find the first occurrence of the [#$] text in a cell, and copies that text to a new paragraph at the start of the cell. I would need to see an example of the table before establishing what the problem you are having is.

If you simply want to insert a new row above the row with the found text then


Sub Macro1()
Dim oTable As Table
Dim oRng As Range
Dim oRow As Row, oNewRow As Row
Dim oCell As Range, oNewcell As Range
Dim i As Integer
Const strFind As String = "[#$]"
Set oTable = Selection.Tables(1)
Set oRng = oTable.Range
With oRng.Find
Do While .Execute(FindText:=strFind)
Set oRow = oRng.Rows(1)
Set oNewRow = oTable.Rows.Add(oRow)
'if you want to include the content of the cells in the new row then
For i = 1 To oRow.Cells.Count
Set oCell = oRow.Cells(i).Range
oCell.End = oCell.End - 1
Set oNewcell = oNewRow.Cells(i).Range
oNewcell.End = oNewcell.End - 1
oNewcell.FormattedText = oCell.FormattedText
Next i
'end of cell content
Exit Do
Loop
End With
lbl_Exit:
Set oTable = Nothing
Set oRow = Nothing
Set oNewRow = Nothing
Set oCell = Nothing
Set oNewcell = Nothing
Set oRng = Nothing
Exit Sub
End Sub

Marzio
01-24-2019, 10:27 AM
Hi Graham,

I have a row table with 5 column and i the line above that row the text [#$] as a tag, I copy the line with its contents, then I would like to add under a new line and paste what I have copied23623
thanks for support

Marzio

gmayor
01-26-2019, 12:20 AM
Looking at your image, the tag does not appear to be in the table and it looks as though there could another table below the tag. You have not posted enough of the document to judge.

From your description it is difficult to understand what you want to copy (presumably the last row of that table above the tag) or where you want that row to be placed with respect to the tag. Do you simply want to add a row to the table above the tag and fill it with the content of the previous row?

Marzio
01-26-2019, 02:45 AM
Hi
I have a word template with a table with 1 row and 5 columns in the cells I have tags that I want to replace with the data read in a text file. The macro as first operation copies the table row and its tags (puts it in the clipboard) then starts reading the text file and replaces the tags of the first row, finished the first row, I move below and add (paste) the row copied to previously. The off-table tag was just a test Selection.Paste command and Selection.PasteAppendTable command both return (Command not available).

Regards
Marzio

Marzio
01-26-2019, 05:39 AM
Hi
this macro return the error


Sub Macro4()'
' Macro4 Macro
'
'
CommandBars("Navigation").Visible = False
Selection.Find.ClearFormatting
With Selection.Find
.Text = "[#Des2]"
.Replacement.Text = "1998-11-27"
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute
Selection.EndKey Unit:=wdLine, Extend:=wdExtend
Selection.EndKey Unit:=wdLine, Extend:=wdExtend
Selection.EndKey Unit:=wdLine, Extend:=wdExtend
Selection.Copy
Selection.MoveDown Unit:=wdLine, Count:=1
Selection.PasteAppendTable
End Sub