PPoinTer
05-13-2014, 01:27 PM
I'm looking to create a few additions / changes to my PPT toolbars using a few custom macros. I have successfully built a few of these macros, as well as learned how to place these macros in existing tabs using the Custom UI Editor for MS office. However, one of these macros in particular is causing an unusual issue that I can't quite figure out.
When opening a file, the all the macros work. However, after executing a 'send specific slides' macro that I built, all the custom macros no longer work after this. I have tried a number of different changes to this macro, as well as removing all the other macros, but none of this has worked.
Couple other quick notes:
I am currently using Office 2013
I'm currently working in the file as a pptm, but plan on converting to an add-in once this bug is fixed
When you save the file as a new name, the macro then works again for one execution
Below is the language from the macro as well as the Custom UI editor.
Option Explicit
Sub AttachSelection(Optional control As IRibbonControl)
Dim oPres As Presentation
Dim oSlide As Slide
Dim sTempFile As String
Dim sFileName As String
Dim tFileName As String
Dim iCounter As Integer
Dim oMessage As Variant
Dim olMailItem As Variant
Dim sFilePath As String
Dim oOutlookApp As Object
Dim Invalids As Variant
Dim e As Variant
Dim strTemp As String
Dim strFileName As String
Dim CleanFileName As String
'If saved not up to date then save
If ActivePresentation.Saved = False Then ActivePresentation.Save
'Create filename
sFileName = ActivePresentation.Name
Invalids = Array("?", "*", ":", "|", "<", ">", "[", "]", "", "/", ".pptx", ".pptm", ".ppt")
strTemp = sFileName
For Each e In Invalids
strTemp = Replace(strTemp, e, "")
Next
CleanFileName = strTemp
tFileName = CleanFileName & "_temp.pptx"
'remove old tags
For Each oSlide In ActivePresentation.Slides
oSlide.Tags.Delete ("SEL")
Next oSlide
'add selected tag
For Each oSlide In ActiveWindow.Selection.SlideRange
oSlide.Tags.Add "SEL", "YES"
Next oSlide
ActivePresentation.SaveCopyAs (Environ("TEMP") & "\" & tFileName)
Set oPres = Presentations.Open(Environ("TEMP") & "\" & tFileName)
sTempFile = oPres.FullName
For iCounter = oPres.Slides.Count To 1 Step -1
If oPres.Slides(iCounter).Tags("SEL") <> "YES" Then oPres.Slides(iCounter).Delete
Next iCounter
'Attaches temp file to Outlook email
'If Outlook is not open this takes a while - may want to consider opening a new instance of outlook for this?
Set oOutlookApp = GetObject(Class:="Outlook.application")
Set oMessage = oOutlookApp.CreateItem(olMailItem)
With oMessage
.Attachments.Add sTempFile
.Subject = sFileName
.Display
End With
oPres.Saved = True
oPres.Close
''Tried including the bottom items to see if it would fix it. Did not.
'Set oSlide = Nothing
'Set oPres = Nothing
'Set oMessage = Nothing
'Set olMailItem = Nothing
'Set oPres = Nothing
'Set oOutlookApp = Nothing
'Set Invalids = Nothing
'Set e = Nothing
'Delete temp file
Kill sTempFile
End Sub
<!-- Creates Custom Tab -->
<customUI xmlns="ht........m/office/2009/07/customui">
<ribbon startFromScratch="false">
<tabs>
<tab idMso="TabHome">
<group id="CustomGroup" label="Send Slides" insertAfterMso="Editing">
<button id="AttachSelection" label="Attach Selection" onAction="AttachSelection" imageMso="ManageReplies" screentip="Attach Selected Slides" supertip="Attaches the selected slides to an email" keytip="AS"/>
</group>
</tab>
</tabs>
</ribbon>
</customUI>
Note, I previously attached this same question to stack overflow but still haven't reached a solve. The code above is for a new simplified file, with no other macros included and no other code. What you see is what you get. Any help you can provide would be much appreciated, this problem is driving me a bit nuts!
When opening a file, the all the macros work. However, after executing a 'send specific slides' macro that I built, all the custom macros no longer work after this. I have tried a number of different changes to this macro, as well as removing all the other macros, but none of this has worked.
Couple other quick notes:
I am currently using Office 2013
I'm currently working in the file as a pptm, but plan on converting to an add-in once this bug is fixed
When you save the file as a new name, the macro then works again for one execution
Below is the language from the macro as well as the Custom UI editor.
Option Explicit
Sub AttachSelection(Optional control As IRibbonControl)
Dim oPres As Presentation
Dim oSlide As Slide
Dim sTempFile As String
Dim sFileName As String
Dim tFileName As String
Dim iCounter As Integer
Dim oMessage As Variant
Dim olMailItem As Variant
Dim sFilePath As String
Dim oOutlookApp As Object
Dim Invalids As Variant
Dim e As Variant
Dim strTemp As String
Dim strFileName As String
Dim CleanFileName As String
'If saved not up to date then save
If ActivePresentation.Saved = False Then ActivePresentation.Save
'Create filename
sFileName = ActivePresentation.Name
Invalids = Array("?", "*", ":", "|", "<", ">", "[", "]", "", "/", ".pptx", ".pptm", ".ppt")
strTemp = sFileName
For Each e In Invalids
strTemp = Replace(strTemp, e, "")
Next
CleanFileName = strTemp
tFileName = CleanFileName & "_temp.pptx"
'remove old tags
For Each oSlide In ActivePresentation.Slides
oSlide.Tags.Delete ("SEL")
Next oSlide
'add selected tag
For Each oSlide In ActiveWindow.Selection.SlideRange
oSlide.Tags.Add "SEL", "YES"
Next oSlide
ActivePresentation.SaveCopyAs (Environ("TEMP") & "\" & tFileName)
Set oPres = Presentations.Open(Environ("TEMP") & "\" & tFileName)
sTempFile = oPres.FullName
For iCounter = oPres.Slides.Count To 1 Step -1
If oPres.Slides(iCounter).Tags("SEL") <> "YES" Then oPres.Slides(iCounter).Delete
Next iCounter
'Attaches temp file to Outlook email
'If Outlook is not open this takes a while - may want to consider opening a new instance of outlook for this?
Set oOutlookApp = GetObject(Class:="Outlook.application")
Set oMessage = oOutlookApp.CreateItem(olMailItem)
With oMessage
.Attachments.Add sTempFile
.Subject = sFileName
.Display
End With
oPres.Saved = True
oPres.Close
''Tried including the bottom items to see if it would fix it. Did not.
'Set oSlide = Nothing
'Set oPres = Nothing
'Set oMessage = Nothing
'Set olMailItem = Nothing
'Set oPres = Nothing
'Set oOutlookApp = Nothing
'Set Invalids = Nothing
'Set e = Nothing
'Delete temp file
Kill sTempFile
End Sub
<!-- Creates Custom Tab -->
<customUI xmlns="ht........m/office/2009/07/customui">
<ribbon startFromScratch="false">
<tabs>
<tab idMso="TabHome">
<group id="CustomGroup" label="Send Slides" insertAfterMso="Editing">
<button id="AttachSelection" label="Attach Selection" onAction="AttachSelection" imageMso="ManageReplies" screentip="Attach Selected Slides" supertip="Attaches the selected slides to an email" keytip="AS"/>
</group>
</tab>
</tabs>
</ribbon>
</customUI>
Note, I previously attached this same question to stack overflow but still haven't reached a solve. The code above is for a new simplified file, with no other macros included and no other code. What you see is what you get. Any help you can provide would be much appreciated, this problem is driving me a bit nuts!