VBA Routine prevents word from closing properly.

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
 
G

Guest

I do not know why, but the VBA routine I describe seems to be working
without problems now. Why this is I do not know. One of the things I did
to try and fix the problem was to update Office 2003, but I have trouble
believing that was the problem. Maybe multiple system reboots did it?

Who knows. Sometimes you just get lucky. I apologize to anyone I troubled
with the original email.
 
J

Jezebel

The problem is likely to be that the first time you ran it, you already had
Word open, and the second time you did not. Your code (set objWord = NEW
Word.Application) will always create a new instance of Word, as opposed to
getting a reference to an existing instance if any. Your second instance
can't save changes to normal.dot because the first instance has it locked.
 
E

EHPorter

Thank you for the tip. This makes sense. How can I change this code so
that it is conditional; i.e., that if Word is already open, it uses the
currently opened instance of Word, but opens a new instance of word if word
is not opened?

As I indicated, I am not very experienced at this yet, and your help would
be appreciated.
 
J

Jezebel

You can do it like this:

on error resume next 'Ignore errors for the
moment
Set objWord = Word.Application 'Returns an existing instance if
any, or an error if not
on error goto 0 'Re-enable normal
error handling

'If no existing instance of Word, create a new one
if objWord is nothing then
Set objWord = New Word.Application
end if
 
W

Word Heretic

G'day <EHPorter>,

When you are kicking off Word it is starting another instance of
WinWord.exe. Destroy all objects in use in your code to minimise this
problem. You may also just want to test Tasks for WinWord.exe running
already, and just ref to that.


Steve Hudson - Word Heretic
Want a hyperlinked index? S/W R&D? See WordHeretic.com

steve from wordheretic.com (Email replies require payment)
 
E

EHPorter

Thank you; this seems to work very well.


Jezebel said:
You can do it like this:

on error resume next 'Ignore errors for the
moment
Set objWord = Word.Application 'Returns an existing instance if
any, or an error if not
on error goto 0 'Re-enable normal
error handling

'If no existing instance of Word, create a new one
if objWord is nothing then
Set objWord = New Word.Application
end if







opposed not
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top