Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Object variable or with block variable not set (Error 91)

Status
Not open for further replies.

KatyWhite

Technical User
Jan 15, 2004
16
US
Hi all, I have a VB 6.0 program that I wrote that does a few different things with a word document. Everything works so far, however I am having an issue when I open a word document then try and run a macro in the document.
(My VB isn't the greatest)

This is the code in the sub that opens the word document from VB 6 (this part works fine, the document opens etc):

Private Sub cmdRunFormat_Click()
On Error Resume Next
Set wdApp = CreateObject("Word.Application")
If Err.Number Then
Set wdApp = CreateObject("Word.Application")
Else
blnAlreadyOpen = True
End If
'open the document
wdApp.Documents.Open (strCompile)
wdApp.Visible = True

wdApp.Run ("RunMacro")

wdApp.Documents.Save (strCompile)
wdApp.Quit
Word.Application.Quit
Set wdApp = Nothing
End Sub

The word macro works fine when it is run in word. When the Macro is called from my VB6 program, I get error 91 at this point:

If optA.Value = True Then 'SAS output Portrait layout

Selection.WholeStory

If ActiveWindow.View.SplitSpecial = wdPaneNone Then
ActiveWindow.ActivePane.View.Type = wdPrintView
Else
ActiveWindow.View.Type = wdPrintView
End If

'wdApp.Activate
With ActiveDocument.PageSetup
---->>>> .LineNumbering.Active = False
.Orientation = wdOrientPortrait
.TopMargin = InchesToPoints(0.4)
.BottomMargin = InchesToPoints(0.4)
.LeftMargin = InchesToPoints(0.5)
.RightMargin = InchesToPoints(0.5)
.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
.GutterPos = wdGutterPosLeft
End With

With Selection.Font
.Name = "Courier New"
.Size = 8
.Bold = False
.Italic = False
.Underline = wdUnderlineNone
.StrikeThrough = False
.Color = wdColorAutomatic
.Spacing = 0
.Scaling = 100
.Position = 0
End With

Selection.HomeKey Unit:=wdStory

'Unload Me
ElseIf optB.Value = True Then 'production output 7.5pt(etc)

Can anyone offer suggestions as to why this is happening? Thanks very much!


 
Could you please state what exactly you are tryiong to do. It appears you are selecting the entire document and doing a Page Setup, and font states - on the whole document.

Is this correct?

Gerry
 
Yes that is exactly what I am doing in the macro. There are 4 option buttons that the user can choose from; each is the same but for the formatting. I just copied the first one.
 
Too much code. If that is all you are doing, there is no need to select anything, least of all the entire document.

Code:
With ActiveDocument.Content.Font
    .Name = "Courier New"
    .Size = 8
        ' and anything else you need
        ' if you don't need it, don't use it
        ' but make sure the defaults are good for you
End With
With ActiveDocument.PageSetup
    .Orientation = wdOrientPortrait
    .TopMargin = InchesToPoints(0.4)
    .BottomMargin = InchesToPoints(0.4)
    .LeftMargin = InchesToPoints(0.5)
    .RightMargin = InchesToPoints(0.5)
        ' and anything else you need
        ' if you don't need it, don't use it
        ' but make sure the defaults are good for you
End With

Now, are you saying you are getting a 91 error at the LineNumbering? Odd if you are. If you are not making it True, get rid of that line, you don't need it. The defualt is false.

Gerry
 
Hi Gerry,

I have tried commenting out some parts of the formatting but I get the error on the next line of code. I can run this macro from the word document, I only have an issue with it when trying to run the macro from VB. It is odd though.

Thanks, Katy
 
Is it just the PageSetup portion that fails, or any portion?
Are all your references up to date?
What version of Word?


Gerry
 
I have a reference to Word in VB, the other parts of my code work, just not where it calls the macro from the word document.

Sometimes the document that I am formatting is in RTF format rather than a doc - don't know if that makes a difference. When I run the macro from word it runs fine.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top