How to print a web page with VBA?

D

Dudely

With VBA, I do something like the following:

Set obIe = CreateObject("InternetExplorer.Application")
obIe.navigate (URLaddress)

The page loads, and now I want to print the page.

I've tried:
obIe.print
me.print
printer.print
obIe.document.print

and some others. Nothing I've tried works. Does anyone know the
correct way to do this?

Thank you in advance.
 

My Computer

M

mayayana

I haven't tested this, but I think it should work:

obIe.ExecWB 6, 0

You should also make sure the page is fully loaded
before calling it:

Do Until obIe.ReadyState = 4
Loop

> With VBA, I do something like the following:
>
> Set obIe = CreateObject("InternetExplorer.Application")
> obIe.navigate (URLaddress)
>
> The page loads, and now I want to print the page.
>
> I've tried:
> obIe.print
> me.print
> printer.print
> obIe.document.print
>
> and some others. Nothing I've tried works. Does anyone know the
> correct way to do this?
>
> Thank you in advance.
 

My Computer

B

Bishop

Hi,

here is a VBScript (tested) that print a Web page.

I think it will also work in VBA.

Link:
http://www.scriptbox.at.tt/index.php?search=Open and print a Web Page.vbs&list=1

alt. Link:
http://scriptbox.boris-toll.at/index.php?search=Open and print a Web Page.vbs&list=1


"Dudely" wrote:

> With VBA, I do something like the following:
>
> Set obIe = CreateObject("InternetExplorer.Application")
> obIe.navigate (URLaddress)
>
> The page loads, and now I want to print the page.
>
> I've tried:
> obIe.print
> me.print
> printer.print
> obIe.document.print
>
> and some others. Nothing I've tried works. Does anyone know the
> correct way to do this?
>
> Thank you in advance.
>
 

My Computer

M

mayayana


>
> here is a VBScript (tested) that print a Web page.
>
> I think it will also work in VBA.
>

Your link seems to go around in circles, and it's basically
the same code I already posted.


The code, so people don't have to track it down:

On Error Resume Next

Const OLECMDID_PRINT = 6
Const OLECMDEXECOPT_DONTPROMPTUSER = 2
Const PRINT_WAITFORCOMPLETION = 2

Dim oIExplorer : Set oIExplorer =
CreateObject("InternetExplorer.Application")
oIExplorer.Navigate "http://www.scriptbox.at.tt/"
oIExplorer.Visible = 1

Do while oIExplorer.ReadyState <> 4
wscript.sleep 1000
Loop

oIExplorer.ExecWB OLECMDID_PRINT, OLECMDEXECOPT_DONTPROMPTUSER

--------------

Or for anyone who wants to get the actual file, the
real download link is here:

http://www.boris-toll.at/scriptbox/download.php?file=Open and print a%
20Web%20Page.vbs&filename=Open%20and%20print%20a%20Web%20Page.vbs
 

My Computer

M

mayayana

This doccumentation of ExecWB might be useful to people.
The parameter options are not easy to find.


ExecWB Method

object.ExecWB nCmdID, nCmdExecOpt, [pvaIn], [pvaOut]


object Required. An object expression that evaluates to an object in the
Applies To list.
nCmdID Required. A long integer that represents the identifier of the
command to execute.
nCmdExecOpt Required. A long integer that represents the options set for
executing the command.
pvaIn Optional. A variant used for specifying command input arguments.
pvaOut Optional. A variant used for specifying command output arguments


______________
first parameter - nCmdID options:
______________


OLECMDID_OPEN = 1,
OLECMDID_NEW = 2,
OLECMDID_SAVE = 3,
OLECMDID_SAVEAS = 4,
OLECMDID_SAVECOPYAS = 5,
OLECMDID_PRINT = 6,
OLECMDID_PRINTPREVIEW = 7,
OLECMDID_PAGESETUP = 8,
OLECMDID_SPELL = 9,
OLECMDID_PROPERTIES = 10,
OLECMDID_CUT = 11,
OLECMDID_COPY = 12,
OLECMDID_PASTE = 13,
OLECMDID_PASTESPECIAL = 14,
OLECMDID_UNDO = 15,
OLECMDID_REDO = 16,
OLECMDID_SELECTALL = 17,
OLECMDID_CLEARSELECTION = 18,
OLECMDID_ZOOM = 19,
OLECMDID_GETZOOMRANGE = 20
OLECMDID_UPDATECOMMANDS = 21
OLECMDID_REFRESH = 22
OLECMDID_STOP = 23
OLECMDID_HIDETOOLBARS = 24
OLECMDID_SETPROGRESSMAX = 25
OLECMDID_SETPROGRESSPOS = 26
OLECMDID_SETPROGRESSTEXT = 27
OLECMDID_SETTITLE = 28
OLECMDID_SETDOWNLOADSTATE = 29
OLECMDID_STOPDOWNLOAD = 30
} OLECMDID;

