简体   繁体   中英

how to open excel on foreground in VBA using powerpoint

i am trying to open an excel file in a powerpoint presentation. this is my code:

Sub diversestickersKoole()

Dim xlApp As Object
Dim xlWorkBook As Object

Set xlApp = CreateObject("Excel.Application")

xlApp.Visible = True
Set xlWorkBook = xlApp.Workbooks.Open("V:\Oliedocs\Koole\Stickers Scheepstanks Koole.xltm", True, False)

Set xlApp = Nothing    
Set xlWorkBook = Nothing
End Sub

the excel file is opening in the background. this must be in the foreground.

can someone help me?

Adding the line xlWorkBook.Activate should be enough.

Your code should look like this:

Sub diversestickersKoole()

Dim xlApp As Object
Dim xlWorkBook As Object

Set xlApp = CreateObject("Excel.Application")

xlApp.Visible = True
Set xlWorkBook = xlApp.Workbooks.Open("V:\Oliedocs\Koole\Stickers   Scheepstanks Koole.xltm", True, False)

xlWorkBook.Activate

Set xlApp = Nothing

Set xlWorkBook = Nothing


End Function

For references:

https://www.mrexcel.com/forum/excel-questions/670476-excel-visual-basic-applications-test-if-workbook-open-if-so-bring-front.html

Post #4

You can use AppActivate .

The code below uses "test.xlsx - Excel as this is the caption of my test workbook.

Stickers Scheepstanks Koole.xltm - Excel should work for you

Sub diversestickersKoole()

Dim xlApp As Object
Dim xlWorkBook As Object

Set xlApp = CreateObject("Excel.Application")

xlApp.Visible = True
'Set xlWorkBook = xlApp.Workbooks.Open("V:\Oliedocs\Koole\Stickers Scheepstanks Koole.xltm", True, False)
Set xlWorkBook = xlApp.Workbooks.Open("C:\temp\test.xlsx", True, False)
AppActivate "test.xlsx - Excel"

Set xlApp = Nothing

Set xlWorkBook = Nothing

End Sub

since you seem not interested in keeping Excel object and its derived ones , you may want to code like follows

Sub diversestickersKoole()
    With CreateObject("Excel.Application") '<--| create a new Excel instance and reference it (all its derived objects will be reached by a 'dot')
        .Visible = True 
        .WindowState = -4137 '<--| maximize Excel window
        .Workbooks.Open("V:\Oliedocs\Koole\Stickers Scheepstanks Koole.xltm", True, False).Activate
    End With
End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM