Printing via Word fails

M

Morten Snedker

In Outlook 2002 I've created a macro, that will use Word for printing.
The first time I run the macro, it works fine.

The second time it fails with error:
"462: The remote server machine does not exist or is unavailable"

When the Word document has been opened and the data inserted, I wish
to leave it open for editing, which is why it is not closed.

I've pointed out the place in the code with some capital letters. It
appears I'm not controlling my Word application properly, but I can't
find my mistake. Any pointers?

Thx in advance! =B-)



Sub PrintAttachments()

On Error GoTo myErr

Dim ns As NameSpace

Dim ib As MAPIFolder
Dim msg As MailItem
Dim att As Attachment
Dim strAtt As String
Dim rcp As Recipient

Dim strFra As String
Dim strMig As String
Dim strModtagere As String
Dim strSendt As String
Dim strEmne As String
Dim strBody As String

Set ns = ThisOutlookSession.Session
Set ib = ns.GetDefaultFolder(olFolderInbox)

For Each msg In ib.Application.ActiveExplorer.Selection
'...some code not relevant
Next


Dim appWord As New Word.Application
Dim docWord As New Word.Document

Set docWord = appWord.Documents.Add
appWord.Visible = True

Selection.TypeText " " 'THIS IS WHERE IT FAILS
Selection.TypeParagraph

With Selection
.PageSetup.LeftMargin = CentimetersToPoints(3#)
.Font.Name = "Arial"
.Font.Size = 14
.Font.Bold = True
.TypeText Text:=strMig

'...bla bla...

.TypeText strBody

End With


myExit:
On Error Resume Next
Set appWord = Nothing
Set docWord = Nothing

Set ns = Nothing
Set ib = Nothing
Set MailItem = Nothing
Set att = Nothing

Exit Sub

myErr:
Debug.Print Err.Number & ": " & Err.Description
Resume Next
End Sub
 
M

Morten Snedker

Okay, problem solved, but I don't get it...


Dim appWord As New Word.Application

changed to

If appWord Is Nothing Then
Set appWord = New Word.Application
End If


But at the end of my code I have:
Set appWord = Nothing
Set docWord = Nothing

So why isn't appWord=Nothing ??


Regards /Snedker
 
M

Michael Bauer

Hi Morten,

appWord may be Nothing but you can´t check it in that way. Testing for
Nothing would create a new instance.
 
M

Morten Snedker

Hi Morten,

appWord may be Nothing but you can´t check it in that way. Testing for
Nothing would create a new instance.

Never the less, that's what makes it work.


ORIGINAL CODE (my initial posting)
The first time I run the code it works. If I close the
Word-application and run the a second time, it doesn't work.

If i do NOT close the Word-app opened at first run, and run the code a
second time, it works.


ALTERED CODE (my reply myself)
Doing the change, it works.

As I stated, I don't get it, but would like to know. I can just
confirm that my alteration makes it work...


Regards /Snedker
 
M

Michael Bauer

I suppose "Dim As New" causes that a reference is hold in the
background. By calling Word.Application.Quit all references on that
instance become invalid.
 
M

Michael Bauer

... If I close the
Word-application and run the a second time, it doesn't work.

You had wrote, you do... In this case existing references are no longer
invalid.
 

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