2015-05-24 212 views
0

我試圖記錄(1.什麼幻燈片和2.時間)到電子表格每次在演示模式下查看幻燈片。當我這樣做時,我不希望電子表格打開,我希望它自動保存。我一直在使用它幾個小時,而且我獲得了不同的成功。我似乎無法按預期工作。VBA PowerPoint寫入幻燈片的幻燈片更改

下面是到目前爲止我一起擠代碼:我沒有嘗試過的代碼進行

Sub OnSlideShowPageChange(ByVal SSW As SlideShowWindow) 
    Dim appExcel As Excel.Application 
    Dim wkb As Excel.Workbook 
    Dim wks As Excel.Worksheet 
    Dim strSheet As String 
    Dim strPath As String 
    Dim curentSlide As Integer 
    Dim timez As Date 
    Dim z As Integer 
    strSheet = "test.xlsx" 
    strPath = "C:\PPToutput\" 
    strSheet = strPath & strSheet 
    Dim counter As Integer 
    counter = 0 
    counter = counter + 1 

    currentslide = ActivePresentation.SlideShowWindow.View.Slide.SlideIndex 
    timez = Now() 

    If Not IsNull(appExcel) And counter < 2 Then 
     Set appExcel = CreateObject("Excel.Application") 
     appExcel.Application.DisplayAlerts = False 
     appExcel.Workbooks.Open (strSheet) 
     Set wkb = appExcel.ActiveWorkbook 
     Set wks = wkb.Sheets(1) 
     wks.Activate 
    End If 
    appExcel.Application.Visible = True 
    Range("A" & Rows.Count).End(xlUp).Offset(1).Value = "Slide " & currentslide 
    Range("B" & Rows.Count).End(xlUp).Offset(1).Value = timez 
    wks.Columns.AutoFit 
    wkb.SaveAs 
    Set appExcel = Nothing 
    appExcel.Workbooks.Close 
    appExcel.Quit 
    Set appExcel = Nothing 
End Sub 
+0

具體說明哪一部分不按預期工作。通過「不想讓電子表格打開...」,你的意思是你不希望Excel可見或根本不打開?如果Excel未打開,那麼寫入範圍相當困難... –

+0

讓我澄清一下:我希望Excel在寫入時隱藏在用戶的視圖中。這是爲了記錄從幻燈片到幻燈片以及從開始到結束的PowerPoint演示時間。 理想情況下,腳本將在後臺打開Excel,每次幻燈片更改時寫入一個新行,並在PowerPoint演示文稿結束時保存並關閉。希望上面編輯的代碼更易於閱讀。我知道代碼是有缺陷的。它會打開太多的Excel實例而無法正確寫入。 – mmcdon8

+0

您使用的是什麼版本的PowerPoint?此代碼是否在加載項中運行? –

回答

0

的事,但我注意到的是這一行:

appExcel.Application.Visible = False 

來後excel程序做的東西。我可以想象這個工作簿的打開是可見的,因爲這發生在這一行之前。

此外,我沒有看到你在哪裏告訴OnSlideShowPageChange子項關於在SlideShowBegin子項中創建的工作簿的任何內容。你正在告訴它做一些範圍內的事情,這不是你之前宣佈的範圍。所以,它認爲你在談論某個範圍的幻燈片。 Powerpoint甚至有範圍嗎?

另一個錯誤是您將所有公開聲明設置爲空。一旦你嘗試再次打電話給他們,你什麼都不會打電話。在錯誤處理程序中執行該操作仍然是一個好主意,但不是該過程的正常部分。

看那[未經測試]改變我做了,看看它們是否有意義:

Public appExcel As Excel.Application 
Public wkb As Excel.Workbook 
Public wks As Excel.Worksheet 
Public rng As Excel.Range 
Public strSheet As String 
Public strPath As String 
Public intRowCounter As Integer 
Public intColumnCounter As Integer 
Public itm As Object 

Sub SlideShowBegin() 
On Error GoTo ErrHandler 

strSheet = "test.xlsx" 
strPath = "C:\PPToutput\" 
strSheet = strPath & strSheet 
Debug.Print strSheet 
'Select export folder 

Dim curentSlide As Integer 
Dim timez As Date 
Dim z As Integer 
Dim placeholder1 As String 
Dim placeholder2 As String 
currentslide = ActivePresentation.SlideShowWindow.View.Slide.SlideIndex 
timez = Now() 

