Need to Wait for 'Print' or 'Close' (VBA)

M

Milothicus

I've got code that includes importing data from solidworks and printing
multiple sheets. since things can tend to be wrong, i'd like the code
to bring up the print preview for a sheet, wait for the user to either
hit 'Close' or 'Print' and then continue to the next sheet...

any time i leave the preview open for more than about 10 seconds i get
a popup complaining that the server's busy, and i have the options of
'switch to' and 'retry.

i really want to get rid of this window, and it's only coming up
because the print preview is waiting for user input.

is there a property like 'WorksheetIsCurrentlyInPrintPreview' that i
could flag?

is there an event trigger that i could use? 'OnPrint'? or
'OnClosePrintPreview'?

any advice at all?
 
M

Milothicus

ok, think i'm getting closer already...

i found this event:

Private Sub Workbook_BeforePrint(Cancel As Boolean)

End Sub

now i need some more help. If i use a boolean in my solidworks macro,
how do I change its state from the code in this module? is it possible?
 
R

Robert Bruce

Roedd said:
I've got code that includes importing data from solidworks and
printing multiple sheets. since things can tend to be wrong, i'd like
the code to bring up the print preview for a sheet, wait for the user
to either hit 'Close' or 'Print' and then continue to the next
sheet...

any time i leave the preview open for more than about 10 seconds i get
a popup complaining that the server's busy, and i have the options of
'switch to' and 'retry.

i really want to get rid of this window, and it's only coming up
because the print preview is waiting for user input.

is there a property like 'WorksheetIsCurrentlyInPrintPreview' that i
could flag?

is there an event trigger that i could use? 'OnPrint'? or
'OnClosePrintPreview'?


It looks like you're connecting to solidworks (which I'm not familiar with)
via COM/OLE automation> In that case, you can turn off OLE messages like
this:

Private Declare Function _
CoRegisterMessageFilter Lib "OLE32.DLL" _
(ByVal lFilterIn As Long, _
ByRef lPreviousFilter) As Long

Sub KillMessageFilter()

Dim lngMsgFilter As Long

''' Remove the message filter before
''' calling the COM server.
CoRegisterMessageFilter 0&, lngMsgFilter

''' Call the COM server here....

''' Restore the message filter after
''' calling the COM server.
CoRegisterMessageFilter lngMsgFilter, _
lngMsgFilter

End Sub

--
Rob

http://www.asta51.dsl.pipex.com/webcam/

This message is copyright Robert Bruce and intended
for distribution only via NNTP.
Dissemination via third party Web forums with the
exception of Google Groups and Microsoft Communities
is strictly prohibited and may result in legal action.
 
M

Milothicus

well, the macro is initiated in Solidworks, and it's all written in VBA.
I do some work in Solidworks, save info to an array, and then use
excel's API to open a spreadsheet and place the info into excel.

I'm not a complete newbie, but i'm not sure what COM/OLE automation is.
I'm just using VBA to move between the two of them. maybe that's what i
need, but i can't make sense of what you've suggested... i don't know
which section of my code would replace 'Call the COM server here...'

The code for the macro is in an .swp file (solidworks program), and the
event i mentioned above is attached to the workbook. there must be a way
to use the event in the workbook code to set a variable to 'true' in the
solidworks code.

could i send keystrokes with one to be received by the other?
 
R

Robert Bruce

Roedd said:
well, the macro is initiated in Solidworks, and it's all written in
VBA. I do some work in Solidworks, save info to an array, and then use
excel's API to open a spreadsheet and place the info into excel.

I'm not a complete newbie, but i'm not sure what COM/OLE automation
is. I'm just using VBA to move between the two of them. maybe that's
what i need, but i can't make sense of what you've suggested... i
don't know which section of my code would replace 'Call the COM
server here...'

The code for the macro is in an .swp file (solidworks program),

OK. It looks like you're doing COM automation without knowing it. Stick it
on your CV.

Put the declaration (Private Declare Function etc.) at the top of a regular
bas module. If you are going to call it from a different module, change the
declaration from Private to Public.

Now, find where you are starting up your Excel instance (either set
[variable] = new Excel.Application or CreateObject(...). Put the line

CoRegisterMessageFilter 0&, lngMsgFilter

just before it. Now find where you are destroying your Excel instance
([variable].Quit: set Excel = nothing) and put the line

CoRegisterMessageFilter lngMsgFilter, lngMsgFilter

just after it. You will also need to declare the lngMsgFilter variable in
your routine. This will stop the message appearing when solidworks thinks
that Excel has stopped responding. You could experiment with putting these
lines just before and after where you call the printpreview if you are
worried about loosing all messages should Excel hang for another reason.
the event i mentioned above is attached to the workbook. there must
be a way to use the event in the workbook code to set a variable to
'true' in the solidworks code.

There is, but I don't think it would help, since the message you are seeing
is raised from deep under the hood.
could i send keystrokes with one to be received by the other?

Don't do that.

--
Rob

http://www.asta51.dsl.pipex.com/webcam/

This message is copyright Robert Bruce and intended
for distribution only via NNTP.
Dissemination via third party Web forums with the
exception of Google Groups and Microsoft Communities
is strictly prohibited and may result in legal action.
 
M

Milothicus

Robert said:
Roedd <<Milothicus>> wedi ysgrifennu:
OK. It looks like you're doing COM automation without knowing it. Stick
it
on your CV.

Woohoo! i have job skills!


Put the declaration (Private Declare Function etc.) at the top of a
regular
bas module. If you are going to call it from a different module, change
the
declaration from Private to Public.

Now, find where you are starting up your Excel instance (either set
[variable] = new Excel.Application or CreateObject(...). Put the line

CoRegisterMessageFilter 0&, lngMsgFilter

just before it. Now find where you are destroying your Excel instance
([variable].Quit: set Excel = nothing) and put the line

CoRegisterMessageFilter lngMsgFilter, lngMsgFilter

just after it. You will also need to declare the lngMsgFilter variable
in
your routine. This will stop the message appearing when solidworks
thinks
that Excel has stopped responding. You could experiment with putting
these
lines just before and after where you call the printpreview if you are
worried about loosing all messages should Excel hang for another
reason.

the event i mentioned above is attached to the workbook. there must
be a way to use the event in the workbook code to set a variable to
'true' in the solidworks code.

There is, but I don't think it would help, since the message you are
seeing
is raised from deep under the hood.

Fair enough.
could i send keystrokes with one to be received by the
other?
Don't do that.

I'll avoid it like the plague.

Thanks for the help. your explanation makes sense to me now. so does
your suggestion just apply a filter to messages that doesn't allow any
through?
 

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

Top