使用郵件合拼並分存文件資料
2015-12-18 13:59
387 查看
前提:先製作完成Word和資料來源的關聯
Sub ProduceDoc()
'Dim stMedd As String
'Dim obMapp As Variant
'stMedd = "請選擇分割後申請表目錄:" '選擇目錄
'Set obMapp = CreateObject("Shell.Application").BrowseForFolder(0, stMedd, &H1)
'If Not obMapp Is Nothing Then
' linkstr = obMapp.self.Path + "\"
'Else
' Exit Sub
'End If
Dim fso As Object
Dim strSrcName As String, strNewName As String
Set fso = CreateObject("Scripting.FileSystemObject")
strSrcName = ActiveDocument.FullName
'MsgBox fso.GetParentFolderName(strSrcName)
ActiveDocument.MailMerge.DataSource.ActiveRecord = wdFirstRecord
For i = 1 To ActiveDocument.MailMerge.DataSource.RecordCount
strNewName = fso.BuildPath(fso.GetParentFolderName(strSrcName), _
fso.GetBaseName(strSrcName) & "_" & i & "." & fso.GetExtensionName(strSrcName))
DoWork (strNewName)
Next i
MsgBox "匯出" + CStr((ActiveDocument.MailMerge.DataSource.RecordCount)) + " 筆資料,結束!"
End Sub
Sub DoWork(filePath As String)
Dim DokName As String
With ActiveDocument.MailMerge
.Destination = wdSendToNewDocument
.SuppressBlankLines = True
With .DataSource
.FirstRecord = ActiveDocument.MailMerge.DataSource.ActiveRecord
.LastRecord = ActiveDocument.MailMerge.DataSource.ActiveRecord
DokName = .DataFields("FieldName").Value 'Change "FieldName" to your MailMerge field name
End With
' Merge the active record
.Execute Pause:=False
End With
ActiveDocument.Range(0, 0).Select
Selection.PageSetup.SectionStart = wdSectionContinuous
Selection.WholeStory
Selection.Fields.Update '更新照片欄位資料
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With ActiveDocument.Content.Find 'Selection.Find
.Text = "<br />" ‘取代<br />為斷行
.Replacement.Text = "^p"
.Forward = True
.Wrap = wdFindContinue ‘不跳出取代後結果
'.Wrap = wdFindAsk have message show
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchByte = True
.CorrectHangulEndings = False
.HanjaPhoneticHangul = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = False
.MatchFuzzy = False
.Execute Replace:=wdReplaceAll
End With
With ActiveDocument.Content.Find 'Selection.Find
.Text = "^b" ’取代節號為空白
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchByte = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = False
.MatchFuzzy = False
.Execute Replace:=wdReplaceAll
End With
‘頁尾資料
Selection.Sections(1).Footers(WdHeaderFooterIndex.wdHeaderFooterPrimary).Range.Text = DokName
Selection.Sections(1).Footers(WdHeaderFooterIndex.wdHeaderFooterPrimary).Range.ParagraphFormat.Alignment = wdAlignParagraphRight
' Save the resulting document.
ActiveDocument.SaveAs2 FileName:=filePath, FileFormat:= _
wdFormatXMLDocument, LockComments:=False, Password:="", AddToRecentFiles _
:=True, WritePassword:="", ReadOnlyRecommended:=False, EmbedTrueTypeFonts _
:=False, SaveNativePictureFormat:=False, SaveFormsData:=False, _
SaveAsAOCELetter:=False, CompatibilityMode:=14
' Close the resulting document
ActiveWindow.Close
' Now, back in the template document, advance to next record
ActiveDocument.MailMerge.DataSource.ActiveRecord = wdNextRecord
End Sub
Sub ProduceDoc()
'Dim stMedd As String
'Dim obMapp As Variant
'stMedd = "請選擇分割後申請表目錄:" '選擇目錄
'Set obMapp = CreateObject("Shell.Application").BrowseForFolder(0, stMedd, &H1)
'If Not obMapp Is Nothing Then
' linkstr = obMapp.self.Path + "\"
'Else
' Exit Sub
'End If
Dim fso As Object
Dim strSrcName As String, strNewName As String
Set fso = CreateObject("Scripting.FileSystemObject")
strSrcName = ActiveDocument.FullName
'MsgBox fso.GetParentFolderName(strSrcName)
ActiveDocument.MailMerge.DataSource.ActiveRecord = wdFirstRecord
For i = 1 To ActiveDocument.MailMerge.DataSource.RecordCount
strNewName = fso.BuildPath(fso.GetParentFolderName(strSrcName), _
fso.GetBaseName(strSrcName) & "_" & i & "." & fso.GetExtensionName(strSrcName))
DoWork (strNewName)
Next i
MsgBox "匯出" + CStr((ActiveDocument.MailMerge.DataSource.RecordCount)) + " 筆資料,結束!"
End Sub
Sub DoWork(filePath As String)
Dim DokName As String
With ActiveDocument.MailMerge
.Destination = wdSendToNewDocument
.SuppressBlankLines = True
With .DataSource
.FirstRecord = ActiveDocument.MailMerge.DataSource.ActiveRecord
.LastRecord = ActiveDocument.MailMerge.DataSource.ActiveRecord
DokName = .DataFields("FieldName").Value 'Change "FieldName" to your MailMerge field name
End With
' Merge the active record
.Execute Pause:=False
End With
ActiveDocument.Range(0, 0).Select
Selection.PageSetup.SectionStart = wdSectionContinuous
Selection.WholeStory
Selection.Fields.Update '更新照片欄位資料
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With ActiveDocument.Content.Find 'Selection.Find
.Text = "<br />" ‘取代<br />為斷行
.Replacement.Text = "^p"
.Forward = True
.Wrap = wdFindContinue ‘不跳出取代後結果
'.Wrap = wdFindAsk have message show
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchByte = True
.CorrectHangulEndings = False
.HanjaPhoneticHangul = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = False
.MatchFuzzy = False
.Execute Replace:=wdReplaceAll
End With
With ActiveDocument.Content.Find 'Selection.Find
.Text = "^b" ’取代節號為空白
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchByte = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = False
.MatchFuzzy = False
.Execute Replace:=wdReplaceAll
End With
‘頁尾資料
Selection.Sections(1).Footers(WdHeaderFooterIndex.wdHeaderFooterPrimary).Range.Text = DokName
Selection.Sections(1).Footers(WdHeaderFooterIndex.wdHeaderFooterPrimary).Range.ParagraphFormat.Alignment = wdAlignParagraphRight
' Save the resulting document.
ActiveDocument.SaveAs2 FileName:=filePath, FileFormat:= _
wdFormatXMLDocument, LockComments:=False, Password:="", AddToRecentFiles _
:=True, WritePassword:="", ReadOnlyRecommended:=False, EmbedTrueTypeFonts _
:=False, SaveNativePictureFormat:=False, SaveFormsData:=False, _
SaveAsAOCELetter:=False, CompatibilityMode:=14
' Close the resulting document
ActiveWindow.Close
' Now, back in the template document, advance to next record
ActiveDocument.MailMerge.DataSource.ActiveRecord = wdNextRecord
End Sub
相关文章推荐
- Linux指令--ln
- 曝光卖假币的店铺和旺旺ID
- Java里的Arrays.asList
- 一个不眠冬夜的清晨
- free 查看内存使用情况
- JAVA代码调用url链接工具类
- vim 配置参考
- iOS开发中隐藏键盘
- BZOJ3931 [CQOI2015] 网络吞吐量
- Netstat 命令
- MySQL replace与insert on duplicate效率分析
- df 查看磁盘使用情况命令
- UIScrollView的图片缩放(只加载一张图片的时候)
- 查看mysql参数命令
- Android消息处理机制
- ugui做小地图
- Android蓝牙开发经验总结(二)
- 使用SVN
- wget 下载整个网站,或者特定目录
- 森华易腾IDC机房参观