PDA

View Full Version : Solved: Landscape printing help



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

fumei
10-07-2005, 07:07 PM
1. Functions really should return a singular value. Why do you have this as a Function, rather than a Sub?

2. It is a good idea to strip off things you do not need. Do you really need all those items - eg. MirrorMargins = False? If you do not....get rid of them.

3. What do you mean it "skips to go "landscape" after the first time (which is successfully)." This is not a very good description - I have no idea what it means. How many times? What makes it more than one - are you just clicking away at the command button? What in fact are you doing?

4. I ran this from Excel...and it made two files - both landscape. Take a look at where you making application object Nothing.

Public objApp As Word.Application
Private Sub MakeTwoFiles()
Dim intCounter1 As Integer
If objApp Is Nothing Then
Set objApp = CreateObject("Word.Application")
End If
For intCounter1 = 1 To 2
On Error Resume Next
With objApp
.Documents.Add , , , True
.Visible = False
End With
' this replaces the function
' as the function is not needed to do this
objApp.ActiveDocument.PageSetup.Orientation _
= wdOrientLandscape

' note the use of With statements
With objApp.Selection
With .Font
.Bold = True
.Size = 20
End With
.ParagraphFormat.Alignment = wdAlignParagraphCenter
.TypeText Text:="testing - " & intCounter1 & vbCrLf
End With
objApp.ActiveDocument.SaveAs Filename:="c:\file" & _
intCounter1 & ".doc"
Next
objApp.Quit True
Set objApp = Nothing
End Sub

Also please note the use of the underscore character in the code. This helps break up the lines so that code in the VBA code window here is not stretched out. It is a pain to scroll left and right, as well as up and down.

Using the underscore character is also helpful in your own code windows!

Mr Doubtfire
10-07-2005, 07:20 PM
Thank you for the prompt reply.
1. Agree totally with YOU.
2. I learn from this site to create macro and that is the code I got when doing "page setup"
3. "Skip" means the first time it went "totally" (found when debugging), so the result of the first file "file1" is landscaped BUT on the second time it did go into the "function" (now it should be "sub") and jump out at once without going down further, and the final result format is portraited..
4. I extracted the code from VB6.

Please advice.
Thanks again.

fumei
10-08-2005, 06:57 AM
Well first of all, try what I did, by NOT using the function, just use the instruction to make the active document landscape.

Second of all, try what I did, look at what you are doing with making the objApp = nothing. You are making it Nothing....then in your loop checking to see if it is nothing. Well....it will always be nothing, because you are making it nothing in the loop.

Thirdly, WHERE are you running this from?

Fourthly, what exactly are you trying to do, and why?

Mr Doubtfire
10-08-2005, 08:18 AM
Thank you again for the instructions.
First, I removed the sub and simplify it for the landscape format. (it works the same as before with the first one successfully formatted ONLY)
Second, the "nothing" has to be there otherwise it will not create the second, third. file. (but when it is there the second will get portrait formatted)
Third, I am running this from VB6 in a simple form with a simple command (for testing)
Fourth, I am trying to create Word documents under Landscape format with NO header and NO footer.
(This is just a simple example/demonstration)
Awaiting for your new instructions.


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

With ActiveDocument.PageSetup
.LineNumbering.Active = False
.Orientation = wdOrientLandscape
End With

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
MsgBox "done"
End Sub

fumei
10-08-2005, 08:49 PM
Your statement about the Nothing having to be there, otherwise it will not create the second file is absolutely incorrect. Wrong, actually. It has - well - nothing to do with it.

If you notice, my code has objApp = Nothing at the very very end. It is NOT in the loop. The loop creates the files. The objApp needs only to be created the once. In fact, it is precisely why you are having problems. You only need the one instance of Word. That instance can make the two files. It is bad programming to make more instances than required.

My code DOES make the two files - both landscape by the way.

Mr Doubtfire
10-09-2005, 04:00 PM
I got it this time as instructed.
What else I could say.:bow:
Thanks!

fumei
10-11-2005, 03:07 AM
Excellent. That means you likely learned something. Can't ask for more than that.

Keep posting!