Mae cwcis yn ein helpu i gyflwyno ein gwasanaethau. Drwy ddefnyddio ein gwasanaethau, rydych yn cytuno i'n defnydd cwcis.
Tip: Mae ieithoedd eraill yn Google-Cyfieithu. Gallwch ymweld â'r English fersiwn o'r ddolen hon.
Mewngofnodi
x
or
x
x
cofrestr
x

or

Sut i drosi neu arbed e-bost ac atodiadau i ffeil PDF sengl yn Outlook?

Mae'r erthygl hon yn sôn am arbed neges e-bost a phob atodiad o fewn iddo i un ffeil PDF yn Outlook.

Trosi neu arbed e-bost ac atodiadau i ffeil PDF sengl gyda chod VBA


Yn hawdd arbed negeseuon e-bost dewisol fel ffeil PDF unigol neu ffeiliau fformat gwahanol yn Outlook:

Efo'r Cadw fel Ffeil cyfleustodau i Kutools ar gyfer Outlook, gallwch chi arbed nifer o negeseuon e-bost dewisol yn hawdd fel ffeil fformat HTML unigol, ffeil fformat TXT, dogfen Word, ffeil CSV yn ogystal â ffeil PDF yn Outlook fel y dangosir y sgrin isod.

Kutools ar gyfer Outlook: gyda mwy na 40 Outlook ategol defnyddiol, yn rhad ac am ddim i geisio heb gyfyngiad mewn diwrnodau 45. Lawrlwythwch a threial am ddim Nawr!

Kutools ar gyfer Outlook: 100 + Offer Uwch Newydd ar gyfer Outlook.
Tab Swyddfa: Galluogi Golygu Tablau a Pori yn y Swyddfa, Just Like Chrome, Firefox, IE 8 / 9 / 10.
Bwydlen Clasurol: Dewch â Hen Fwydlenni a Pheiriau Offer Yn ôl i Office 2007, 2010, 2013, 2016 a 2019.

Trosi neu arbed e-bost ac atodiadau i ffeil PDF sengl gyda chod VBA

Gwnewch fel a ganlyn i arbed e-bost gyda'i holl atodiadau i ffeil PDF sengl yn Outlook.

1. Dewiswch e-bost gydag atodiadau, byddwch yn arbed i un ffeil PDF, ac yna pwyswch Alt + F11 allweddi i agor y Microsoft Visual Basic ar gyfer Ceisiadau ffenestr.

2. Yn y Microsoft Visual Basic ar gyfer Ceisiadau ffenestr, cliciwch Mewnosod > Modiwlau. Ac yna copïwch y cod VBA isod i mewn i ffenestr y Modiwl.

Cod VBA: Cadw e-bost ac atodiad i ffeil PDF sengl

Public Sub MergeMailAndAttachsToPDF()
'Update by Extendoffice 2018/3/5
Dim xSelMails As MailItem
Dim xFSysObj As FileSystemObject
Dim xOverwriteBln As Boolean
Dim xLooper As Integer
Dim xEntryID As String
Dim xNameSpace As Outlook.NameSpace
Dim xMail As Outlook.MailItem
Dim xExt As String
Dim xSendEmailAddr, xCompanyDomain As String
Dim xWdApp As Word.Application
Dim xDoc, xNewDoc As Word.Document
Dim I As Integer
Dim xPDFSavePath As String
Dim xPath As String
Dim xFileArr() As String
Dim xExcel As Excel.Application
Dim xWb As Workbook
Dim xWs As Worksheet
Dim xTempDoc As Word.Document

On Error Resume Next
If (Outlook.ActiveExplorer.Selection.Count > 1) Or (Outlook.ActiveExplorer.Selection.Count = 0) Then
    MsgBox "Please Select a email.", vbInformation + vbOKOnly
    Exit Sub
End If
Set xSelMails = Outlook.ActiveExplorer.Selection.Item(1)
xEntryID = xSelMails.EntryID
Set xNameSpace = Application.GetNamespace("MAPI")
Set xMail = xNameSpace.GetItemFromID(xEntryID)

xSendEmailAddr = xMail.SenderEmailAddress
xCompanyDomain = Right(xSendEmailAddr, Len(xSendEmailAddr) - InStr(xSendEmailAddr, "@"))
xOverwriteBln = False
Set xExcel = New Excel.Application
xExcel.Visible = False
Set xWdApp = New Word.Application
xExcel.DisplayAlerts = False
xPDFSavePath = xExcel.Application.GetSaveAsFilename(InitialFileName:="", FileFilter:="PDF Files(*.pdf),*.pdf")
If xPDFSavePath = "False" Then
    xExcel.DisplayAlerts = True
    xExcel.Quit
    xWdApp.Quit
    Exit Sub
