Pasting not working in Excel VBA

Shaun Allan

New Member
Joined
May 18, 2016
Messages
18
I have a workbook which creates Word reports based on a Word template and tables in the workbook.

Depending on the equipment type, it copies a range from the spreadsheet and pastes it to two bookmark locations in the word document (bmInternal and bmExternal). I tried using PasteAppendTable, but this only works once. If I try to use it twice, for each bookmark, it copies nothing both times. As such I used Paste for one and PasteAppendTable for the second (PasteAppendTable is much neater as the formatting is better).

This worked fine, but I made changes to the code, not related to this, and now the Paste (which goes to bmInternal) isn't working. I can't see why when I've not changed anything regarding that part:

Code:
Sub Data2Word()
    
    Application.GoTo Reference:=ActiveSheet.Range("A2")
    
GoAgain:
On Error Resume Next
    Dim vItem As String
    'Dim vImagePath As String
    
    Dim vCurrentRow As Integer
    
    Dim vDesc As String
    Dim vN2 As String
    Dim vGuide As String
    Dim vUnit As String
    Dim vBlock As String
            
    Dim wrdPic As Word.InlineShape
    Dim rng As Excel.Range                    'our source range
    Dim rngText As Variant
    Dim rngText2 As Variant
    
    Dim wdApp As New Word.Application   'a new instance of Word
    Dim wdDoc As Word.Document          'our new Word template
    Dim myWordFile As String            'path to Word template
    Dim wsExcel As Worksheet
    Dim tmpAut
    
'Find Item and type
    vItem = ActiveCell.Value
    vDesc = ActiveCell.Offset(0, 2)
    vN2 = ActiveCell.Offset(0, 1)
    vGuide = ActiveCell.Offset(0, 3)
    vBlock = ActiveCell.Offset(0, 4)
    vUnit = Left(vItem, 3)
    
    If ActiveSheet.Range("rngREPORTED") = "Yes" Then
        MsgBox vItem & " already has a report."
        Exit Sub
    End If
    'initialize the Word template path
    'here, it's set to be in the same directory as our source workbook
    myWordFile = "W:\Entity\Inspect\WORD\INSPECTION TEMPLATES\Inspection Template - 20160511.dotx"
    
    'open a new word document from the template
    Set wdDoc = wdApp.Documents.Add(myWordFile)
    
    If vGuide = "IGE01" Then
    
        rngText = "rngEXCH"
        rngText2 = "rngEXCHE"
        
    ElseIf ActiveCell.Offset(, 4) = "Mono" Then
    
        'Do Mono
        rngText = "rngMONO"
    
    Else
            
            ActiveWorkbook.Names.Add Name:="rngItemSub", RefersTo:=Worksheets("SubEquipment").Range("B" & ActiveCell.Offset(0, 6) & ":C" & ActiveCell.Offset(0, 7) + ActiveCell.Offset(0, 6))

CarryOn:
            rngText = "rngItemSub"
            
    End If
    
    'Insert Tables
    'get the range of the data
     
    Set rng = Range(rngText)
    rng.Copy                            'copy the range
    
    wdDoc.Bookmarks("bmInternal").Range.Paste 'AppendTable
    
    If vGuide = "IGE01" Then
        Set rng = Range(rngText2)
        rng.Copy
    End If
    
    wdDoc.Bookmarks("bmExternal").Range.PasteAppendTable
    
    wdDoc.Bookmarks("bmItem").Range.InsertAfter vItem
    wdDoc.Bookmarks("bmDesc").Range.InsertAfter vDesc
    wdDoc.Bookmarks("bmN2").Range.InsertAfter vN2
    wdDoc.Bookmarks("bmGuide").Range.InsertAfter vGuide
    wdDoc.Bookmarks("bmBlock").Range.InsertAfter vBlock
    
    wdDoc.Variables("wvItem").Value = vItem
    ActiveDocument.Fields.Update

    With wdDoc
            Set wrdPic = .Bookmarks("bmImage").Range.InlineShapes.AddOLEObject(ClassType:="AcroExch.Document.7", Filename:="W:\Entity\Inspect\T&I\2016\Various Items\Photos\Sorted\" & vItem & ".pdf", LinkToFile:=False, DisplayAsIcon:=False)
            wrdPic.ScaleHeight = 55
            wrdPic.ScaleWidth = 55
    End With
    
    wdApp.Visible = True
    
    wdApp.Activate
    
    wdDoc.SaveAs "W:\Entity\Inspect\WSDATA\REPORTS\2016\" & vUnit & "\" & vItem & " " & vN2 & " THO.docx" 'Mid(ActiveDocument.Name, 1, Len(ActiveDocument.Name) - 4)

MoveHere:
    
    ActiveWorkbook.Sheets("AllItems").Range("G" & ActiveCell.Offset(0, 8)).Value = "Yes"
    ActiveWorkbook.Save

End Sub
 

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
Can't you repeat the copy before pasting again?
 
Upvote 0
I've tried that for both parts, but it doesn't seem to work. Currently, the bmExternal (PasteAppendTable) works but the bmInternal (Paste) isn't working. I can't see why, though. It WAS working. Using PasteAppendTable both times would work better, but then neither pastes anything.
 
Upvote 0
If you remove On Error Resume Next then run the code do you get any errors?
 
Upvote 0
No option to Debug?

Any error message?
 
Upvote 0
I've had a quick search on that error number and the usual message that goes with it is the somewhat vague 'Command failed'.

Making things even vaguer is no specific line being highlighted by the debugger.:)

I've looked through the code a couple of times and the only thing, to me anyway, that stands out is this.
Code:
    ActiveDocument.Fields.Update

If this code is being run from Excel then ActiveDocument doesn't really mean anything within the Excel VBA environment, and that could cause problems.

Try replacing ActiveDocument with wdDoc, which you've been using throughout the rest of the code.
 
Upvote 0
Thanks Norie. Actually, that command does work (in updating the fields within the document) but I'll give it a go. I appreciate your time.
 
Upvote 0

Forum statistics

Threads
1,214,834
Messages
6,121,871
Members
449,055
Latest member
excelhelp12345

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top