setting word portriat landscape from msacess oApp run time error 91

Am tryign to take control of the word object model and am playing with the code to open a doc and then change it ot portrait ot landscape using the below code
but when it hit the ... With ActiveDocument.Range(Start:=Selection.Start, End:=ActiveDocument. _
 line it throws up a run-time error 91 not set, i have not set the object access correctly and have stacks of web sites open to see how this is done and lots of examples. Just cannot see wht i am doing wrong
thanks for any help

Function PlayWord()
Dim oApp As Object
Dim wdDoc As Word.Document
Dim LWordDoc As String
Dim oRng As Word.Range
Set oApp = CreateObject(Class:="Word.Application")
LWordDoc = "C:\eagle\AssetDir\MainBook.doc"
 If Dir(LWordDoc) = "" Then
        MsgBox "Document not found."

     Else
        'Create an instance of MS Word
       
        oApp.Visible = True

        'Open the Document
        oApp.Documents.Open FileName:=LWordDoc
     End If
     With ActiveDocument.Range(Start:=Selection.Start, End:=ActiveDocument. _
        Content.End).PageSetup
        .LineNumbering.Active = False
        .Orientation = wdOrientPortrait
        .TopMargin = InchesToPoints(0.25)
        .BottomMargin = InchesToPoints(0.25)
        .LeftMargin = InchesToPoints(0.25)
        .RightMargin = InchesToPoints(0.25)
        .Gutter = InchesToPoints(0)
        .HeaderDistance = InchesToPoints(0.5)
        .FooterDistance = InchesToPoints(0.5)
        .PageWidth = InchesToPoints(8.5)
        .PageHeight = InchesToPoints(11)
        .FirstPageTray = wdPrinterDefaultBin
        .OtherPagesTray = wdPrinterDefaultBin
        .SectionStart = wdSectionNewPage
        .OddAndEvenPagesHeaderFooter = False
        .DifferentFirstPageHeaderFooter = False
        .VerticalAlignment = wdAlignVerticalTop
        .SuppressEndnotes = False
        .MirrorMargins = False
        .TwoPagesOnOne = False
        .BookFoldPrinting = False
        .BookFoldRevPrinting = False
        .BookFoldPrintingSheets = 1
        .GutterPos = wdGutterPosLeft
    End With
 End Function
sydneyguyAsked:
Who is Participating?
 
lee555J5Connect With a Mentor Commented:
I tested your code with an exact copy/paste (except for the filename) and did not get your error. The function worked perfectly. I am running Access and Word 2007. Until I added the reference to the Microsoft Word 12.0 Object Library, it failed at the line
Dim wdDoc As Word.Document
In Access, I have 4 references checked by default:
Visual Basic for Applications
Microsoft Access 12.0 Object Library
OLE Automation
Microsoft Office 12.0 Access database engine Library
plus the additional one in the first paragraph. So, check your references.
Lee

0
 
sydneyguyAuthor Commented:
the same code crashes stilla t the same point
i have as references and in this order and it will not work for me the above code

Visual Basic for Applications
microsoft access 11.0 object library
Microsoft DAO 3.6 Object Library
OLE Automation
Microsoft Visual basic for Applictions Extensibility 5.1
Microsoft ActiveX Data Objects 2.1 Library
Microsoft Office 11.0 Object Library
Microsoft windows common controls 6.0(sp6)
Microsoft word 11.00 Object Library
0
 
lee555J5Commented:
I have an XP box at work with Office 2003 (Office 11). I will test tomorrow.
I only needed the 5 references I mentioned above for the pasted code to work. I left the 4 defaults.
Is it possible you copied code from someone posting code from Office 2007? As I said, the code worked on my Office 2007 installation.
Lee
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
sydneyguyAuthor Commented:
no the code for openiong the file, is from my normal code running the extra open the code was macro recorded in 2003 on a vist machine, i am now my self trying on a XP machine to see what it does. good thinking though.
word object model is a bit of a pain to understand and finding some good data on how it all hangs togethere takes a little bit of time to fighure out
0
 
sydneyguyAuthor Commented:
just tried this code on another xp machine and it worked ok but will not run on the vist machine
will check the same code on the vist machine
0
 
sydneyguyAuthor Commented:
the identical cose that works on the xp machine throws up a run-time error 91
will create a new DB and load it in there with min info and ref and try it again
0
 
lee555J5Commented:
I will still try tomorrow and report.
Lee
0
 
sydneyguyAuthor Commented:
thanks lee most appreciated
0
 
GrahamSkanConnect With a Mentor RetiredCommented:
The Activedocument is unqualified (should be oApp.ActiveDocument). Try explicitly capturing the document object and using it in the setup.

Also, there is no need to use the Selection object which is also undefined or even to specify the range.

        oApp.Documents.Open FileName:=LWordDoc
     End If
     With ActiveDocument.Range(Start:=Selection.Start, End:=ActiveDocument. _
        Content.End).PageSetup
 '...
        'Open the Document
        Set wdDoc = oApp.Documents.Open(FileName:=LWordDoc)
     End If
     With wdDoc.PageSetup
        .LineNumbering.Active = False
        .Orientation = wdOrientPortrait
'...

Open in new window

0
 
sydneyguyAuthor Commented:
thanks for all your input tried on 3 other machines, and it worked, in desperation i turned the vista machine  unplugged the power supply and took out the battery and restarted it.......
Guess what it now works as it should so it was just some stupid hang that was held inside the memory so till it happens again i will close this question and be mindful of this problem but now i know what it  is i will probably never see it again.
Such is life sorry for the run around for not a real problem.
But i did have some code that was working and i made some changes then the problem started but i had not changed the code that was then failing. so down a bit of a rabbit hole. sorry for that
but thanks for every ones input
Garry
0
 
sydneyguyAuthor Commented:
Graham yes your are right and i am aware and it made sense for the oApp but none of it was working this is what did not make sense any way we will move on and pretend it never happened
0
All Courses

From novice to tech pro — start learning today.