Opening Word from Excel using VBA – Several errors - excel

I am currently trying to open Microsoft Word from Microsoft Excel using VBA. I found these resources online:
Official Microsoft Tutorial
Microsoft Forum
Based on them, I came up with the following code:
Code 1
Dim wordApp As Word.Application
Set wordApp = CreateObject("Word.Application")
Running the code with Word already open
Excel stops responding.
Running the code with Word closed
Word opens, but Excel gives me the error Out of memory or Automation error and wordApp is not defined.
Code 2
Dim wordApp As Word.Application
Set wordApp = GetObject(,"Word.Application")
Running the code with Word already open
Excel throws the error Out of memory and wordApp is not defined.
Running the code with Word closed
Excel throws the error ActiveX component can't create object or return reference to this object and wordApp is not defined.
System information
OS: macOS Mojave (10.14.6)
Excel and Word Version: 16.36
References in Excel:
Visual Basic For Applications, Microsoft Excel 16.0 Object Library, Microsoft Forms 2.0 Object Library, Microsoft Office 16.0 Object Library, Microsoft Word 16.0 Object Library, OLE Automation
Conclusion
These errors are quite confusing and I could not fix them by reading Microsoft's error pages. For instance, I continuously get the error Out of memory (see above), although I have around 10 GB of free RAM space. How can I get this to work both on my machine (see above) as well as any other machine reliably?

Combining your both approaches in only one will look like this, and it shouldn't return any error.
If the error still persists, it is not connected to the code itself...
Only reference to Microsoft Word 16.0 Object Library necessary.
Sub openWordApp()
Dim wordApp As Word.Application
On Error Resume Next
Set wordApp = GetObject(, "Word.Application")
If Err.Number <> 0 Then
Err.Clear: On Error GoTo 0
Set wordApp = CreateObject("Word.Application")
End If
On Error GoTo 0
wordApp.Visible = True
Debug.Print wordApp.Documents.count
End Sub
Please, also check if not multiple Word sessions are open... Maybe you checked the code many times with Word session Visible = False and there still are a lot of such sessions open. Just maybe, but it would be good to check it.

Related

Task Scheduler failed to run the Excel VBA Macro

