Mr Doubtfire
10-07-2005, 05:21 PM
I have a simple VBA code as followed. Just do not why it skips to go "landscape" after the first time (which is successfully).
Please advice.:bow:
(Please be patient the following is just a simple example WITHOUT professional variable declaration/format, coding .... I am not a VB professional but am willing to learn)
Public objApp As Word.Application
Private Sub Command1_Click()
Dim intCounter1 As Integer
For intCounter1 = 1 To 2
On Error Resume Next
Set objApp = GetObject(, "Word.Application")
If objApp Is Nothing Then
Set objApp = CreateObject("Word.Application")
End If
With objApp
.Documents.Add , , , True
.Visible = False
End With
Landscape_format
With objApp
.Selection.Font.Bold = True
.Selection.ParagraphFormat.Alignment = wdAlignParagraphCenter
.Selection.Font.Size = 20
.Selection.TypeText Text:="testing - " & intCounter1
.Selection.TypeText Text:=vbCrLf
End With
objApp.ActiveDocument.SaveAs FileName:="c:\file" & intCounter1 & ".doc"
objApp.Quit False
Set objApp = Nothing
Next
End Sub
Function Landscape_format()
With ActiveDocument.PageSetup
.LineNumbering.Active = False
.Orientation = wdOrientLandscape
.TopMargin = CentimetersToPoints(1#)
.BottomMargin = CentimetersToPoints(1#)
.LeftMargin = CentimetersToPoints(2.01)
.RightMargin = CentimetersToPoints(2.01)
.Gutter = CentimetersToPoints(0)
.HeaderDistance = CentimetersToPoints(1.17)
.FooterDistance = CentimetersToPoints(1.01)
.PageWidth = CentimetersToPoints(27.94)
.PageHeight = CentimetersToPoints(21.59)
.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
.LayoutMode = wdLayoutModeDefault
End With
If ActiveWindow.ActivePane.View.Type = wdNormalView Or ActiveWindow. _
ActivePane.View.Type = wdOutlineView Then
ActiveWindow.ActivePane.View.Type = wdPrintView
End If
End Function
Please advice.:bow:
(Please be patient the following is just a simple example WITHOUT professional variable declaration/format, coding .... I am not a VB professional but am willing to learn)
Public objApp As Word.Application
Private Sub Command1_Click()
Dim intCounter1 As Integer
For intCounter1 = 1 To 2
On Error Resume Next
Set objApp = GetObject(, "Word.Application")
If objApp Is Nothing Then
Set objApp = CreateObject("Word.Application")
End If
With objApp
.Documents.Add , , , True
.Visible = False
End With
Landscape_format
With objApp
.Selection.Font.Bold = True
.Selection.ParagraphFormat.Alignment = wdAlignParagraphCenter
.Selection.Font.Size = 20
.Selection.TypeText Text:="testing - " & intCounter1
.Selection.TypeText Text:=vbCrLf
End With
objApp.ActiveDocument.SaveAs FileName:="c:\file" & intCounter1 & ".doc"
objApp.Quit False
Set objApp = Nothing
Next
End Sub
Function Landscape_format()
With ActiveDocument.PageSetup
.LineNumbering.Active = False
.Orientation = wdOrientLandscape
.TopMargin = CentimetersToPoints(1#)
.BottomMargin = CentimetersToPoints(1#)
.LeftMargin = CentimetersToPoints(2.01)
.RightMargin = CentimetersToPoints(2.01)
.Gutter = CentimetersToPoints(0)
.HeaderDistance = CentimetersToPoints(1.17)
.FooterDistance = CentimetersToPoints(1.01)
.PageWidth = CentimetersToPoints(27.94)
.PageHeight = CentimetersToPoints(21.59)
.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
.LayoutMode = wdLayoutModeDefault
End With
If ActiveWindow.ActivePane.View.Type = wdNormalView Or ActiveWindow. _
ActivePane.View.Type = wdOutlineView Then
ActiveWindow.ActivePane.View.Type = wdPrintView
End If
End Function