Set appExcel = CreateObject("Excel.Application") 
appExcel.Application.Visible = False 
appExcel.Workbooks.Open (strSheet) 
Set wkb = appExcel.ActiveWorkbook 
Set wks = wkb.Sheets(1) 
wks.Activate 
wks.Range("A1").Value = "Current Slide" 
wks.Range("B1").Value = "Time" 

Exit Sub 

ErrHandler:  
    If Err.Number = 1004 Then 
     MsgBox strSheet & " doesn't exist", vbOKOnly, _ 
     "Error" 
    Else 
     MsgBox Err.Number & "; Description: ", vbOKOnly, _ 
     "Error" 
    End If 

    Set appExcel = Nothing 
    Set wkb = Nothing 
    Set wks = Nothing 
    Set rng = Nothing 
    Set msg = Nothing 
    Set nms = Nothing 
    Set fld = Nothing 
    Set itm = Nothing 

End Sub 

Sub OnSlideShowPageChange(ByVal SSW As SlideShowWindow) 
    Dim curentSlide As Integer 
    Dim timez As Date 
    Dim z As Integer 
    Dim placeholder1 As String 
    Dim placeholder2 As String 

    currentslide = ActivePresentation.SlideShowWindow.View.Slide.SlideIndex 
    timez = Now() 
    wks.Range("A" & Rows.Count).End(xlUp).Offset(1).Value = "Slide " & currentslide 
    wks.Range("B" & Rows.Count).End(xlUp).Offset(1).Value = timez 
    wks.Columns.AutoFit 
    wkb.Save 

    If SSW.View.CurrentShowPosition = _ 
     SSW.Presentation.SlideShowSettings.EndingSlide Then 
     wkb.Save 
     wkb.Close 
    End If 
End Sub 
Sub SlideShowEnd() 
    wkb.Save 
    wkb.Close 
End Sub 
+0

將幻燈片索引和時間寫入數組可能會更好;您可以在幻燈片播放結束時打開電子表格/寫入/關閉,而不是重複啓動Excel或啓動它並在整個幻燈片放映過程中保持打開狀態。 –

+0

這真是個好主意。我只是指出可能導致OP代碼無法正常工作的錯誤。你說得對。這將是一個更好的方式去。 – JMcD

+0

我真的應該評論OP的帖子,不是你的。這並不意味着它聽起來像我批評你的。 –

0

我重新安排你的代碼位,所以只初始化在幻燈片放映期間發生一次。一旦幻燈片放映結束,我添加了另一個過程來關閉Excel。

Private appExcel As Excel.Application 
Private wkb As Excel.Workbook 
Private wks As Excel.Worksheet 
Private counter As Integer 

Sub OnSlideShowPageChange(ByVal SSW As SlideShowWindow) 

    ' initialization 
    Dim strSheet As String 
    Dim strPath As String 
    strSheet = "test.xlsx" 
    strPath = "C:\PPToutput\" 
    strSheet = strPath & strSheet 
    Debug.Print strSheet, appExcel Is Nothing 
    If appExcel Is Nothing Then 
     Set appExcel = CreateObject("Excel.Application") 
     appExcel.Application.DisplayAlerts = False 
     appExcel.WindowState = xlMinimized 
     appExcel.Visible = True 
     Set wkb = appExcel.Workbooks.Open(strSheet) 
     Set wks = wkb.Sheets(1) 
     counter = wks.UsedRange.Rows.Count - 1 
    End If 

    ' make log entry 
    Dim currentSlide As Integer 
    currentSlide = ActivePresentation.SlideShowWindow.View.Slide.SlideIndex 
    counter = counter + 1 
    wks.Range("A" & counter).Value = "Slide " & currentSlide 
    wks.Range("B" & counter).Value = Now() 

End Sub 

Sub OnSlideShowTerminate(ByVal Wn As SlideShowWindow) 
    If Not appExcel Is Nothing Then 
     wks.Columns.AutoFit 
     appExcel.WindowState = xlNormal 
     wkb.Close True 
     appExcel.Quit 
    End If 
    Set appExcel = Nothing 
End Sub 

如果它是我的代碼,我還會分解出的初始化代碼,並把它放在自己的過程,使得OnSlideShowPageChange程序集中的幻燈片變化的記錄。