Results 1 to 3 of 3

Thread: unsure what to Set object to

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    May 2003
    Location
    USA, East Coast
    Posts
    257

    unsure what to Set object to

    Hello,

    I'm having a problem with this code when viewed online. Here's the code:

    VB Code:
    1. Private Sub UserForm_Initialize()
    2.  
    3. Dim OldState As Integer
    4. OldState = Application.WindowState
    5. Application.WindowState = xlMaximized
    6. Me.Top = Application.Top
    7. Me.Left = Application.Left
    8. Me.Height = Application.Height
    9. Me.Width = Application.Width
    10. Application.WindowState = OldState
    11.  
    12. End Sub
    Evidently I must explicitly define "Application". So when I add:

    Dim xlApp as Excel.Application

    I'm unsure of what to set it to:
    Set xlApp = ????????

    I tried adding "Excel.Application" to each line:
    OldState = Excel.Application.WindowState
    Excel.Application.WindowState = xlMaximized
    etc....
    but I'm still getting error 1004, object not SET. What do I set the object to? This is straight Excel/VBA.

    Thank you,
    CJ

  2. #2
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709
    What about...
    VB Code:
    1. Private Sub UserForm_Initialize()
    2.  
    3.     Dim xlApp as Excel.Application
    4.     Dim OldState As Integer
    5.  
    6.     Set xlApp = New Excel.Application
    7.     xlApp.WindowState = xlMaximized
    8.     Me.Top = xlApp.Top
    9.     Me.Left = xlApp.Left
    10.     Me.Height = xlApp.Height
    11.     Me.Width = xlApp.Width
    12.     xlApp.WindowState = OldState
    13.  
    14. End Sub
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    May 2003
    Location
    USA, East Coast
    Posts
    257
    Cool Robdog,

    You're amazing! This worked - using New solved it. I only added:

    xlApp.Quit
    Set xlApp = Nothing

    Thanks,
    CJ

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