Consulting

Results 1 to 6 of 6

Thread: VBA Copy-paste problem

  1. #1
    VBAX Newbie
    Joined
    Nov 2014
    Posts
    5
    Location

    VBA Copy-paste problem

    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
    Last edited by macropod; 01-24-2019 at 04:22 PM. Reason: Split from http://www.vbaexpress.com/forum/showthread.php?63156-VBA-Word-problem-with-Paste

  2. #2
    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
    Last edited by gmayor; 01-24-2019 at 01:16 AM.
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  3. #3
    VBAX Newbie
    Joined
    Nov 2014
    Posts
    5
    Location
    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 copiedMyImage1.bmp
    thanks for support

    Marzio

  4. #4
    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?
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  5. #5
    VBAX Newbie
    Joined
    Nov 2014
    Posts
    5
    Location
    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

  6. #6
    VBAX Newbie
    Joined
    Nov 2014
    Posts
    5
    Location
    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

Posting Permissions

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