Creating an event procedure question



I am using the example on Chip Pearson's web page for creating an even procedure in
another workbook for formatting printed output from that workbook.
I have modified his code as follows:

Sub CopytoThisWorkbook()
Dim StartLine As Long
With ActiveWorkbook.VBProject.VBComponents("ThisWorkbook").CodeModule
StartLine = .CreateEventProc("BeforePrint", "Workbook") + 1
.InsertLines StartLine, _
" dim NumRows as integer" & vbCr & _
" NumRows = Activesheet.usedrange.rows.count" & vbCr & _
" With Activesheet.pagesetup" & vbCr & _
" .PrintTitleRows = ""$2:$3""" & vbCr & _
" .PrintArea = ""$A$4:$K$"" & NumRows" & vbCr & _
" .PrintHeadings = False" & vbCr & _
" .PrintGridlines = False" & vbCr & _
" .PrintComments = xlPrintNoComments" & vbCr & _
" End With"
End With
End Sub

It works as expected and creates the Workbook_BeforePrint procedure.
However whenever the code runs it opens a VBA window and shows the
newly created code. No errors, just gives focus to the VBA window.
This isn't a problem for me while I'm programming/debugging, but the
end user shouldn't see this.

Any ideas why this is happening and how to keep it from happening?

Thanks for any help/suggestions.

Norman Jones

Hi Bhofsetz,

Try inserting:

Application.VBE.MainWindow.Visible = False

before End Sub.

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question