MrExcel Publishing
Your One Stop for Excel Tips & Solutions

Print to File Macro

Posted by Jamey on July 10, 2001 11:57 AM

I am trying to print to file from a command button on a worksheet. I am getting a run-time error 1004, witht the reason of: method 'Goto' of object Application failed. Here is my code:

Private Sub CommandButton1_Click()

Dim PSfilename As String
Application.Goto reference:="r2c1"
Let PSfilename = Application.ActiveCell
SendKeys PSfilename & "{ENTER}", False
ActiveSheet.Printout , PrintToFile:=True, collate:=True
PSfilename = Chr(34) & PSfilename & Chr(34)

End sub

The reference part is where the file name comes from. It is a cell on the worksheet. This code works for me in other places, doing basically the same thing, that is why I can't figure out why it isn't working. If anyone could help me out, it would be greatly appreciated. Thanks!


Posted by Ivan F Moala on July 10, 2001 12:22 PM

You are probably using Excel97.
Click on design Mode
Right clcik button
Select Properties
Select TakeFocusOnClick & set this to False.

Alternatively you can Just activate a cell
before you try and reference any adresses.