title: VBA Error 1004 - PasteSpecial method of Range class
body: <p>Recently I started getting the error <code>1004: PasteSpecial method of Range class failed</code>. I know people have said before that it might be that it is trying to paste to the active sheet when there is none, however, as you can see, everything is based on <code>ThisWorkbook</code> so that shouldn't be the problem. It happens extra much when Excel doesn't have the focus.</p>
  
  <pre><code>'references the Microsoft Forms Object Library
  Sub SetGlobals()
      Set hwb = ThisWorkbook' home workbook
      Set mws = hwb.Worksheets("Code Numbers") ' main worksheet
      Set hws = hwb.Worksheets("Sheet3") ' home worksheet (Scratch pad)
      Set sws = hwb.Worksheets("Status") ' Status sheet
      Set aws = hwb.Worksheets("Addresses") ' Addresses sheet
  End Sub
  Sub Import()
  
      Call SetGlobals
      hws.Select
      'a bunch of code to do other stuff here.
      For Each itm In itms
          Set mitm = itm
          body = Replace(mitm.HTMLBody, "&lt;img border=""0"" src=""http://www.simplevoicecenter.com/images/svc_st_logo.jpg""&gt;", "")
          Call Buf.SetText(body)
          Call Buf.PutInClipboard
          Call hws.Cells(k, 1).Select
          Call hws.Cells(k, 1).PasteSpecial
  
          For Each shape In hws.Shapes
              shape.Delete
          Next shape
  
          'Some code to set the value of k 
          'and do a bunch of other stuff.
      Next itm
  End Sub
  </code></pre>
  
  <p><strong>Update:</strong> mitm and itm have two different types, so I did it for intellisense and who knows what else. This code takes a list of emails and pastes them into excel so that excel parses the html (which contains tables) and pastes it directly into excel. Thus the data goes directly into the sheet and I can sort it and parse it and whatever else I want.</p>
  
  <p>I guess I'm basically asking for anyone who knows another way to do this besides putting it in an html file to post it. Thanks</p>