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?

 

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type

Kamolga

Well-known Member
Joined
Jan 28, 2015
Messages
1,176
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
2,006

ADVERTISEMENT

IGNORE.. sorry just re-read your post
 
Last edited:

gallen

Well-known Member
Joined
Jun 27, 2011
Messages
2,006
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
2,006
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,130,021
Messages
5,639,584
Members
417,100
Latest member
Simon123456789

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Top