Hi
I have used the "Record Macro" Function to write the below code in word. I then copied it to Excel cause I want excel to open word and create the mailmerge word document from addresses in the mailmerge.xls file. I have turned on the references to Word VBA scripts in the options and the macro runs to the point of opening word and creating a blank sheet but no further. Please is someone able to spot the flaw in my code? I've marked the place where the 'Debug' tool always highlights if it helps at all.
Code
Sub Open_Word()
Set appWD = CreateObject("Word.Application")
appWD.Visible = True
Documents.Add DocumentType:=wdNewBlankDocument
ActiveDocument.MailMerge.MainDocumentType = wdFormLetters
ActiveDocument.MailMerge.OpenDataSource Name:="C:\Documents and Settings\James Parker\Desktop\MBI\Mailmerge.xls" _
, ConfirmConversions:=False, _
ReadOnly:=False, LinkToSource:=False, AddToRecentFiles:=False, _
PasswordDocument:="", PasswordTemplate:="", WritePasswordDocument:="", _
WritePasswordTemplate:="", Revert:=False, Format:=wdOpenFormatAuto, _
Connection:="", SQLStatement:="", SQLStatement1:=""
'Add 9 Blank Lines 'This is where the debug tool highlights!
Selection.TypeParagraph
Selection.TypeParagraph
Selection.TypeParagraph
Selection.TypeParagraph
Selection.TypeParagraph
Selection.TypeParagraph
Selection.TypeParagraph
Selection.TypeParagraph
ActiveDocument.Fields.Add Range:=Selection.Range, Type:=wdFieldMergeField _
, Text:="""RTLNAME"""
Selection.TypeParagraph
ActiveDocument.Fields.Add Range:=Selection.Range, Type:=wdFieldMergeField _
, Text:="""ADD1"""
Selection.TypeParagraph
ActiveDocument.Fields.Add Range:=Selection.Range, Type:=wdFieldMergeField _
, Text:="""ADD2"""
Selection.TypeParagraph
ActiveDocument.Fields.Add Range:=Selection.Range, Type:=wdFieldMergeField _
, Text:="""ADD3"""
Selection.TypeParagraph
ActiveDocument.Fields.Add Range:=Selection.Range, Type:=wdFieldMergeField _
, Text:="""ADD4"""
Selection.TypeParagraph
ActiveDocument.Fields.Add Range:=Selection.Range, Type:=wdFieldMergeField _
, Text:="""POSTCODE"""
Selection.TypeText Text:="THE MANAGING DIRECTOR"
Selection.MoveUp Unit:=wdLine, Count:=7
Selection.ParagraphFormat.Alignment = wdAlignParagraphRight
Selection.InsertDateTime DateTimeFormat:="dd MMMM yyyy", InsertAsField:= _
True, DateLanguage:=wdEnglishUK, CalendarType:=wdCalendarWestern, _
InsertAsFullWidth:=False
With ActiveDocument.MailMerge
.Destination = wdSendToNewDocument
.SuppressBlankLines = True
With .DataSource
.FirstRecord = wdDefaultFirstRecord
.LastRecord = wdDefaultLastRecord
End With
.Execute Pause:=False
End With
End Sub
Display More
Thanks
James