I created an Excel VBA that check for data in the cells and send email with WorkBook_Open().
Option Explicit
Private Sub Workbook_Open()
'Declaring variables
Dim notifyEmailApplication As Object
Dim notifyEmailContent As Object
Dim triggerEmailApplication As Object
Dim triggerEmailContent As Object
'Create email object
Set notifyEmailApplication = CreateObject("Outlook.Application")
Set notifyEmailContent = notifyEmailApplication.CreateItem(0)
Set triggerEmailApplication = CreateObject("Outlook.Application")
Set triggerEmailContent = triggerEmailApplication.CreateItem(0)
...
I then created a VBScript to run the Excel file.
Call ExcelMacro
Sub ExcelMacro()
Set xlApp = CreateObject("Excel.Application")
xlApp.Visible = True
xlApp.DisplayAlerts = False
Set xlBook = xlApp.Workbooks.Open("....\Email Automation.xlsm", 0, False)
xlBook.Close
Set xlBook = Nothing
xlApp.Quit
Set xlApp = Nothing
End Sub
I also created a cmd file to run the VBScript on cscript.exe
cscript.exe "....\vbscript.vbs"
exit
Whenever I trigger the cmd file manually (double clicking it), the Excel Macro runs perfectly and successfully send email to the designated person.
But when I use Task Scheduler to run the cmd file, the Excel Macro does not run successfully and this line was highlighted.
Set notifyEmailApplication = CreateObject("Outlook.Application")
Notes: I already viewed a lot of forums and didn't find a fix:
In 'dcomcnfg' I already set Outlook Message Attachment to Interactive User
I tried changing Dim notifyEmailApplication As Object to Dim notifyEmailApplication As Outlook.Application, same line is highlighted
I already added Outlook Object Library as reference in Excel VBA
But when I use Task Scheduler to run the cmd file, the Excel Macro does not run successfully
Microsoft does not currently recommend, and does not support, Automation of Microsoft Office applications from any unattended, non-interactive client application or component (including ASP, ASP.NET, DCOM, and NT Services), because Office may exhibit unstable behavior and/or deadlock when Office is run in this environment.
If you are building a solution that runs in a server-side context, you should try to use components that have been made safe for unattended execution. Or, you should try to find alternatives that allow at least part of the code to run client-side. If you use an Office application from a server-side solution, the application will lack many of the necessary capabilities to run successfully. Additionally, you will be taking risks with the stability of your overall solution.
Read more about that in the Considerations for server-side Automation of Office article.
If you deal with open XML documents you may consider using the Open XML SDK instead, see Welcome to the Open XML SDK 2.5 for Office for more information. Also take a look at any third-party components designed for the server-side execution (they don't require Office installed on the system).
I haven't touched this topic for many years, but as I remember, from a long time ago, the Task Scheduler allows, or requires, you to enter your Windows password and if the password is incorrect it won't notify you of the error, so it seems like it's working, but with the incorrect password, it won't work. Can you double-check that your Windows password is entered correctly?

Can't create Scripting.Dictionary or MSXML.DOMDocument60 in VBA since Windows Update KB4586781

Some VBA code no longer works for me, when creating COM objects from standard libraries such as Microsoft Scripting Runtime or Microsoft Xml, v6.0. I get an error on the line of instantiation, the error is code
80004021(-2147467231) and error description is Automation error The operation attempted is not supported. But I can create C# code with interop references to these libraries perfectly fine. I had an Windows 10 update KB4586781 two days ago.
The code fails in both Excel VBA and Word VBA. Anyone else with similar issues?
Sub TestCreate_Scripting_Dictionary()
'* THIS CODE FAILS !!!!
Dim lateBound As Object
Set lateBound = VBA.CreateObject("Scripting.Dictionary") '<=== FAILS 80004021
' ... Automation error The operation attempted is not supported.
'* Tools->References->Microsoft Scripting Runtime
Dim dict As Scripting.Dictionary
Set dict = New Scripting.Dictionary '<=== FAILS 80004021
' ... Automation error The operation attempted is not supported.
End Sub
Sub TestCreate_MSXML2_DOMDocument60()
'* THIS CODE FAILS !!!!
'* Tools->References->Microsoft Xml, v6.0
Dim xml As MSXML2.DOMDocument60
Set xml = New MSXML2.DOMDocument60 '<=== FAILS 80004021
' ... Automation error The operation attempted is not supported.
End Sub
UPDATE: I was meaning to reinstall but it went away without any intervention. Odd.

VBA Dim Object error

If I Run
Sub test()
Dim Template_Excel_Instance As excel.application
Set Template_Excel_Instance = CreateObject("excel.application")
End Sub
my code breaks with an error "Automation Error, Library not registered"
If I run
Sub test()
Dim Template_Excel_Instance As object
Set Template_Excel_Instance = CreateObject("excel.application")
End Sub
It runs fine. Is there any way to fix this? Reason I ask is that this issue only affects one PC, despite having the same references as all other PCs. The first error is not coming up anywhere else
Does that PC have a different version of Excel?
The problem is with As Excel.Application. If you don't have the appropriate reference defined, then the VBA compiler will not recognise the type. Yes, VBA does have a compilation step. If you do have the reference defined, then this is sensitive to the application version (just the major part of the application version I think), so is therefore inherently non-portable.
In your latter example, you are using late binding, so only COM object registration is required, not any specific library to be added to your project. For portability, this is the way to go.
Firstly, have you tried repairing the install?
Secondly, is there a reason you are instantiating a second instance of Excel?
Thirdly, let's get a grip on what is happening, please run this code and report back.
Option Explicit
Sub TestWhatVersionDoesCreateObjectReturn()
Dim obj As Object
Set obj = CreateObject("Excel.Application")
Debug.Print obj.Version
End Sub
Sub TestWhatOtherVersionsAreCreatable()
Dim lLoop As Long
For lLoop = 7 To 16
Dim obj As Object
Set obj = Nothing
On Error Resume Next '///set break on unhadnled errors
Set obj = CreateObject("Excel.Application." & CStr(lLoop))
On Error GoTo 0
If Not obj Is Nothing Then Debug.Print obj.Version
Next
End Sub
Also, look for multiple installations of Excel. The code at this blog page will read your registry and write the results to an Excel worksheet. You say you have repaired the install but there might be other Excel installations interfering. The code in that blog will highlight multiple Excel installs.
Ok, from your feedback you say all the Excel libraries version return 14, implying no multi version installs. Hmmm, we ought to consider that actually the library that error is complaining about isn't Excel but a dependency.
Other possible leads
Social MSDN - automation error. Library not registered - solved by cleaning registry of legacy "Microsoft.Office.Interop.Excel" keys

GetObject Function in VB 6 is throwing the Error number 429 in Windows 7

I am exporting the data from my application to excel. For this i am checking whether any excel windows are open and then creating the excel application object. Like as mentioned below.
lHwnd = FindWindow("XLMain", vbNullString)
If lHwnd = 0 Then
\\create new application
Set GetExcelApp = GetObject("", "Excel.Application")
Else
\\get existing application
Set GetExcelApp = GetObject(, "Excel.Application")
End If
But if any excel windows are already open then this code is throwing the error 429.In Windows Xp this code is working fine for all scenarios but in Windows 7 its throwing the Error 429 while running the exe.
How can i solve this. Please advice..
Error 429 - ActiveX component can't create object means that the Excel.Application object is not registered on your Windows 7 machines. This could be because Excel is not installed, or improperly installed. I would go to the Control Panel=>Programs and Features, select Microsoft Excel xx.0 or Office xx.0 and select Change. Select the Repair option.

Vbs on Excel Starter 2010: activex component can't create object Excel.Application

I have a new pc with Windows 7 and Office Starter 2010 pre-installed. If I launch the following simple vb script (from command line: cscript testScript.vbs):
Dim xlApp
Set xlApp = CreateObject("Excel.application")
xlApp.visible = True
Set xlWorkbooks = xlApp.Workbooks
Set xlWorkbook = xlWorkbooks.Open("C:\path\myFile.xls")
xlWorkbook.ActiveSheet.Rows("1:2").AutoFit
xlApp.visible = False
xlWorkbook.Save
xlWorkbook.Close("C:\path\myFile.xls")
xlApp.Quit
Set xlApp = Nothing
it returns this error: activex component can't create object 'Excel.Application'.
I don't understand if the error is due to Starter limitations (http://office.microsoft.com/en-us/starter-help/excel-features-that-are-not-fully-supported-in-excel-starter-HA010374501.aspx), and I found dissenting opinions on the web.
Is there a way to make it works using Office Starter version?
Despite this is already very aged question, I haved decided to post the following information only to help other googlers.
Since you are trying to create an instance of Excel.Application outside VBA, there's a good chance of being succeeded if you install an updated version of Microsoft Excel Viewer on target machine. This will allow you to access Excel's API.
Bear in mind, that Excel Starter Edition does not support macros, along with other important limitations.
Cheers!

Resources