G
Guest
I am working on a VBA routine which starts in/from Outlook 2003, but runs
primarily in Word 2003. Because the problem seems to relate to the Windows
code (the Outlook section seems to run fine), I've posted the question here,
rather than in an Outlook group. The purpose of this procedure is to create
a new Word document containing the mailing address for each selected
contact, including in the document a standard form letterhead and signature
block. The routine works, but causes one odd problem.
The routine opens the new document as Document 2. When I try and close Word
afterwords, however, it generates the following error message:
"Word cannot save this file because it is already open somewhere else
(Normal.Dot)" I presume that the file that is keeping Normal.Dot open
somewhere else is generated by my VBA routine, but I cannot figure out
where.
I am primarily an end user, so my understanding of VBA programming is
primitive to say the least. Most of the code in the following routine was
adapted from Jim Boyce's book "Outlook Inside and Out." I have not a clue
why this VBA routine is doing what it is doing, and any assistance would be
appreciated.
**********************************************
What I have so far
**********************************************
Public Sub SendLettertoContact()
Dim itmContact As Outlook.ContactItem
Dim selContacts As Selection
Dim objWord As Word.Application
Dim objLetter As Word.Document
Dim secNewArea As Word.Section
Set selContacts = Application.ActiveExplorer.Selection
If selContacts.Count > 0 Then
Set objWord = New Word.Application
For Each itmContact In selContacts
Set objLetter = objWord.Documents.Add
With objWord.ActiveDocument.PageSetup
.TopMargin = InchesToPoints(0.8)
.BottomMargin = InchesToPoints(1)
.LeftMargin = InchesToPoints(1)
.RightMargin = InchesToPoints(1)
End With
objWord.Selection.InsertFile FileName:="c:\word\header\Legal
Letterhead.doc"
With objWord.Selection.ParagraphFormat
.Alignment = wdAlignParagraphCenter
End With
objWord.Selection.InsertDateTime DateTimeFormat:="MMMM d, yyyy",
InsertAsField:= _
False, DateLanguage:=wdEnglishUS, CalendarType:=wdCalendarWestern, _
InsertAsFullWidth:=False
objWord.Selection.TypeParagraph
objWord.Selection.TypeParagraph
With objWord.Selection.ParagraphFormat
.Alignment = wdAlignParagraphLeft
End With
objLetter.Select
objWord.Selection.InsertAfter itmContact.FullName
objLetter.Paragraphs.Add
If itmContact.CompanyName <> "" Then
objWord.Selection.InsertAfter itmContact.CompanyName
objLetter.Paragraphs.Add
End If
If itmContact.BusinessAddress <> "" Then
objWord.Selection.InsertAfter itmContact.BusinessAddress
GoTo Complete
End If
If itmContact.HomeAddress <> "" Then
objWord.Selection.InsertAfter itmContact.HomeAddress
GoTo Complete
End If
Complete:
With objLetter
.Paragraphs.Add
.Paragraphs.Add
End With
With objWord.Selection
.Collapse wdCollapseEnd
.InsertAfter "Re:" + vbTab
.Paragraphs.Add
.Paragraphs.Add
.InsertAfter "Dear " & itmContact.FullName & ":"
.Paragraphs.Alignment = wdAlignParagraphLeft
End With
Set secNewArea = objLetter.Sections.Add(Start:=wdSectionContinuous)
With secNewArea.Range
.Paragraphs.Add
.InsertAfter vbTab + "<Insert text of letter here>"
.Paragraphs.Add
.Paragraphs.Add
End With
Set secNewArea = objLetter.Sections.Add(Start:=wdSectionContinuous)
With secNewArea.Range
.InsertFile FileName:= _
"C:\word\DocAssembly\Signatures\Standard Signature.doc", Range:="",
_
ConfirmConversions:=False, Link:=False, Attachment:=False
End With
objWord.Selection.MoveDown Unit:=wdLine, Count:=2
objWord.Selection.HomeKey Unit:=wdLine
objWord.Selection.MoveRight Unit:=wdCharacter, Count:=1
Next
objWord.Visible = True
End If
ChangeFileOpenDirectory "C:\l\client\"
End Sub
primarily in Word 2003. Because the problem seems to relate to the Windows
code (the Outlook section seems to run fine), I've posted the question here,
rather than in an Outlook group. The purpose of this procedure is to create
a new Word document containing the mailing address for each selected
contact, including in the document a standard form letterhead and signature
block. The routine works, but causes one odd problem.
The routine opens the new document as Document 2. When I try and close Word
afterwords, however, it generates the following error message:
"Word cannot save this file because it is already open somewhere else
(Normal.Dot)" I presume that the file that is keeping Normal.Dot open
somewhere else is generated by my VBA routine, but I cannot figure out
where.
I am primarily an end user, so my understanding of VBA programming is
primitive to say the least. Most of the code in the following routine was
adapted from Jim Boyce's book "Outlook Inside and Out." I have not a clue
why this VBA routine is doing what it is doing, and any assistance would be
appreciated.
**********************************************
What I have so far
**********************************************
Public Sub SendLettertoContact()
Dim itmContact As Outlook.ContactItem
Dim selContacts As Selection
Dim objWord As Word.Application
Dim objLetter As Word.Document
Dim secNewArea As Word.Section
Set selContacts = Application.ActiveExplorer.Selection
If selContacts.Count > 0 Then
Set objWord = New Word.Application
For Each itmContact In selContacts
Set objLetter = objWord.Documents.Add
With objWord.ActiveDocument.PageSetup
.TopMargin = InchesToPoints(0.8)
.BottomMargin = InchesToPoints(1)
.LeftMargin = InchesToPoints(1)
.RightMargin = InchesToPoints(1)
End With
objWord.Selection.InsertFile FileName:="c:\word\header\Legal
Letterhead.doc"
With objWord.Selection.ParagraphFormat
.Alignment = wdAlignParagraphCenter
End With
objWord.Selection.InsertDateTime DateTimeFormat:="MMMM d, yyyy",
InsertAsField:= _
False, DateLanguage:=wdEnglishUS, CalendarType:=wdCalendarWestern, _
InsertAsFullWidth:=False
objWord.Selection.TypeParagraph
objWord.Selection.TypeParagraph
With objWord.Selection.ParagraphFormat
.Alignment = wdAlignParagraphLeft
End With
objLetter.Select
objWord.Selection.InsertAfter itmContact.FullName
objLetter.Paragraphs.Add
If itmContact.CompanyName <> "" Then
objWord.Selection.InsertAfter itmContact.CompanyName
objLetter.Paragraphs.Add
End If
If itmContact.BusinessAddress <> "" Then
objWord.Selection.InsertAfter itmContact.BusinessAddress
GoTo Complete
End If
If itmContact.HomeAddress <> "" Then
objWord.Selection.InsertAfter itmContact.HomeAddress
GoTo Complete
End If
Complete:
With objLetter
.Paragraphs.Add
.Paragraphs.Add
End With
With objWord.Selection
.Collapse wdCollapseEnd
.InsertAfter "Re:" + vbTab
.Paragraphs.Add
.Paragraphs.Add
.InsertAfter "Dear " & itmContact.FullName & ":"
.Paragraphs.Alignment = wdAlignParagraphLeft
End With
Set secNewArea = objLetter.Sections.Add(Start:=wdSectionContinuous)
With secNewArea.Range
.Paragraphs.Add
.InsertAfter vbTab + "<Insert text of letter here>"
.Paragraphs.Add
.Paragraphs.Add
End With
Set secNewArea = objLetter.Sections.Add(Start:=wdSectionContinuous)
With secNewArea.Range
.InsertFile FileName:= _
"C:\word\DocAssembly\Signatures\Standard Signature.doc", Range:="",
_
ConfirmConversions:=False, Link:=False, Attachment:=False
End With
objWord.Selection.MoveDown Unit:=wdLine, Count:=2
objWord.Selection.HomeKey Unit:=wdLine
objWord.Selection.MoveRight Unit:=wdCharacter, Count:=1
Next
objWord.Visible = True
End If
ChangeFileOpenDirectory "C:\l\client\"
End Sub