Elements
OLECMDID_OPEN
File menu, Open command
OLECMDID_NEW
File menu, New command
OLECMDID_SAVE
File menu, Save command
OLECMDID_SAVEAS
File menu, Save As command
OLECMDID_SAVECOPYAS
File menu, Save Copy As command
OLECMDID_PRINT
File menu, Print command
OLECMDID_PRINTPREVIEW
File menu, Print Preview command
OLECMDID_PAGESETUP
File menu, Page Setup command
OLECMDID_SPELL
Tools menu, Spelling command
OLECMDID_PROPERTIES
File menu, Properties command
OLECMDID_CUT
Edit menu, Cut command
OLECMDID_COPY
Edit menu, Copy command
OLECMDID_PASTE
Edit menu, Paste command
OLECMDID_PASTESPECIAL
Edit menu, Paste Special command
OLECMDID_UNDO
Edit menu, Undo command
OLECMDID_REDO
Edit menu, Redo command
OLECMDID_SELECTALL
Edit menu, Select All command
OLECMDID_CLEARSELECTION
Edit menu, Clear command
OLECMDID_ZOOM
View menu, Zoom command (see below for details)
OLECMDID_GETZOOMRANGE
Retrieves zoom range applicable to View Zoom. (See below for details.)
OLECMDID_UPDATECOMMANDS
Informs the receiver, usually a frame, of state changes. The receiver can
then query the status of the commands whenever convenient.
OLECMDID_REFRESH
Asks the receiver to refresh its display. Implemented by the
document/object.
OLECMDID_STOP
Stops all current processing. Implemented by the document/object.
OLECMDID_HIDETOOLBARS
View Menu, Toolbars command. Implemented by the document/object to hide its
toolbars.
OLECMDID_SETPROGRESSMAX
Sets the maximum value of a progress indicator if one is owned by the
receiving object, usually a frame. The minimum value is always zero.
OLECMDID_SETPROGRESSPOS
Sets the current value of a progress indicator if one is owned by the
receiving object, usually a frame.
OLECMDID_SETPROGRESSTEXT
Sets the text contained in a progress indicator if one is owned by the
receiving object, usually a frame. If the receiver currently has no progress
indicator, this text should be displayed in the status bar (if one exists)
as with IOleInPlaceFrame::SetStatusText.
OLECMDID_SETTITLE
Sets the title bar text of the receiving object, usually a frame.
OLECMDID_SETDOWNLOADSTATE
Called by the object when downloading state changes. Takes a VT_BOOL
parameter, which is TRUE if the object is downloading data and FALSE if it
not. Primarily implemented by the frame.
OLECMDID_STOPDOWNLOAD
Stops the download when executed. Typically, this command is propagated to
all contained objects. When queried, sets MSOCMDF_ENABLED. Implemented by
the document/object.

____________________
second parameter - nCmdExecOpt options:
____________________

OLECMDEXECOPT_DODEFAULT = 0,
OLECMDEXECOPT_PROMPTUSER = 1,
LECMDEXECOPT_DONTPROMPTUSER = 2,
OLECMDEXECOPT_SHOWHELP = 3

OLECMDEXECOPT_DODEFAULT
Prompt the user for input or not, whichever is the default behavior.
OLECMDEXECOPT_PROMPTUSER
Execute the command after obtaining user input.
OLECMDEXECOPT_DONTPROMPTUSER
Execute the command without prompting the user. For example, clicking the
Print toolbar button causes a document to be immediately printed without
user input.
OLECMDEXECOPT_SHOWHELP
Show help for the corresponding command, but do not execute
 

My Computer

D

Dudely

Oh wow. Is that really the VBA way to do it? I've never heard of
ExecWB (which means nothing, but) how does one know where to find or
look for something like that in the first place?

Thank you very very much! I'll give it a try.

On Oct 10, 7:19 am, "mayayana" <[email protected]> wrote:

> This doccumentation of ExecWB might be useful to people.
> The parameter options are not easy to find.
>
> ExecWB Method
>
> object.ExecWB nCmdID, nCmdExecOpt, [pvaIn], [pvaOut]
>
> object Required. An object expression that evaluates to an object in the
> Applies To list.
> nCmdID Required. A long integer that represents the identifier of the
> command to execute.
> nCmdExecOpt Required. A long integer that represents the options set for
> executing the command.
> pvaIn Optional. A variant used for specifying command input arguments.
> pvaOut Optional. A variant used for specifying command output arguments
>
> ______________
> first parameter - nCmdID options:
> ______________
>
>      OLECMDID_OPEN              = 1,
>      OLECMDID_NEW               = 2,
>      OLECMDID_SAVE              = 3,
>      OLECMDID_SAVEAS            = 4,
>      OLECMDID_SAVECOPYAS        = 5,
>      OLECMDID_PRINT             = 6,
>      OLECMDID_PRINTPREVIEW      = 7,
>      OLECMDID_PAGESETUP         = 8,
>      OLECMDID_SPELL             = 9,
>      OLECMDID_PROPERTIES        = 10,
>      OLECMDID_CUT               = 11,
>      OLECMDID_COPY              = 12,
>      OLECMDID_PASTE             = 13,
>      OLECMDID_PASTESPECIAL      = 14,
>      OLECMDID_UNDO              = 15,
>      OLECMDID_REDO              = 16,
>      OLECMDID_SELECTALL         = 17,
>      OLECMDID_CLEARSELECTION    = 18,
>      OLECMDID_ZOOM              = 19,
>      OLECMDID_GETZOOMRANGE      = 20
>      OLECMDID_UPDATECOMMANDS    = 21
>      OLECMDID_REFRESH           = 22
>      OLECMDID_STOP              = 23
>      OLECMDID_HIDETOOLBARS      = 24
>      OLECMDID_SETPROGRESSMAX    = 25
>      OLECMDID_SETPROGRESSPOS    = 26
>      OLECMDID_SETPROGRESSTEXT   = 27
>      OLECMDID_SETTITLE          = 28
>      OLECMDID_SETDOWNLOADSTATE  = 29
>      OLECMDID_STOPDOWNLOAD      = 30
>
> } OLECMDID;
>
> Elements
> OLECMDID_OPEN
> File menu, Open command
> OLECMDID_NEW
> File menu, New command
> OLECMDID_SAVE
> File menu, Save command
> OLECMDID_SAVEAS
> File menu, Save As command
> OLECMDID_SAVECOPYAS
> File menu, Save Copy As command
> OLECMDID_PRINT
> File menu, Print command
> OLECMDID_PRINTPREVIEW
> File menu, Print Preview command
> OLECMDID_PAGESETUP
> File menu, Page Setup command
> OLECMDID_SPELL
> Tools menu, Spelling command
> OLECMDID_PROPERTIES
> File menu, Properties command
> OLECMDID_CUT
> Edit menu, Cut command
> OLECMDID_COPY
> Edit menu, Copy command
> OLECMDID_PASTE
> Edit menu, Paste command
> OLECMDID_PASTESPECIAL
> Edit menu, Paste Special command
> OLECMDID_UNDO
> Edit menu, Undo command
> OLECMDID_REDO
> Edit menu, Redo command
> OLECMDID_SELECTALL
> Edit menu, Select All command
> OLECMDID_CLEARSELECTION
> Edit menu, Clear command
> OLECMDID_ZOOM
> View menu, Zoom command (see below for details)
> OLECMDID_GETZOOMRANGE
> Retrieves zoom range applicable to View Zoom. (See below for details.)
> OLECMDID_UPDATECOMMANDS
> Informs the receiver, usually a frame, of state changes. The receiver can
> then query the status of the commands whenever convenient.
> OLECMDID_REFRESH
> Asks the receiver to refresh its display. Implemented by the
> document/object.
> OLECMDID_STOP
> Stops all current processing. Implemented by the document/object.
> OLECMDID_HIDETOOLBARS
> View Menu, Toolbars command. Implemented by the document/object to hide its
> toolbars.
> OLECMDID_SETPROGRESSMAX
> Sets the maximum value of a progress indicator if one is owned by the
> receiving object, usually a frame. The minimum value is always zero.
> OLECMDID_SETPROGRESSPOS
> Sets the current value of a progress indicator if one is owned by the
> receiving object, usually a frame.
> OLECMDID_SETPROGRESSTEXT
> Sets the text contained in a progress indicator if one is owned by the
> receiving object, usually a frame. If the receiver currently has no progress
> indicator, this text should be displayed in the status bar (if one exists)
> as with IOleInPlaceFrame::SetStatusText.
> OLECMDID_SETTITLE
> Sets the title bar text of the receiving object, usually a frame.
> OLECMDID_SETDOWNLOADSTATE
> Called by the object when downloading state changes. Takes a VT_BOOL
> parameter, which is TRUE if the object is downloading data and FALSE if it
> not. Primarily implemented by the frame.
> OLECMDID_STOPDOWNLOAD
> Stops the download when executed. Typically, this command is propagated to
> all contained objects. When queried, sets MSOCMDF_ENABLED. Implemented by
> the document/object.
>
> ____________________
> second parameter -  nCmdExecOpt options:
> ____________________
>
>     OLECMDEXECOPT_DODEFAULT        = 0,
>     OLECMDEXECOPT_PROMPTUSER       = 1,
>     LECMDEXECOPT_DONTPROMPTUSER    = 2,
>     OLECMDEXECOPT_SHOWHELP         = 3
>
> OLECMDEXECOPT_DODEFAULT
> Prompt the user for input or not, whichever is the default behavior.
> OLECMDEXECOPT_PROMPTUSER
> Execute the command after obtaining user input.
> OLECMDEXECOPT_DONTPROMPTUSER
> Execute the command without prompting the user. For example, clicking the
> Print toolbar button causes a document to be immediately printed without
> user input.
> OLECMDEXECOPT_SHOWHELP
> Show help for the corresponding command, but do not execute
 

