Solved

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

Posted on 2013-06-19
12
367 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
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 
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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
VB6 Compile Compatibility Issue 4 102
WPF - Tooltips for ComboBox items 5 75
Passing a Text Box name to a Sub 6 96
MS Word Office 365 Mail Merge 2 62
Microsoft Word is a program we have all encountered at some point, but very few of us have dug deep into its full scope of features, let alone customized it to suit our needs. Luckily making the ribbon (aka toolbar, first introduced in Word 2007) wo…
This article describes how to use the Send to Mail Recipient command. The instructions apply generally to Office 2007 and later versions, but Microsoft® Word 2013 was used for the specific steps and figures.  What is Send to Mail Recipient? Send…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

831 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