G
gfspiteri
Dear experts,
I have been trying to produce a mail merge using Word 2000 on Windows
2000 and then emailing the results via a macro. The macro first does
the mail merge, which works fine, but when I then try to save using the
activedocument.saveas method, word give an Out of Stack Space error and
crashes, AFTER saving the file. The next section then should
technically email the document as an attachment (and I know this bit
works fine).
I have tried this on another machine and the same thing happens. I
would really appreciate any help on how to solve this or ways of going
around this problem. The VBA code is below,
cheers
Gianfranco
Sub mailmerge()
Documents.Open
FileName:="F:\Users\GSpiteri\ExceedanceProject\exceedance_merge.doc",
ConfirmConversions:=False _
, ReadOnly:=False, AddToRecentFiles:=False,
PasswordDocument:="", _
PasswordTemplate:="", Revert:=False, WritePasswordDocument:="",
_
WritePasswordTemplate:="", Format:=wdOpenFormatAuto
ActiveDocument.mailmerge.Destination = wdSendToNewDocument
ActiveDocument.mailmerge.Execute
End Sub
Sub exceedance()
'saves mailmerge
Dim SaveName As String
Dim Path As String
Dim FileName As String
Windows("exceedance_merge.doc").Activate
ActiveWindow.Close wdDoNotSaveChanges
Path = "F:\Users\GSpiteri\ExceedanceProject\Reports\"
FileName = "exceedance" + Format$(Date, "ddmmyyyy")
SaveName = Path + FileName
'check if document already exists and delete it if yes
If Len(Dir(SaveName & ".doc")) > 0 Then
Kill [SaveName] & ".doc"
MsgBox [SaveName] & ".doc has been annihilated"
Else
MsgBox [SaveName] & ".doc never existed"
End If
'problem part
ActiveDocument.SaveAs ([SaveName])
'crashes here
End Sub
Sub email()
'email document as attachment
Dim bStarted As Boolean
Dim oOutlookApp As Outlook.Application
Dim oItem As Outlook.MailItem
On Error Resume Next
If Len(ActiveDocument.Path) = 0 Then
MsgBox "Document needs to be saved first"
Exit Sub
End If
Set oOutlookApp = GetObject(, "Outlook.Application")
If Err <> 0 Then
Set oOutlookApp = CreateObject("Outlook.Application")
bStarted = True
End If
Set oItem = oOutlookApp.CreateItem(olMailItem)
With oItem
.To = "(e-mail address removed)"
.CC = ""
.Subject = "Exceedance report, Northern Ireland, " & Format$(Date,
"dd-MMM-yyyy")
.Body = "Exceedance report generated on " & Format$(Date, "dd MMMM
yyyy") & " attached," & vbCr & "Gianfranco" & vbCr
'Add the document as an attachment, you can use the .displayname
property
'to set the description that's used in the message
.Attachments.Add Source:=ActiveDocument.FullName, Type:=olByValue,
_
DisplayName:="ExceedanceReport" & Format$(Date, "mm-dd-yyyy")
.Send
End With
If bStarted Then
oOutlookApp.Quit
End If
Set oItem = Nothing
Set oOutlookApp = Nothing
End Sub
I have been trying to produce a mail merge using Word 2000 on Windows
2000 and then emailing the results via a macro. The macro first does
the mail merge, which works fine, but when I then try to save using the
activedocument.saveas method, word give an Out of Stack Space error and
crashes, AFTER saving the file. The next section then should
technically email the document as an attachment (and I know this bit
works fine).
I have tried this on another machine and the same thing happens. I
would really appreciate any help on how to solve this or ways of going
around this problem. The VBA code is below,
cheers
Gianfranco
Sub mailmerge()
Documents.Open
FileName:="F:\Users\GSpiteri\ExceedanceProject\exceedance_merge.doc",
ConfirmConversions:=False _
, ReadOnly:=False, AddToRecentFiles:=False,
PasswordDocument:="", _
PasswordTemplate:="", Revert:=False, WritePasswordDocument:="",
_
WritePasswordTemplate:="", Format:=wdOpenFormatAuto
ActiveDocument.mailmerge.Destination = wdSendToNewDocument
ActiveDocument.mailmerge.Execute
End Sub
Sub exceedance()
'saves mailmerge
Dim SaveName As String
Dim Path As String
Dim FileName As String
Windows("exceedance_merge.doc").Activate
ActiveWindow.Close wdDoNotSaveChanges
Path = "F:\Users\GSpiteri\ExceedanceProject\Reports\"
FileName = "exceedance" + Format$(Date, "ddmmyyyy")
SaveName = Path + FileName
'check if document already exists and delete it if yes
If Len(Dir(SaveName & ".doc")) > 0 Then
Kill [SaveName] & ".doc"
MsgBox [SaveName] & ".doc has been annihilated"
Else
MsgBox [SaveName] & ".doc never existed"
End If
'problem part
ActiveDocument.SaveAs ([SaveName])
'crashes here
End Sub
Sub email()
'email document as attachment
Dim bStarted As Boolean
Dim oOutlookApp As Outlook.Application
Dim oItem As Outlook.MailItem
On Error Resume Next
If Len(ActiveDocument.Path) = 0 Then
MsgBox "Document needs to be saved first"
Exit Sub
End If
Set oOutlookApp = GetObject(, "Outlook.Application")
If Err <> 0 Then
Set oOutlookApp = CreateObject("Outlook.Application")
bStarted = True
End If
Set oItem = oOutlookApp.CreateItem(olMailItem)
With oItem
.To = "(e-mail address removed)"
.CC = ""
.Subject = "Exceedance report, Northern Ireland, " & Format$(Date,
"dd-MMM-yyyy")
.Body = "Exceedance report generated on " & Format$(Date, "dd MMMM
yyyy") & " attached," & vbCr & "Gianfranco" & vbCr
'Add the document as an attachment, you can use the .displayname
property
'to set the description that's used in the message
.Attachments.Add Source:=ActiveDocument.FullName, Type:=olByValue,
_
DisplayName:="ExceedanceReport" & Format$(Date, "mm-dd-yyyy")
.Send
End With
If bStarted Then
oOutlookApp.Quit
End If
Set oItem = Nothing
Set oOutlookApp = Nothing
End Sub