2017-04-03 107 views
1

我想在powerpoint演示文稿中打開excel文件。 這是我的代碼:如何在VBA中使用Powerpoint在前臺打開excel

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 

excel文件在後臺打開。這必須在前臺。

有人可以幫助我嗎?

+0

不應該是End Sub而不是End Function?代碼在演示文稿頂部打開工作簿。有什麼問題? – sktneer

回答

0

添加行xlWorkBook.Activate應該足夠了。

您的代碼應該是這樣的:

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 

爲引用:

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

帖子#4

+0

您鏈接的示例假定Excel已處於活動狀態。 – brettdj

+0

不幸的是它不起作用:( –

0

您可以使用AppActivate

下面的代碼使用「test.xlsx - Excel中因爲這是我的測試工作簿的標題

貼紙Scheepstanks Koole.xltm - Excel中應該爲你

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 
0

工作由於您對保持Excel對象及其派生的對象不感興趣,因此您可能想要編碼如下:

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 
+0

不幸的是這不起作用,excel文件在後臺打開。 –

相關問題