My Computer

D

Dudely

On Oct 9, 9:55 pm, "mayayana" <[email protected]> wrote:

> I haven't tested this, but I think it should work:
>
> obIe.ExecWB 6, 0
>
>    You should also make sure the page is fully loaded
> before calling it:
>
>  Do Until obIe.ReadyState = 4
>  Loop
>
>
>

> > With VBA, I do something like the following:
>

> > Set obIe = CreateObject("InternetExplorer.Application")
> > obIe.navigate (URLaddress)
>

> > The page loads, and now I want to print the page.
>

> > I've tried:
> > obIe.print
> > me.print
> > printer.print
> > obIe.document.print
>

> > and some others.  Nothing I've tried works.  Does anyone know the
> > correct way to do this?
>

> > Thank you in advance.- Hide quoted text -
>
> - Show quoted text -

For whatever reason the loop code doesn't seem to work, instead I use
a sleep 5000. It may have to do with the fact that I'm using the
Click and Onclick methods (when I posted, I simplified so as not to
confuse the real issue of needing to print), I really don't know and
don't care about that at the moment, I do know the page is fully
loaded, because I'm stepping through it and I don't move on until the
page is done.

The real issue: Your code works! Well, partially. It prints the
first page, but not the second. There are only 2 pages. The second
page is all images. For my purposes it's not absolutely critical that
I get the second page, however it would be nice. Any ideas why it
stops after the first page? In fact, I now notice that on the printed
page it says "Page 1 of 1", so it doesn't even recognize that there's
more than one page. Or perhaps 1 very long page?

Thank you again
 

My Computer

M

mayayana

>
Oh wow. Is that really the VBA way to do it? I've never heard of
ExecWB (which means nothing, but) how does one know where to find or
look for something like that in the first place?

ExecWB is a method of both IE and the WebBrowser
control used in VB (which is also really just IE without
the window frame "chrome").

It's not so easy to find out this stuff. That's why I
posted the detailed documentation. IE has a fairly extensive
object model. And the IE DOM for webpage scripting is
far bigger. So it's hard to research it without having a
local copy of the MSDN documentation. (The online
MSDN system is a mess, full of dead links and bloat.
When looking for one particular item I always start with
Google. When you need to find a lot of info., like
documentation of all IE object properties and methods,
the spread out disarray of MSDN online makes that
virtually impossible.)
 

My Computer

M

mayayana

For whatever reason the loop code doesn't seem to work, instead I use
a sleep 5000.
It should work if it's called after Navigate.
I usually use it with WScript.Sleep 100 in
the loop, but it should work either way.
The real issue: Your code works! Well, partially. It prints the
first page, but not the second. There are only 2 pages. The second
page is all images.

The second page? There's only one page that
one navigates to. If there's a page 2, as with many
news articles that run multiple pages, that's just
a link on the first page. You'd have to navigate to
that separately and load it as a second document.
 

My Computer

Top