PDA

View Full Version : Macros to sort merged data



mosslovell
04-20-2010, 10:26 AM
Hiya Guys and Gals

Ive been creating a few mail merged documents in word 2007. I have managed toget one working where a field from Access is copied and then it is used in a macro to find a specific record in the merged document.

Another bit of code then opens a second word document and does the same... Except the same code doesent seem to work and i get a user undefined error pops up. this is the code im using.

PLEASE HELP ME

Sub Sorting2()
' Macro1 Macro
'
'
Dim dsMain As MailMergeDataSource
Dim numRecord As Integer
Dim MyData As DataObject
Dim NINumber As String
Set MyData = New DataObject
MyData.GetFromClipboard
NINumber = MyData.GetText
ActiveDocument.MailMerge.ViewMailMergeFieldCodes = False
Set dsMain = ActiveDocument.MailMerge.DataSource
If dsMain.FindRecord(FindText:=NINumber, _
Field:="NatNumber") = True Then
numRecord = dsMain.ActiveRecord
End If
'open other document
'
ChangeFileOpenDirectory "E:\New Form Filler\"
Documents.Open FileName:="""Type of Service.docm""", _
ConfirmConversions:=False, ReadOnly:=False, AddToRecentFiles:=False, _
PasswordDocument:="", PasswordTemplate:="", Revert:=False, _
WritePasswordDocument:="", WritePasswordTemplate:="", Format:= _
wdOpenFormatAuto, XMLTransform:=""

End Sub