Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Documents.Add Does Not Return Document Object

Posted on 2009-05-12
1
Medium Priority
?
751 Views
Last Modified: 2012-05-06
We have a macro that creates a new document from a template and then "does stuff" with the resulting document. This used to work fine.

Today, without any apparent cause, the code throws an error. Stepping through the code, I can see that everything works fine, up until the point where I accept the returned object from Documents.Add into a Document variable and try to use that variable (see code). The document is created perfectly, the frmLetter form displays and is processed properly, and all the code in frmLetter runs without error. The end result is an open Letter document, processed like we expect Letter.dot to do.

It's only after the code returns from the call to Documents.Add that the weirdness starts. Apparently, the Documents.Add call does not return an object here.

Any ideas on what might cause this weird behaviour?
'Macro in standard module:
Public Sub CreateLetter()
   Dim doc As Word.Document
   Set doc = Application.Documents.Add(strWorkgroup & "Letter.dot")
   ' "Do stuff" with the resulting document:
   Call MsgBox(doc.Name) '--> RTE: Object variable or with block variable not Set
                         '    and stepping through the code, I can see that
                         '    the doc variable is indeed Nothing
End Sub
 
'Event in Letter.dot:
Private Sub Document_New()
   Dim f As frmLetter
   Set f = New frmLetter
   With f
      Set .CurrentDocument = ActiveDocument
      .Show
      'After cmdOK_Click, frmLetter processes its CurrentDocument correctly
   End With
   Set f = Nothing
End Sub

Open in new window

0
Comment
Question by:FlorisMK
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 2

Accepted Solution

by:
FlorisMK earned 0 total points
ID: 24364159
Solved:

One of the things that cmdOK_Click does is open, read, and close our central repository template (which contains our house style Styles and AutoText entries). Apparently, opening another document from within the Documents.Add runtime scope breaks the reference to the newly created document.

Working with ActiveDocument instead of using the return object from Add solves the issue. I don't like the solution, because with taskbar buttons and fidgety users you can't always rely on ActiveDocument, but it's the best I can do in this situation.

Edited the code to read:


'Macro in standard module:
Public Sub CreateLetter()
   Dim doc As Word.Document
   Call Application.Documents.Add(strWorkgroup & "Letter.dot")
   ' "Do stuff" with the resulting document:
   Set doc = ActiveDocument
   Call MsgBox(doc.Name) '--> NO RTE, Doc is set
End Sub

Open in new window

0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This is written from a 'VBA for MS Word' perspective, but I am sure it applies to most other MS Office components where VBA is used.  One thing that really bugs me is slow code, ESPECIALLY when it's mine!  In programming there are so many ways to…
Preface: When I started this series, I used the term CommandBars because that is the Office Object class that it discusses. Unfortunately, when Microsoft introduced Office 2007, they replaced the standard Commandbar menus with "The Ribbon" and rem…
In this video, we show how to convert an image-only PDF file into a PDF Searchable Image file, that is, a file with both the image (typically from scanning) and text, which is created in an automated fashion with Optical Character Recognition (OCR) …
Learn how to make your own table of contents in Microsoft Word using paragraph styles and the automatic table of contents tool. We'll be using the paragraph styles in Word’s Home toolbar to help you create a table of contents. Type out your initial …

715 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question