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

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.
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:
Upvote 0
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
 
Upvote 0
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
 
Upvote 0
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
 
Upvote 0

Forum statistics

Threads
1,213,567
Messages
6,114,342
Members
448,570
Latest member
rik81h

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
Back
Top