Results 1 to 7 of 7

Thread: Print an invisible excel form

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Nov 2005
    Posts
    208

    Print an invisible excel form

    hi there, i will need so help please. Here is my problem.

    i have an excel form with specific named cells A1 (name), B1 (description), A2(deparment ID ) and B2 (purpose). I have to pass some a set of values via the vb code to the specific name cells in the excel form. Each set of values need to be in a different page so for example you have 3 pages for 3 sets of values. When i click on the Print Button on a different vb form, it should open excel, pass the values, print the form and close excel, everything in the background. This means i will not see any excel activities on my screen and it should go to the default printer. Thank you.

  2. #2
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Print an invisible excel form

    Ok.. what have you done so far, and where are you stuck?

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Nov 2005
    Posts
    208

    Re: Print an invisible excel form

    Well i don't know the best way to approach this problem. I need some guidance, please.

  4. #4
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Print an invisible excel form

    Ok, well here's some code that does pretty much what you want - you just need to set the values appropriately.

    In order to use this in VB you will need a reference ("project" ->"references") to the Excel Object Library.
    VB Code:
    1. Dim oXLApp as Excel.Application         'Declare the object variables
    2. Dim oXLBook as Excel.Workbook
    3. Dim oXLSheet as Excel.Worksheet
    4.  
    5.     'Start Excel:
    6.   Set oXLApp = New Excel.Application    'Create a new instance of Excel
    7.   Set oXLBook = oXLApp.Workbooks.Add    'Add a new workbook
    8.   Set oXLSheet = oXLBook.Worksheets(1)  'Work with the first worksheet
    9.  
    10.     'Set the values:
    11.   With oXLSheet
    12.     .Range("A1").Value = "Name"   'note that you can use variables instead of "Name" etc.
    13.  
    14.     .Range("B1").Value = "Description"
    15.     .Range("A2").Value = "Deparment ID"
    16.     .Range("B2").Value = "Purpose"
    17.   End With
    18.  
    19.     'Print the workbook
    20.   oXLBook.PrintOut
    21.  
    22.     'Close Excel, but dont save the workbook
    23.   Set oXLSheet = Nothing             'disconnect from the Worksheet
    24.   oXLBook.Close SaveChanges:= False  'Close (and disconnect from) the Workbook
    25.   Set oXLBook = Nothing
    26.   oXLApp.Quit                        'Close (and disconnect from) Excel
    27.   Set oXLApp = Nothing

    While you are testing it is probably better to not print it out every time (just display it instead to show any mistakes), so I would recommend using code below instead of the code from "Print the workbook" onwards until it is ok.
    VB Code:
    1. oXLApp.Visible = True                'Show it to the user
    2.   Set oXLSheet = Nothing               'Disconnect from all Excel objects (let the user take over)
    3.   Set oXLBook = Nothing
    4.   Set oXLApp = Nothing

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Nov 2005
    Posts
    208

    Re: Print an invisible excel form

    Thanks man, well i going to use a virtual printer for testing.

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Nov 2005
    Posts
    208

    Re: Print an invisible excel form

    how can i tell the code which exsisting excel workbook it should be using. There are different excel workbooks for different set of values. Moreover i used the reference - "Microsoft Visual Basic For Applications Extensibility 5.3", is it ok?
    Last edited by Prav; Jan 8th, 2006 at 08:12 PM.

  7. #7
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Print an invisible excel form

    To use an existing workbook, change this:
    VB Code:
    1. Set oXLBook = oXLApp.Workbooks.Add    'Add a new workbook
    to this:
    VB Code:
    1. Set oXLBook = oXLApp.Workbooks.Open("c:\path\file.xls")   'Open a workbook

    As for your reference, it isn't relevant to this - only the Excel Object Library matters.

Posting Permissions

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



Click Here to Expand Forum to Full Width