Avatar billede lokesa Juniormester
Oprettet i dag kl. 10:22 Der er 1 kommentar og
1 løsning

Makro der gemmer vedhæftet fil fra Msg og omdøber filen

Jeg har en kode som jeg bruger til at gemme vedhæftede filer fra Outlook på lokalt drev. Jeg kan markere ligeså mange Msg som jeg vil, men mit problem er at ofte så har den vedhæftede fil (PDF) det samme navn og derfor betyder det at filen allerede eksistere og så kan jeg ikke gemme flere filer med samme navn.

Kan man lave makroen om så den omdøber hver enkel fil den gemmer eller kan man gøre andet?

vba koden ser således ud:

Public Sub SaveAttachments()
'Update 20200821
Dim xMailItem As Outlook.MailItem
Dim xAttachments As Outlook.Attachments
Dim xSelection As Outlook.Selection
Dim i As Long
Dim xAttCount As Long
Dim xFilePath As String, xFolderPath As String, xSaveFiles As String
Dim xYesNo As Integer
Dim xFlag As Boolean
On Error Resume Next
xFolderPath = CreateObject("WScript.Shell").SpecialFolders(16)
Set xSelection = Outlook.Application.ActiveExplorer.Selection
xFolderPath = xFolderPath & "\Attachments\"
If VBA.Dir(xFolderPath, vbDirectory) = vbNullString Then
    VBA.MkDir xFolderPath
End If
For Each xMailItem In xSelection
    Set xAttachments = xMailItem.Attachments
    xAttCount = xAttachments.Count
    xSaveFiles = ""
    If xAttCount > 0 Then
        For i = xAttCount To 1 Step -1
            xFilePath = xFolderPath & xAttachments.Item(i).FileName
            xFlag = True
            If VBA.Dir(xFilePath, 16) <> Empty Then
                xYesNo = MsgBox("The file is exists, do you want to replace it", vbYesNo + vbInformation, "Kutools for Outlook")
                If xYesNo = vbNo Then xFlag = False
            End If
            If xFlag = True Then
                xAttachments.Item(i).SaveAsFile xFilePath
                If xMailItem.BodyFormat <> olFormatHTML Then
                    xSaveFiles = xSaveFiles & vbCrLf & "<Error! Hyperlink reference not valid.>"
                Else
                    xSaveFiles = xSaveFiles & "<br>" & "<a href='file://" & xFilePath & "'>" & xFilePath & "</a>"
                End If
            End If
        Next i
    End If
Next
Set xAttachments = Nothing
Set xMailItem = Nothing
Set xSelection = Nothing
End Sub
Avatar billede claes57 Ekspert
Skrevet i dag kl. 11:21 #1
den enkle er at sætte (1) efter filnavnet - dvs mellemrum(1) og derefter filtypen (fx .pdf) så vil windows selv finde ud af at nummerere filen (dvs filen som den fremtræder i stifinder) Du vil ikke selv i koden få løbenummeret at vide, så du kan ikke lave link til filen eller andet i samme omgang - det er kun filen der skifter navn.
Jeg gør det fx med billeder - 'julen 2025 (1).jpg' og så styrer windows selv løbenummeret.
Avatar billede lokesa Juniormester
Skrevet i dag kl. 11:47 #2
her var løsningen:
Public Sub SaveAttachments_new_name()

    Dim xMailItem As Outlook.MailItem
    Dim xAttachments As Outlook.Attachments
    Dim xSelection As Outlook.Selection
    Dim i As Long
    Dim xAttCount As Long
    Dim xFilePath As String, xFolderPath As String
    Dim xSaveFiles As String
    Dim xFlag As Boolean
    Dim xCounter As Long
    Dim xError As Boolean
    Dim xSkipped As Long
    On Error GoTo ErrorHandler
   
    xFolderPath = CreateObject("WScript.Shell").SpecialFolders(16)
    Set xSelection = Outlook.Application.ActiveExplorer.Selection
    xFolderPath = xFolderPath & "\Attachments\"
   
    If VBA.Dir(xFolderPath, vbDirectory) = vbNullString Then
        VBA.MkDir xFolderPath
    End If
   
    xSkipped = 0  ' Track skipped attachments
   
    For Each xMailItem In xSelection
        Set xAttachments = xMailItem.Attachments
        xAttCount = xAttachments.Count
        xSaveFiles = ""
       
        If xAttCount > 0 Then
            For i = xAttCount To 1 Step -1
                On Error Resume Next ' Continue even if one file fails
               
                ' Check if the attachment is valid
                If xAttachments.Item(i) Is Nothing Then
                    xSkipped = xSkipped + 1
                  '  Continue For
                End If
               
                ' Generate file path with timestamp
                xFilePath = xFolderPath & Format(Now, "yyyyMMdd_HHmmss") & "_" & xAttachments.Item(i).FileName
                xCounter = 1
               
                ' Check if the file already exists and add a number suffix
                While VBA.Dir(xFilePath) <> ""
                    xFilePath = xFolderPath & Left(xAttachments.Item(i).FileName, InStrRev(xAttachments.Item(i).FileName, ".") - 1) & "_" & xCounter & Mid(xAttachments.Item(i).FileName, InStrRev(xAttachments.Item(i).FileName, "."))
                    xCounter = xCounter + 1
                Wend
               
                ' Save the attachment
                xAttachments.Item(i).SaveAsFile xFilePath
               
                ' Check if the attachment was saved successfully
                If VBA.Dir(xFilePath) = "" Then
                    xSkipped = xSkipped + 1
                End If
               
                ' Debug logging
                Debug.Print "Saved: " & xFilePath
               
                If xMailItem.BodyFormat <> olFormatHTML Then
                    xSaveFiles = xSaveFiles & vbCrLf & "<Error! Hyperlink reference not valid.>"
                Else
                    xSaveFiles = xSaveFiles & "<br>" & "<a href='file://" & xFilePath & "'>" & xFilePath & "</a>"
                End If
               
                On Error GoTo ErrorHandler
            Next i
        End If
    Next

    Set xAttachments = Nothing
    Set xMailItem = Nothing
    Set xSelection = Nothing

    ' If there were skipped files, notify the user
    If xSkipped > 0 Then
        MsgBox xSkipped & " files were skipped due to errors or conflicts.", vbExclamation
    End If
   
    Exit Sub

ErrorHandler:
    MsgBox "Error: " & Err.Description
    Debug.Print "Error: " & Err.Description
    Resume Next
End Sub
Avatar billede Ny bruger Nybegynder

Din løsning...

Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.

Loading billede Opret Preview
Kategori
Excel kurser for alle niveauer og behov – find det kursus, der passer til dig

Log ind eller opret profil

Hov!

For at kunne deltage på Computerworld Eksperten skal du være logget ind.

Det er heldigvis nemt at oprette en bruger: Det tager to minutter og du kan vælge at bruge enten e-mail, Facebook eller Google som login.

Du kan også logge ind via nedenstående tjenester