Domanda

I am trying to write a code that exports data to excel after user prompted actions. Basically, I have been able to export to Excel successfully, but the 2nd instance I want to write to a new tab instead of a new Excel application.

oExcel = CreateObject("Excel.Application")
oExcel.Visible = True
oBook = oExcel.Workbooks.Add
oSheet = oBook.Worksheets(3)
oSheet.Delete()
oSheet = oBook.Worksheets(2)
oSheet.Delete()
oSheet = oBook.Worksheets(1)
oSheet.Name = "Run " & Counter

At this point, the user will press a button, making Excel no longer active. So when I want to write more data to a new sheet, the Object commands do not 'work unless I repeat code exactly.

I tried:

Counter +=1

'For the first instance
If Counter = 1 Then

        oExcel = CreateObject("Excel.Application")
        oExcel.Visible = True
        oBook = oExcel.Workbooks.Add
        oSheet = oBook.Worksheets(3)
        oSheet.Delete()
        oSheet = oBook.Worksheets(2)
        oSheet.Delete()
        oSheet = oBook.Worksheets(1)
        oSheet.Name = "Run " & Counter

Else
'For every instance after that the user wants to do another run

        oExcel.ActivateObject(Excel.Application)
        oBook = oExcel.Workbooks(1)
        oSheet = oBook.Worksheets.Add
        oSheet.Name = "Run " & Counter

End If

I have been looking for days and am getting very frustrated. I do not know how to reference back to the open excel in order to continue to writing data ... after the user has pressed a button on the VB form confirming they want to do another run.

È stato utile?

Soluzione

To get a reference to an already-running instance of excel you can use GetObject.

Eg:

' Test to see if a copy of Excel is already running. 
Private Sub testExcelRunning()
    On Error Resume Next 
    ' GetObject called without the first argument returns a 
    ' reference to an instance of the application. If the 
    ' application is not already running, an error occurs. 
    Dim excelObj As Object = GetObject(, "Excel.Application")
    If Err.Number = 0 Then
        MsgBox("Excel is running")
    Else
        MsgBox("Excel is not running")
    End If
    Err.Clear()
    excelObj = Nothing 
End Sub

http://msdn.microsoft.com/en-us/library/e9waz863(v=vs.90).aspx

If Excel is not already running you can start a new instance using CreateObject.

Altri suggerimenti

I used to write VBA, but I was taught to get out of the habit of using CreateObject. you could also use a boolean just as well, but i imagine thats just preference. You should create the excel object outside the loop, hold reference at class level once its assigned. you then use the loop to solely assign the next sheets and add values. Keeping the dimension at class level means you do not need to get rid of the object immediately, because there is possibility the user might still need to use the reference.

Public Class Form1
Dim firstRun As Boolean = True
Dim xlApp As New Excel.Application
Dim xlWb As Excel.Workbook
Dim xlSheet As Excel.Worksheet
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    'set up us some excel
    xlApp.Visible = True
    xlApp.SheetsInNewWorkbook = 1
    xlWb = xlApp.Workbooks.Add

    'imaginary loop
    For i = 0 To 5

        Dim msgResponse = MessageBox.Show("Do you want to loop?", "Keep Looping?", MessageBoxButtons.YesNo)

        If msgResponse = Windows.Forms.DialogResult.No Then Exit For

        If firstRun Then
            xlSheet = xlWb.Sheets(1)
            firstRun = False
        Else
            xlWb.Activate()
            xlSheet = xlWb.Sheets.Add(After:=xlWb.Sheets(xlWb.Sheets.Count))
        End If

        xlSheet.Name = "TEST" & i
        xlSheet.Range("A1").Value = "Some Data"


    Next i

End Sub
End Class

You will need to make sure that you clean up your references once you are certain the user is done with the sheet.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top