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
Solved

VBA Documents.Add returns 'nothing' when executed outside the debugger

Posted on 2013-06-19
12
368 Views
Last Modified: 2013-07-15
I have a problem when trying to open a Word document via VBA code within Excel. The office environment is hosted as a VM (Office 2010 running on a virtual Windows 7 environment)

I've reduced the code to bare minimum to illustrate the problem. Code follows:

Dim NewDoc as Word.Document
Dim wdApp as Word.Application

set wdApp = New Word.Application
set NewDoc = wdApp.Documents.Add
wdApp.ActiveDocument.SaveAs "New.docx"
wdApp.ActiveDocument.Close
wdApp.Quit

...  clean up 

Open in new window


Executing the code via Excel VBA (not shown here) crashes with error 4248 "This command is not available because no document is open."

The line that is attempting to save the document is failing. Entering the debugger and inspecting NewDoc reveals that 'NewDoc = Nothing'.

Placing a breakpoint on the Documents.Add line and executing the code within the debugger, I can see that NewDoc is initialized to "Document1". Running the remaining code within the debugger correctly saves a Word doc named 'New.docx'!

If I port the code to a 'thick client' and run it locally it works fine. So, the problem is related to running within the VM.

Got me baffled :)    Any ideas out there?
0
Comment
Question by:XCalRocketMan
  • 5
  • 3
12 Comments
 
LVL 59

Accepted Solution

by:
Chris Bottomley earned 500 total points
ID: 39261491
Try replacing


Dim NewDoc as Word.Document
Dim wdApp as Word.Application

set wdApp = New Word.Application

with


Dim NewDoc as object
Dim wdApp as object

set wdApp = createobject("Word.Application")
0
 
LVL 59

Expert Comment

by:Chris Bottomley
ID: 39262035
Note

The underlying assumption to my suggestion is that in the VM the libraries may not be available / accessible and that switching to late binding as in the suggestion will circumvent that possible cause making it worth trying.

Chris
0
 

Author Comment

by:XCalRocketMan
ID: 39262235
I had thought it might be a binding issue, and although I thought I tried this before I recoded this morning and ... now I get another error.  This time the complaint is on the createobject call with error # 429: ActiveX component can't create object.
0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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.

 
LVL 59

Expert Comment

by:Chris Bottomley
ID: 39262274
Are you sure your VM has word installed?

Chris
0
 

Author Comment

by:XCalRocketMan
ID: 39262353
Yep.  We are using a 'thin client' setup here at work. All our apps are hosted on the VM.  The Office Suite, and a ton of others. The binding issue sounded like a winner - remember I can run the code just fine from within the debugger - the original code I posted works. But outside the debug session, no joy.
0
 
LVL 59

Expert Comment

by:Chris Bottomley
ID: 39262454
I do not work with thin / thick clients so cannot be sure here but on the client try:

Re-registering Word:
    In the run dialog box type:
         C:\Program Files\Microsoft Office\Office\Word.exe /regserver
         Press OK

Chris
0
 

Author Comment

by:XCalRocketMan
ID: 39266567
Hi all,

Looks like this issue might be related to a Security Posture set by the VM administrators. Thanks for all the insight provided. I'll need to bark up another tree to get this resolved :)

Mike
0
 
LVL 46

Expert Comment

by:Martin Liss
ID: 39326645
I've requested that this question be deleted for the following reason:

Not enough information to confirm an answer.
0
 
LVL 59

Expert Comment

by:Chris Bottomley
ID: 39326646
Original request was:

'Executing the code via Excel VBA (not shown here) crashes with error 4248 "This command is not available because no document is open."'

in POST 39262235 this seemed to resolve the specific issue as the error changed to "error # 429: ActiveX component can't create object"

In post 39266567 it was stated that this latter error was caused by "Security Posture set by the VM administrators".

I am at a loss as to why there is insufficient data available and suggest the post at 39261491 was a valid solution for the question even though the asker thought they had tried it earlier as the error changed to a different install specific at that time.

Chris
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

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…
If you haven’t already, I encourage you to read the first article (http://www.experts-exchange.com/articles/18680/An-Introduction-to-R-Programming-and-R-Studio.html) in my series to gain a basic foundation of R and R Studio.  You will also find the …
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 …
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

861 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