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!
(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!