Search code examples
excelvisual-studiovbaexcel-template

How to open and edit an excel template using visual basic


I have a template that we use at work that I want to edit using visual basic. I have a gui built that asks the user for a few pieces of data. I need to open the template and just edit the contents of the template based on the user input. I have done research so far and everything I've seen shows me how to open a new excel document, not a current one. How do I open the current template?

*I get the user to browse for and select the filename and have that stored as a variable


Solution

  • Hard to say what you want to achieve maybe more clarification is needed.

    To reference an Opened Excel file

    Set execlObj = GetObject(fileName)
    

    To open an Unopened Excel file (in separate Excel application)

    Dim oXLApp As Object, wb As Object
    Set oXLApp = CreateObject("Excel.Application")
    Set wb = oXLApp.Workbooks.Open(fileName)
    

    Where file name is e.g. c:\test.xls