Solved

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

Posted on 2013-06-19
12
364 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
 
LVL 59

Expert Comment

by:Chris Bottomley
Comment Utility
Are you sure your VM has word installed?

Chris
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

by:XCalRocketMan
Comment Utility
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
Comment Utility
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
Comment Utility
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 45

Expert Comment

by:Martin Liss
Comment Utility
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
Comment Utility
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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
This Micro Tutorial well show you how to find and replace special characters in Microsoft Word. This is similar to carriage returns to convert columns of values from Microsoft Excel into comma separated lists.
In a previous video Micro Tutorial here at Experts Exchange (http://www.experts-exchange.com/videos/1358/How-to-get-a-free-trial-of-Office-365-with-the-Office-2016-desktop-applications.html), I explained how to get a free, one-month trial of Office …

772 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now