Results 1 to 10 of 10
  1. #1
    4 Star Lounger
    Join Date
    Mar 2005
    Location
    Leicestershire, United Kingdom
    Posts
    506
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Printing a Webpage automatically from code (2000)

    Hi,

    Back on the scene and trying to get back into things.

    I have a command button that generates various letters and forms for an appointment via word merge done by access. I want to be able to automate printing of a website together with this code. The website contents vary depending on the postcode field. I already have the link to the varying website stored in the particular form / table, I just want to be able to make is seamless.

    Any suggestions or ideas?
    Best Regards,

    Luke

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Printing a Webpage automatically from code (2000)

    See Print a Web Page for example code.

  3. #3
    4 Star Lounger
    Join Date
    Mar 2005
    Location
    Leicestershire, United Kingdom
    Posts
    506
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Printing a Webpage automatically from code (2000)

    Thanks,

    Looking at it now, despite it saying 'easy' level, I am left scratching my head
    Best Regards,

    Luke

  4. #4
    4 Star Lounger
    Join Date
    Mar 2005
    Location
    Leicestershire, United Kingdom
    Posts
    506
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Printing a Webpage automatically from code (2000)

    Struggling a bit.

    I have a command button on the form and the field ourproperty with the url in.

    I past the code into the onclick event of the command button like below:

    Public Function PrintWebPage(ByVal URL As String) As Boolean
    ' #VBIDEUtils#************************************** *****
    ' * Programmer: Leo Koach
    ' * Web Site : http://www.vbonly.com
    ' * E-Mail :
    ' * Date : 8/7/2000
    ' ************************************************** *******
    ' * Comments : Prints web page specified by URL
    ' * : Returns True if successful, false otherwise
    ' * : Requires IE4 or above
    ' * : Will show print dialog before printing
    ' * : Example: PrintWebPage "http://www.freevbcode.com"
    ' ************************************************** *******
    Dim sFile As String
    sFile = me!ourproperty

    'check if .dll to print page exists
    If Dir(sFile) = "" Then Exit Function
    On Error Resume Next
    Shell "rundll32.exe " & sFile & ",PrintHTML " & URL, _
    vbNormalFocus

    'return true if no errors
    PrintWebPage = Err.Number = 0
    End Function

    Private Function SystemDir() As String

    Dim sRet As String, lngRet As Long

    sRet = String$(MAX_PATH, 0)
    lngRet = GetSystemDirectory(sRet, MAX_PATH)
    SystemDir = Left(sRet, lngRet)

    End Function

    Doesn't seem to work
    Best Regards,

    Luke

  5. #5
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Printing a Webpage automatically from code (2000)

    Copy/paste the first part at the top of a module (below lines starting with Option, but above all procedures and functions):

    Private Const MAX_PATH = 255

    Private Declare Function GetSystemDirectory Lib "kernel32" _
    Alias "GetSystemDirectoryA" (ByVal lpBuffer As String, _
    ByVal nSize As Long) As Long

    Copy/paste the second part somewhere below the declarations in the same module. You can then use the code like this: assume that you have a field or control named txtWebAddrress containing the URL of the web page.

    If PrintWebPage(Me.txtWebAddress) = False Then
    MsgBox "Print failed", vbExclamation
    End If

  6. #6
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Printing a Webpage automatically from code (2000)

    You cannot insert a series of functions inside another sub. See my other reply.

  7. #7
    4 Star Lounger
    Join Date
    Mar 2005
    Location
    Leicestershire, United Kingdom
    Posts
    506
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Printing a Webpage automatically from code (2000)

    So,

    Let me get it right.

    I put the following code at the top of the module directly below 'Option compare'


    Private Const MAX_PATH = 255

    Private Declare Function GetSystemDirectory Lib "kernel32" _
    Alias "GetSystemDirectoryA" (ByVal lpBuffer As String, _
    ByVal nSize As Long) As Long


    Then in the onclick event of a command button I have :

    Dim sFile As String
    sFile = SystemDir & "MSHTML.DLL"

    'check if .dll to print page exists
    If Dir(sFile) = "" Then Exit Function
    On Error Resume Next
    Shell "rundll32.exe " & sFile & ",PrintHTML " & URL, _
    vbNormalFocus

    'return true if no errors
    PrintWebPage = Err.Number = 0
    End Function

    Private Function SystemDir() As String

    Dim sRet As String, lngRet As Long

    sRet = String$(MAX_PATH, 0)
    lngRet = GetSystemDirectory(sRet, MAX_PATH)
    SystemDir = Left(sRet, lngRet)

    End Function

    Luke
    Best Regards,

    Luke

  8. #8
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Printing a Webpage automatically from code (2000)

    No! I repeat, you cannot insert functions inside other functions or subs. You must paste the code from the website I referred to OUTSIDE any other function or sub. You can then call the PrintWebPage function in the On Click event procedure of your button:

    Private Sub cmdPrintWebPage_Click()
    If PrintWebPage(Me.txtWebAddress) = False Then
    MsgBox "Print failed", vbExclamation
    End If
    End Sub

    Here, cmdPrintWebPage is the name of the command button, and txtWebAddress is the name of the text box or field containing the URL of the web page you want to print.

  9. #9
    2 Star Lounger
    Join Date
    Mar 2007
    Location
    Wikltshire UK
    Posts
    152
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Re: Printing a Webpage automatically from code (2000)

    Hi. Does anyone know if theres a problem with IE7 (XP SP2) with this PrintWebPage Function.

    I've got everything in the right place Above and Below the Public Function and I'm calling the PrintWebPage Function with :

    Variable = PrintWebPage ("http://www.google.com") for testing perposes

    Using "Option Explicit" in a Access 2007 Module (Any other "references" in the Module needed apart from "Microsoft XMl v6.0")

    Must be something Simple but is still eluding me.

  10. #10
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Printing a Webpage automatically from code (2000)

    Apparently this method requires the URL to be enclosed in quotes. Change the lines
    <code>
    Shell "rundll32.exe " & sFile & ",PrintHTML " & URL, _
    vbNormalFocus
    </code>
    to
    <code>
    Shell "rundll32.exe " & sFile & ",PrintHTML " & Chr(34) & URL & Chr(34), _
    vbNormalFocus</code>

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •