Save an Excel file which contains a string from Outlook2007 - vba

Im newbiee in VBA, so i need a little help.
My goal is make an Outlook rule, but i have a problem:
I want to save one excel (xlsx) file from my Outlook Inbox to my PC. But only the file which contains (in spreadsheet) a string. But it saves (or not saving anything) the last excel file.. (not checking for MYSTRING)
Using this code:
Option Explicit
Sub CheckAttachments(olItem As MailItem)
Const strPath As String = "C:\Users\PC2\Documents\Temp_attachs\"
Const strFindText As String = "Completed"
Dim strFilename As String
Dim olAttach As Attachment
Dim xlApp As Object
Dim xlWB As Object
Dim xlSheet As Object
Dim bXStarted As Boolean
Dim bFound As Boolean
If olItem.Attachments.Count > 0 Then
For Each olAttach In olItem.Attachments
If Right(LCase(olAttach.FileName), 4) = "xlsx" Then
strFilename = strPath & Format(olItem.ReceivedTime, "yyyymmdd-HHMMSS") & _
Chr(32) & olAttach.FileName
olAttach.SaveAsFile strFilename
On Error Resume Next
Set xlApp = GetObject(, "Excel.Application")
If Err <> 0 Then
Application.StatusBar = "Please wait while Excel source is opened ... "
Set xlApp = CreateObject("Excel.Application")
bXStarted = True
End If
On Error GoTo 0
'Open the workbook to read the data
Set xlWB = xlApp.Workbooks.Open(strFilename)
Set xlSheet = xlWB.Sheets("Sheet1")
If FindValue(strFindText, xlSheet) Then
MsgBox "Value found in " & strFilename
bFound = True
End If
xlWB.Close 0
If bXStarted Then xlApp.Quit
If Not bFound Then Kill strFilename
Exit For
End If
Next olAttach
End If
End Sub
Function FindValue(FindString As String, iSheet As Object) As Boolean
Dim Rng As Object
If Trim(FindString) <> "" Then
With iSheet.Range("A:J")
Set Rng = .Find(What:=FindString, _
After:=.Cells(.Cells.Count), _
LookIn:=-4163, _
LookAt:=1, _
SearchOrder:=1, _
SearchDirection:=1, _
MatchCase:=False)
If Not Rng Is Nothing Then
FindValue = True
Else
FindValue = False
End If
End With
End If
End Function
Sub Test()
Dim olMsg As MailItem
On Error Resume Next
Set olMsg = ActiveExplorer.Selection.Item(1)
CheckAttachments olMsg
End Sub

I think I found your Problem:
You have used Exit For in your For Loop only. So only after scanning 1st file, loop is exited.
You need to remove the Exit For and then your code will work smoothly.
Option Explicit
Sub CheckAttachments(olItem As MailItem)
Const strPath As String = "C:\Users\PC2\Documents\Temp_attachs\"
Const strFindText As String = "Completed"
Dim strFilename As String
Dim olAttach As Attachment
Dim xlApp As Object
Dim xlWB As Object
Dim xlSheet As Object
Dim bXStarted As Boolean
Dim bFound As Boolean
If olItem.Attachments.Count > 0 Then
For Each olAttach In olItem.Attachments
If Right(LCase(olAttach.FileName), 4) = "xlsx" Then
strFilename = strPath & Format(olItem.ReceivedTime, "yyyymmdd-HHMMSS") & _
Chr(32) & olAttach.FileName
olAttach.SaveAsFile strFilename
On Error Resume Next
Set xlApp = GetObject(, "Excel.Application")
If Err <> 0 Then
Application.StatusBar = "Please wait while Excel source is opened ... "
Set xlApp = CreateObject("Excel.Application")
bXStarted = True
End If
On Error GoTo 0
'Open the workbook to read the data
Set xlWB = xlApp.Workbooks.Open(strFilename)
Set xlSheet = xlWB.Sheets("Sheet1")
If FindValue(strFindText, xlSheet) Then
MsgBox "Value found in " & strFilename
bFound = True
End If
xlWB.Close 0
If bXStarted Then xlApp.Quit
If Not bFound Then Kill strFilename
End If
Next olAttach
End If
End Sub
Function FindValue(FindString As String, iSheet As Object) As Boolean
Dim Rng As Object
If Trim(FindString) <> "" Then
With iSheet.Range("A:J")
Set Rng = .Find(What:=FindString, _
After:=.Cells(.Cells.Count), _
LookIn:=-4163, _
LookAt:=1, _
SearchOrder:=1, _
SearchDirection:=1, _
MatchCase:=False)
If Not Rng Is Nothing Then
FindValue = True
Else
FindValue = False
End If
End With
End If
End Function
Sub Test()
Dim olMsg As MailItem
On Error Resume Next
Set olMsg = ActiveExplorer.Selection.Item(1)
CheckAttachments olMsg
End Sub

Related

How to call code where “Argument not optional”?

I want to make a rule for Outlook to move mail.
I have VBA code that works.
How do I call that code as script.
Part of the code:
Option Explicit
Sub CheckAttachments(olItem As MailItem)
Const strPath As String = "C:\Users\PC2\Documents\Temp_attachs\"
Const strFindText As String = "Completed"
Dim strFilename As String
Dim olAttach As Attachment
Dim xlApp As Object
Dim xlWB As Object
Dim xlSheet As Object
Dim bXStarted As Boolean
Dim bFound As Boolean
Dim myNameSpace As Outlook.NameSpace
Dim myInbox As Outlook.Folder
Dim myDestFolder As Outlook.Folder
Set myNameSpace = Application.GetNamespace("MAPI")
Set myInbox = myNameSpace.GetDefaultFolder(olFolderInbox)
Set myDestFolder = myInbox.Folders("Subfolder1")
If olItem.Attachments.Count > 0 Then
For Each olAttach In olItem.Attachments
If Right(LCase(olAttach.FileName), 4) = "xlsx" Then
strFilename = strPath & Format(olItem.ReceivedTime, "yyyymmdd-HHMMSS") & _
Chr(32) & olAttach.FileName
olAttach.SaveAsFile strFilename
On Error Resume Next
Set xlApp = GetObject(, "Excel.Application")
If Err <> 0 Then
Application.StatusBar = "Please wait while Excel source is opened ... "
Set xlApp = CreateObject("Excel.Application")
bXStarted = True
End If
On Error GoTo 0
'Open the workbook to read the data
Set xlWB = xlApp.workbooks.Open(strFilename)
Set xlSheet = xlWB.sheets("Sheet1")
If FindValue(strFindText, xlSheet) Then
olItem.Move myDestFolder
'MsgBox "Value found in " & strFilename
bFound = True
End If
xlWB.Close 0
If bXStarted Then xlApp.Quit
If Not bFound Then Kill strFilename
'Exit For
End If
Next olAttach
End If
End Sub
There is a Function also for FindValue.
I tried:
Sub callmacro(Item as Outlook.MailItem)
call ChcekAttachments
End SUB
I get compiler error message:
Argument not optional
Try this. You need to pass argument in Check sub - (olItem As MailItem)
Sub callmacro(Item as Outlook.MailItem)
call CheckAttachments Item
End sub

VBA - Searching for a SPECIFIC file in a folder and attaching it in an Outlook Mail

I am working with VBA that would send error logs to multiple user. This error log can be found in a folder together with a process log file. These files have dates on their names and are not dependent on Now().
I only want to attach the error log and disregard the process log. I have done multiple research with similar topics and was able to made this code:
Sub SendEmailFail()
Dim OutlookApp As Outlook.Application
Dim OutlookMail As Outlook.MailItem
Dim RecipientF As Object
Dim myRecipientF As Outlook.Recipient
Dim sToF As Object
Dim CCf As Object
Dim myCCf As Outlook.Recipient
Dim sCcF As Object
Dim FilesF As VBA.Collection
Dim mDoneF As String
Dim FileF As Scripting.File
Dim AttsF As Outlook.Attachments
Application.ScreenUpdating = False
Set OutlookApp = New Outlook.Application
Set OutlookMail = OutloookApp.CreateItem(0)
Set FilesF = GetFilesF
mDoneF = Environ("userprofile") & _
"\Documents\Automation Anywhere Files\Automation Anywhere\My Reports\ABC\Logs\Done"
'=========================================START========================================='
Workbooks("ConfigFile.xlsm").Activate
Sheets("Sheet1").Activate
Range("C2").Select
Set RecipientF = Range(ActiveCell, ActiveCell.End(xlDown))
ActiveCell.Offset(0, 1).Select
Set CCf = Range(ActiveCell, ActiveCell.End(xlDown))
On Error Resume Next
With OutlookMail
.Display
End With
With OutlookMail
'Get all recipients from Column C
For Each sToF In RecipientF
Set myRecipientF = OutlookMail.Recipients.Add(sToF)
myRecipientF.Type = olTo
myRecipientF.Resolve
If Not myRecipientF.Resolved Then
myRecipientF.Delete
End If
Next sToF
'Get all CCs from Column D
For Each sCcF In CCf
Set myCCf = OutlookMail.Recipients.Add(sCcF)
myCCf.Type = olCC
myCCf.Resolve
If Not myCCf.Resolved Then
myCCf.Delete
End If
Next sCcF
.Body = ThisWorkbook.Sheets("Sheet1").Range("F2").Value & vbNewLine & _
vbNewLine & ThisWorkbook.Sheets("Sheet1").Range("F3").Value & vbNewLine & _
vbNewLine & ThisWorkbook.Sheets("Sheet1").Range("F4").Value & vbNewLine & _
vbNewLine & "Thank You!"
'Adding Error Logs
If FilesF.Count Then
Set AttsF = OutlookMail.Attachments
For Each File In Files
AttsF.Add FileF.Path
Next
End If
End With
On Error GoTo 0
Set OutlookMail = Nothing
Set OutlookApp = Nothing
Application.ScreenUpdating = True
End Sub
Function GetFilesF() As VBA.Collection
Dim FolderF As Scripting.Folder
Dim FsoF As Scripting.FileSystemObject
Dim FilesF As Scripting.Files
Dim FileF As Scripting.File
Dim ListF As VBA.Collection
Dim mSendF As String
Dim mDoneF As String
Dim StrFileF As String
mSendF = Environ("userprofile") & _
"\Documents\Automation Anywhere Files\Automation Anywhere\My Reports\ABC\Logs\Send"
mDoneF = Environ("userprofile") & _
"\Documents\Automation Anywhere Files\Automation Anywhere\My Reports\ABC\Logs\Done"
Set ListF = New VBA.Collection
Set FsoF = New Scripting.FileSystemObject
Set FolderF = FsoF.GetFolder(mSendF)
Set FilesF = FolderF.FilesF
For Each FileF In FilesF
'Return only visible files
If (FileF.Attributes Or Hidden) <> FileF.Attributes Then
StrFileF = Dir(Environ("userprofile") & _
"\Documents\Automation Anywhere Files\Automation Anywhere\My Reports\ABC\Logs\Send\*Error Log*")
If Len(StrFileF) > 0 Then
List.Add FileF
End If
End If
Next
Set GetFilesF = ListF
End Function
However, I encountered a run time error "424" : object required. This MsgBox only has an OK and HELP Button and a little bit small compared to the usual MsgBox size for errors. I do not know where the error is even though I can the macro using F8 since it doesn't highlight the line after the error was displayed.
EDITED
Changed some declarations and I was able to completely run the macro. Yet, Error logs AND process logs were both attached. I know there is a problem with my codes in searching for a file with "ERROR LOG" on its filename. The modified code was as follows:
Sub SendEmailFail()
Dim OutlookApp As Outlook.Application
Dim OutMail As Outlook.MailItem
Dim RecipientF As Object
Dim myRecipientF As Outlook.Recipient
Dim sToF As Object
Dim CCf As Object
Dim myCCf As Outlook.Recipient
Dim sCcF As Object
Dim Files As VBA.Collection
Dim mDoneF As String
Dim FileF As Scripting.File
Dim AttsF As Outlook.Attachments
Application.ScreenUpdating = False
Set OutlookApp = New Outlook.Application
Set OutMail = OutlookApp.CreateItem(olMailItem)
Set Files = GetFilesF
mDoneF = Environ("userprofile") & _
"\Documents\Automation Anywhere Files\Automation Anywhere\My Reports\AccentureCIO\Logs\Done"
'=========================================START========================================='
Workbooks("ConfigFile.xlsm").Activate
Sheets("Sheet1").Activate
Range("C2").Select
Set RecipientF = Range(ActiveCell, ActiveCell.End(xlDown))
ActiveCell.Offset(0, 1).Select
Set CCf = Range(ActiveCell, ActiveCell.End(xlDown))
On Error Resume Next
With OutMail
.Display
End With
With OutMail
'Get all recipients from Column C
For Each sToF In RecipientF
Set myRecipientF = OutMail.Recipients.Add(sToF)
myRecipientF.Type = olTo
myRecipientF.Resolve
If Not myRecipientF.Resolved Then
myRecipientF.Delete
End If
Next sToF
'Get all CCs from Column D
For Each sCcF In CCf
Set myCCf = OutMail.Recipients.Add(sCcF)
myCCf.Type = olCC
myCCf.Resolve
If Not myCCf.Resolved Then
myCCf.Delete
End If
Next sCcF
.Body = ThisWorkbook.Sheets("Sheet1").Range("F2").Value & vbNewLine & _
vbNewLine & ThisWorkbook.Sheets("Sheet1").Range("F3").Value & vbNewLine & _
vbNewLine & ThisWorkbook.Sheets("Sheet1").Range("F4").Value & vbNewLine & _
vbNewLine & "Thank You!"
'Adding Error Logs
If Files.Count Then
Set AttsF = OutMail.Attachments
For Each FileF In Files
AttsF.Add FileF.Path
Next
End If
End With
On Error GoTo 0
Set OutMail = Nothing
Set OutlookApp = Nothing
Application.ScreenUpdating = True
End Sub
Function GetFilesF() As VBA.Collection
Dim FolderF As Scripting.Folder
Dim FsoF As Scripting.FileSystemObject
Dim FilesF As Scripting.Files
Dim FileF As Scripting.File
Dim ListF As VBA.Collection
Dim mSendF As String
Dim mDoneF As String
Dim StrFileF As String
mSendF = Environ("userprofile") & _
"\Documents\Automation Anywhere Files\Automation Anywhere\My Reports\AccentureCIO\Logs\Send"
mDoneF = Environ("userprofile") & _
"\Documents\Automation Anywhere Files\Automation Anywhere\My Reports\AccentureCIO\Logs\Done"
Set ListF = New VBA.Collection
Set FsoF = New Scripting.FileSystemObject
Set FolderF = FsoF.GetFolder(mSendF)
Set Files = FolderF.Files
For Each FileF In Files
'Return only visible files
If (FileF.Attributes Or Hidden) <> FileF.Attributes Then
StrFileF = Dir(Environ("userprofile") & _
"\Documents\Automation Anywhere Files\Automation Anywhere\My Reports\AccentureCIO\Logs\Send\*Error Log*")
If Len(StrFileF) > 0 Then
ListF.Add FileF
End If
End If
Next
Set GetFilesF = ListF
End Function

VBA export certain data from Outlook to Excel running but producing nothing?

Option Explicit
Sub CopyToExcel()
Dim xlApp As Object
Dim xlWB As Object
Dim xlSheet As Object
Dim olItem As Outlook.MailItem
Dim vText As Variant
Dim sText As String
Dim vItem As Variant
Dim i As Long
Dim rCount As Long
Dim bXStarted As Boolean
Const strPath As String = "C:\Users\Rob\Documents\Excel\Excel.xlsx" 'the path of the workbook
If Application.ActiveExplorer.Selection.Count = 0 Then
MsgBox "No Items selected!", vbCritical, "Error"
Exit Sub
End If
On Error Resume Next
Set xlApp = GetObject(, "Excel.Application")
If Err <> 0 Then
Application.StatusBar = "Please wait while Excel source is opened ... "
Set xlApp = CreateObject("Excel.Application")
bXStarted = True
End If
On Error GoTo 0
'Open the workbook to input the data
Set xlWB = xlApp.Workbooks.Open(strPath)
Set xlSheet = xlWB.Sheets("Sheet1")
'Process each selected record
rCount = xlSheet.UsedRange.Rows.Count
For Each olItem In Application.ActiveExplorer.Selection
sText = olItem.Body
vText = Split(sText, Chr(13))
'Find the next empty line of the worksheet
rCount = rCount + 1
'Check each line of text in the message body
For i = UBound(vText) To 0 Step 1
If InStr(1, vText(i), "Destination -") > 0 Then
vItem = Split(vText(i), Chr(58))
xlSheet.Range("a" & rCount) = Trim(vItem(1))
End If
Next i
xlWB.Save
Next olItem
xlWB.Close SaveChanges:=True
If bXStarted Then
xlApp.Quit
End If
Set xlApp = Nothing
Set xlWB = Nothing
Set xlSheet = Nothing
Set olItem = Nothing
End Sub
This code is from online where I tried making it work for myself...
I need to extract Specific Data from emails (over 5000) and produce them on an Excel document. I've never touched VBA before only C#, Javascript & C++.
The code runs, the excel sheet updates to the current Date/time but nothing is produced?
Any help please?
I also get an error "Subscript out of range" for this line:
xlSheet.Range("A" & rCount) = Trim(vItem(1))
I think you need to change the second split delimiter to match the first one. This will take care of the Subscript error
Use this:
vItem = Split(vText(i), "Destination -")

How to fix Outlook script rule Error

I'm trying to run my code using rule script to only processes newly arrived message but it keeps throwing Error
What am I doing wrong on my code?
Option Explicit
Public Sub Test(Item As Outlook.MailItem)
Dim xlApp As Excel.Application
Dim xlWB As Excel.Workbook
Dim xlSheet As Excel.Worksheet
Dim vText As Variant
Dim sText As String
Dim vItem As Variant
Dim i As Long
Dim rCount As Long
Dim XStarted As Boolean
Dim FileName As String
Dim FilePath As String '// SaveAs CSV File Path
Dim sPath As String '// .CSV File Path
'// the path of the workbook
sPath = "C:\temp\temp.csv"
On Error Resume Next
Set xlApp = GetObject(, "Excel.Application")
If Err <> 0 Then
Application.StatusBar = "Please wait while Excel source is opened ... "
Set xlApp = CreateObject("Excel.Application")
XStarted = True
End If
' On Error GoTo 0
'// Open the workbook to input the data
Set xlWB = xlApp.Workbooks.Open(sPath)
Set xlSheet = xlWB.Sheets("Report")
'// Process received Mail
sText = Item.Body
vText = Split(sText, Chr(13)) ' Chr(13)) carriage return
'// Find the next empty line of the worksheet
rCount = xlSheet.Range("B" & xlSheet.Rows.Count).End(xlUp).Row
rCount = rCount + 1
'// Check each line of text in the message body
For i = UBound(vText) To 0 Step -1
'// Customer Name
If InStr(1, vText(i), "Customer") > 0 Then
vItem = Split(vText(i), Chr(9)) ' Chr(9) horizontal tab
xlSheet.Range("A" & rCount) = Trim(vItem(1))
End If
'// Ref Number
If InStr(1, vText(i), "Order #") > 0 Then
vItem = Split(vText(i), Chr(9))
xlSheet.Range("B" & rCount) = Trim(vItem(1))
End If
'// Service Level
If InStr(1, vText(i), "Service Level") > 0 Then
vItem = Split(vText(i), Chr(9))
xlSheet.Range("J" & rCount) = Trim(vItem(1))
End If
Next i
FilePath = Environ("USERPROFILE") & "\Documents\Temp\"
FileName = Sheets(1).Range("B2").Value
xlWB.SaveAs FileName:=FilePath & FileName
'// Close & SaveChanges
xlWB.Close SaveChanges:=True
If XStarted Then
xlApp.Quit
End If
Set xlApp = Nothing
Set xlWB = Nothing
Set xlSheet = Nothing
Set Item = Nothing
End Sub
Per Patrick.
You may have changed the VBA Project name. Go to Outlook Rules, Edit, re-assign the macro.
Also, it should FileName = xlWB.Sheets(1).Range("B2").Value And get rid of Application.StatusBar = ...
Thanks...

VBA PowerPoint Write to Excel on Slide Change in Slideshow

I am attempting to log (1. what slide and 2. the time) to a spreadsheet each time a slide is viewed in presentation mode. I don't want to have the spreadsheet open when I do this and I want it to save automatically. I've been screwing around with it for a few hours now, and I've had varying success. I can't seem to get it to work as intended.
Here's the code I've crammed together so far:
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
I haven't tried the code out, but something I noticed is that this line:
appExcel.Application.Visible = False
comes after the excel program does stuff. I would imagine the workbook opening would be visible because that happens before this line.
Also, I don't see where you're telling the OnSlideShowPageChange sub anything about the workbook you created in the SlideShowBegin sub. You're telling it to do something with a range, which is not the one you declared earlier. So, it thinks you're talking about some range in the powerpoint. Do powerpoints even have ranges?
The other mistake is that you set all of your public declarations to nothing. Once you try to call them again, you're calling nothing. It's still a good idea to do that in your error handler, but not as a normal part of the process.
Look at the [untested] changes I made and see if they make sense:
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
I rearranged your code a bit so that the initialization only occurs once during the slide show. I added another procedure to close Excel once the slide show has ended.
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
If it were my code, I'd also factor out the initialization code and put it in its own procedure so that the OnSlideShowPageChange procedure focused on the logging of the slide changes.