ActiveX issue

Lavina

Board Regular
Joined
Dec 18, 2018
Messages
75
Hello guys,

I want to write an excel macro that opens up sheets of word in directories per request and prints them out

I already have a full path created for each file, so all i really need to do is:

1) Open the word document with specific name
2) Print out only page 2, this i can achieve with something like:
objDoc.PrintOut

Current code:

Code:
  Dim objWord


  Dim objDoc


  Set objWord = CreateObject("Word.Application")


  Set objDoc = objWord.Documents.Open("C:\Documents\test.docx")


  objWord.Visible = True
This should create the object and open it up, but i get the runtime error 429, activex component can't create object.

My references contain: Microsoft DAO 3.6 Object Library and Microsoft ActiveX Data Objects 6.1 Library

What am i missing?

 

Some videos you may like

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).

Kamolga

Well-known Member
Joined
Jan 28, 2015
Messages
1,172
In your references, you need
Microsoft Word XXX Object library
(mine is 16.0, you might have a different number if not running last version of MS Office) to work with Word from Excel
 
Last edited:

James006

Well-known Member
Joined
Apr 4, 2009
Messages
3,680
Hello,

Once you have the Microsoft Word library ... you need to save and close ...

And then re-open ... since libraries are loaded when you open your file ...

Hope this will help
 

gallen

Well-known Member
Joined
Jun 27, 2011
Messages
1,996
IGNORE.. sorry just re-read your post
 
Last edited:

gallen

Well-known Member
Joined
Jun 27, 2011
Messages
1,996
I got it to work, including the printing out. Only time it failed was when I had mis-typed the address or the file was already open

Ensure the address and filename are 100% correct
 

gallen

Well-known Member
Joined
Jun 27, 2011
Messages
1,996
OK I struggled with it working constantly and had to use references. This code worked every time

Code:
Sub PrintDoc()
    '[COLOR=#ff0000][B]needs reference to microsoft Word Object library[/B][/COLOR]
    
    Dim objWord As Word.Application
    Dim objDoc As Word.Document
    Dim sPath As String, sFileName As String
  
    sPath = "C:\Test\" 'Change as required. Ensure to leave final backslash
    sFileName = "Test.docx" 'change as required
  
    Set objWord = New Word.Application
    
    On Error Resume Next
        'first check that it's not open
        Set objDoc = objWord.Documents(sFileName)
        'if not try to open it
        If objDoc Is Nothing Then
            Set objDoc = objWord.Documents.Open(Filename:=sPath & sFileName, ReadOnly:=True)
            If objDoc Is Nothing Then
                MsgBox "Could not open " & sPath & sFileName, vbCritical, "Error"
                Exit Sub
            End If
        End If
    On Error GoTo 0
    
    objDoc.PrintOut
    objDoc.Close
    objWord.Quit
    
    Set objDoc = Nothing
    Set objWord = Nothing
    
End Sub
 

Watch MrExcel Video

Forum statistics

Threads
1,099,803
Messages
5,470,871
Members
406,733
Latest member
darzu

This Week's Hot Topics

Top