Results 1 to 5 of 5

Thread: Open excel file with VBA?

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2006
    Posts
    292

    Open excel file with VBA?

    Hi everyone?

    I'm trying to open a excel file trough autocad VBA.

    my question is: can i open a excel file the same way as VB6?, or i will have to use some API, because i can´t make a reference to excel object library in the autocad VBA.


    Thanks.
    Last edited by em07189; Feb 5th, 2008 at 07:29 PM.

  2. #2
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Open excel file with VBA?

    This may not be the best way, but I have no idea how AutoCad VBA works.

    But, if you can run an API in it, I would use ShellExecute to open your spreadsheet.

  3. #3
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Open excel file with VBA?

    Thread Moved

    Why would you need to make a reference to Excel if you just want to open the workbook? Unless you are also doing some automation then you can solve that with using Late Binding.

    ShellExecute API example:
    Code:
    Option Explicit
    
    Private Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" ( _
                        ByVal hwnd As Long, _
                        ByVal lpOperation As String, _
                        ByVal lpFile As String, _
                        ByVal lpParameters As String, _
                        ByVal lpDirectory As String, _
                        ByVal nShowCmd As Long) As Long
    
    Private Const SW_HIDE As Long = 0
    Private Const SW_SHOWNORMAL As Long = 1
    Private Const SW_SHOWMAXIMIZED As Long = 3
    Private Const SW_SHOWMINIMIZED As Long = 2
    
    Private Sub Command1_Click()
        ShellExecute Me.hwnd, "open", "C:\Users\Public\Book1.xls", vbNullString, "C:\", SW_SHOWNORMAL
    End Sub
    Late Binding:
    http://www.vbforums.com/showthread.php?t=406640
    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

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2006
    Posts
    292

    Re: Open excel file with VBA?

    Thanks a lot RobDog888 and Hack!

    But i have one more question, how can i put values into a excel sheet cells trough VBA in the opened excel document?

    The idea here is to open and write to a template excel file information from a autocad drawing.

    By the way robdog what you mean by Late Binding?

    Please if you know some thread or tutorial to manipulate excel trough VBA, please post it Thanks.

  5. #5
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Open excel file with VBA?

    see the office development faq link in robdog post above
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

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