End If
xPath = Left(xPDFSavePath, InStrRev(xPDFSavePath, "\"))
cPath = xPath & xCompanyDomain & "\"
yPath = cPath & Format(Now(), "yyyy") & "\"
mPath = yPath & Format(Now(), "MMMM") & "\"
If Dir(xPath, vbDirectory) = vbNullString Then
   MkDir xPath
End If
EmailSubject = CleanFileName(xMail.Subject)
xSaveName = Format(xMail.ReceivedTime, "yyyymmdd") & "_" & EmailSubject & ".doc"
Set xFSysObj = CreateObject("Scripting.FileSystemObject")
If xOverwriteBln = False Then
   xLooper = 0
  Do While xFSysObj.FileExists(yPath & xSaveName)
      xLooper = xLooper + 1
      xSaveName = Format(xMail.ReceivedTime, "yyyymmdd") & "_" & EmailSubject & "_" & xLooper & ".doc"
   Loop
Else
   If xFSysObj.FileExists(yPath & xSaveName) Then
      xFSysObj.DeleteFile yPath & xSaveName
   End If
End If
xMail.SaveAs xPath & xSaveName, olDoc
If xMail.Attachments.Count > 0 Then
   For Each atmt In xMail.Attachments
      xExt = SplitPath(atmt.filename, 2)
      If (xExt = ".docx") Or (xExt = ".doc") Or (xExt = ".docm") Or (xExt = ".dot") Or (xExt = ".dotm") Or (xExt = ".dotx") _
      Or (xExt = ".xlsx") Or (xExt = ".xls") Or (xExt = ".xlsm") Or (xExt = ".xlt") Or (xExt = ".xltm") Or (xExt = ".xltx") Then
        atmtName = CleanFileName(atmt.filename)
        atmtSave = xPath & Format(xMail.ReceivedTime, "yyyymmdd") & "_" & atmtName
        atmt.SaveAsFile atmtSave
      End If
   Next
End If
Set xNewDoc = xWdApp.Documents.Add("Normal", False, wdNewBlankDocument, False)
Set xFilesFld = xFSysObj.GetFolder(xPath)
xFileArr() = GetFiles(xPath)
For I = 0 To UBound(xFileArr()) - 1
    xExt = SplitPath(xFileArr(I), 2)
    If (xExt = ".xlsx") Or (xExt = ".xls") Or (xExt = ".xlsm") Or (xExt = ".xlt") Or _
       (xExt = ".xltm") Or (xExt = ".xltx") Then  'conver excel to word
        Set xWb = xExcel.Workbooks.Open(xPath & xFileArr(I))
        Set xTempDoc = xWdApp.Documents.Add("Normal", False, wdNewBlankDocument, False)
        Set xWs = xWb.ActiveSheet
        xWs.UsedRange.Copy
        xTempDoc.Content.PasteAndFormat wdFormatOriginalFormatting
        xTempDoc.SaveAs2 xPath & xWs.Name + ".docx", wdFormatXMLDocument
        xWb.Close False
        Kill xPath & xFileArr(I)
        xTempDoc.Close wdDoNotSaveChanges, wdOriginalDocumentFormat, False
    End If
Next
xExcel.DisplayAlerts = True
xExcel.Quit
xFileArr() = GetFiles(xPath)
'Merge Documents
For I = 0 To UBound(xFileArr()) - 1
    xExt = SplitPath(xFileArr(I), 2)
    If (xExt = ".docx") Or (xExt = ".doc") Or (xExt = ".docm") Or (xExt = ".dot") Or _
       (xExt = ".dotm") Or (xExt = ".dotx") Then
        MergeDoc xWdApp, xPath & xFileArr(I), xNewDoc
        Kill xPath & xFileArr(I)
    End If
Next
xNewDoc.Sections.Item(1).Range.Delete wdCharacter, 1
xNewDoc.SaveAs2 xPDFSavePath, wdFormatPDF
xNewDoc.Close wdDoNotSaveChanges, wdOriginalDocumentFormat, False
xWdApp.Quit
Set xMail = Nothing
Set xNameSpace = Nothing
Set xFSysObj = Nothing
MsgBox "Merged successfully", vbInformation + vbOKOnly
End Sub

Public Function SplitPath(FullPath As String, ResultFlag As Integer) As String
Dim SplitPos As Integer, DotPos As Integer
SplitPos = InStrRev(FullPath, "/")
DotPos = InStrRev(FullPath, ".")
Select Case ResultFlag
Case 0
   SplitPath = Left(FullPath, SplitPos - 1)
Case 1
   If DotPos = 0 Then DotPos = Len(FullPath) + 1
   SplitPath = Mid(FullPath, SplitPos + 1, DotPos - SplitPos - 1)
Case 2
   If DotPos = 0 Then DotPos = Len(FullPath)
   SplitPath = Mid(FullPath, DotPos)
Case Else
   Err.Raise vbObjectError + 1, "SplitPath Function", "Invalid Parameter!"
End Select
End Function
  
Function CleanFileName(StrText As String) As String
Dim xStripChars As String
Dim xLen As Integer
Dim I As Integer
xStripChars = "/\[]:=," & Chr(34)
xLen = Len(xStripChars)
StrText = Trim(StrText)
For I = 1 To xLen
StrText = Replace(StrText, Mid(xStripChars, I, 1), "")
Next
CleanFileName = StrText
End Function

Function GetFiles(xFldPath As String) As String()
On Error Resume Next
Dim xFile As String
Dim xFileArr() As String
Dim xArr() As String
Dim I, x As Integer
x = 0
ReDim xFileArr(1)
xFileArr(1) = xFldPath '& "\"
xFile = Dir(xFileArr(1) & "*.*")
Do Until xFile = ""
    x = x + 1
    xFile = Dir
Loop
ReDim xArr(0 To x)
x = 0
xFile = Dir(xFileArr(1) & "*.*")
Do Until xFile = ""
    xArr(x) = xFile
    x = x + 1
    xFile = Dir
Loop
GetFiles = xArr()
End Function

Sub MergeDoc(WdApp As Word.Application, xFileName As String, Doc As Document)
Dim xNewDoc As Document
Dim xSec As Section
    Set xNewDoc = WdApp.Documents.Open(filename:=xFileName, Visible:=False)
    Set xSec = Doc.Sections.Add
    xNewDoc.Content.Copy
    xSec.PageSetup = xNewDoc.PageSetup
    xSec.Range.PasteAndFormat wdFormatOriginalFormatting
    xNewDoc.Close
End Sub

3. Cliciwch offer > cyfeiriadau i agor y cyfeiriadau blwch deialog. Gwiriwch y Llyfrgell Amcanion Microsoft Excel, Microsoft Scripting Runtime ac Llyfrgell Gwrthrychau Microsoft Word blychau ac yna cliciwch ar OK botwm. Gweler y sgrin:

4. Gwasgwch y F5 allwedd neu glicio ar Run botwm i redeg y cod. Yna a Save As pops up dialog, nodwch ffolder i achub y ffeil, yna rhowch enw'r ffeil PDF a chliciwch ar Save botwm. Gweler y sgrin:

5. Yna a Microsoft Outlook pops up dialog, cliciwch y OK botwm.

Nawr caiff yr e-bost a ddewiswyd gyda'i holl atodiadau ei gadw i mewn i ffeil PDF sengl.

Nodyn: Mae'r sgript VBA hwn yn unig yn gweithio ar gyfer atodiadau Microsoft Word ac Excel.


Erthyglau cysylltiedig:



Kutools ar gyfer Outlook

Mwy na Swyddogaethau Uwch 100 ar gyfer Outlook 2010, 2013, 2016, 2019 a 365

sgrinio kutools ar gyfer rhagolygon bach ar gyfer baner 201812

Mwy o Nodweddion | Lawrlwythiad Am Ddim | $ 39.00 yn unig ar gyfer cannoedd o swyddogaethau

Say something here...
symbols left.
You are guest ( Sign Up? )
or post as a guest, but your post won't be published automatically.
Loading comment... The comment will be refreshed after 00:00.
  • To post as a guest, your comment is unpublished.
    Peter · 8 months ago
    A fix that has worked for me is to have MergeDoc read as follows - note Word.Document

    Sub MergeDoc(WdApp As Word.Application, xFileName As String, Doc As Word.Document)
    Dim xNewDoc2 As Word.Document
    Dim xSec As Section

    Set xNewDoc2 = WdApp.Documents.Open(FileName:=xFileName, Visible:=False)
    Set xSec = Doc.Sections.Add
    xNewDoc2.Content.Copy
    xSec.PageSetup = xNewDoc2.PageSetup
    xSec.Range.PasteAndFormat wdFormatOriginalFormatting
    xNewDoc2.Close
    End Sub

    Peter
  • To post as a guest, your comment is unpublished.
    Peter · 8 months ago
    This looks very powerful and just what I am looking for right now.


    All is good, apart from I am having trouble passing the 'Doc as Document' to the Merge routine

    (NB have to replace On Error Resume Next with On Error GoTo 0 to catch the problem) -

    2 difficulties -
    1. Type Mismatch caused by xNewDoc in the merge-call at MergeDoc xWdApp, xPath & xFileArr(I), xNewDoc
    2. And/or Set xSec = Doc.Sections.Add won't compile

    - perhaps because the macro is being run from Outlook and not e.g. Word
    - perhaps because of some local issues at my end

    But very encouraging to have a structure and method to approach the problem
    Thank you.
  • To post as a guest, your comment is unpublished.
    Allan · 9 months ago
    BEAUTIFUL!! And what timing.

    I've been meaning to attempt this for a while now. I look forward to testing it out.

    Thank you