Posts tagged: automated

Update all pivot tables in a workbook

Option Explicit

Sub RefreshAllPivots()
Dim ws As Worksheet
Dim pt As PivotTable

On Error Resume Next
For Each ws In ActiveWorkbook.Worksheets
   For Each pt In ws.PivotTables
     pt.RefreshTable
   Next
Next
End Sub

Outlook macro to save attachments

Here is an Outlook macro that saves email attachments to the C:\ folder. It uses a For Loop nested in an If statement, as well as a Select Case statement.

Sub GoThroughAttachments()
Dim MyItem As Outlook.MailItem
Dim myAttachments As Outlook.Attachments
Dim i As Long
Dim Att As String

On Error Resume Next
Select Case TypeName(Application.ActiveWindow)
    Case "Explorer"
        Set MyItem = ActiveExplorer.Selection.Item(1)
    Case "Inspector"
        Set MyItem = ActiveInspector.CurrentItem
    Case Else
End Select
On Error GoTo 0
   
If MyItem Is Nothing Then
    Exit Sub
End If
   
Set myAttachments = MyItem.Attachments

If myAttachments.Count> 0 Then
    For i = 1 To myAttachments.Count
        Att = myAttachments.Item(i).DisplayName
        myAttachments.Item(i).SaveAsFile "C:\" & Att
    Next i
End If

Set myAttachments = Nothing
Set MyItem = Nothing
End Sub

Source: http://www.codeforexcelandoutlook.com/blog/