PDA

View Full Version : Solved: Can't delete button - Syntax Help



johnske
10-07-2004, 08:16 PM
Hi, I have a favourite page setup for Word docs that I want to use MOST of the time and I want new docs opened in the normal template to setup the pages using this setup if I choose it.

I'm not too sure exactly what I'll end up with, but I have the following inserted in the Normal template for "ThisDocument" for now. The problem is that I'm not sure of the syntax for deleting the button after use..... :bink:

Public MyObj As Object
Sub CreateCommandButton()
Set MyCtrl = ActiveDocument.Shapes _
.AddOLEControl(ClassType:="Forms.CommandButton.1")
With MyCtrl.OLEFormat
.Activate
Set MyObj = .Object
End With
With MyObj
.Caption = "Page Setup"
.AutoSize = True
End With
End Sub
Private Sub CommandButton1_Click()
SetupPage
End Sub
Sub SetupPage()
With ActiveDocument.PageSetup
.LineNumbering.Active = False
.Orientation = wdOrientPortrait
.TopMargin = InchesToPoints(0.6)
.BottomMargin = InchesToPoints(0.97)
.LeftMargin = InchesToPoints(0.6)
.RightMargin = InchesToPoints(0.6)
.Gutter = InchesToPoints(0)
.HeaderDistance = InchesToPoints(0.5)
.FooterDistance = InchesToPoints(0.5)
.PageWidth = InchesToPoints(8.27)
.PageHeight = InchesToPoints(11.69)
.FirstPageTray = wdPrinterDefaultBin
.OtherPagesTray = wdPrinterDefaultBin
.SectionStart = wdSectionNewPage
.OddAndEvenPagesHeaderFooter = False
.DifferentFirstPageHeaderFooter = False
.VerticalAlignment = wdAlignVerticalTop
.SuppressEndnotes = False
.MirrorMargins = False
.TwoPagesOnOne = False
.GutterPos = wdGutterPosLeft
End With
ActiveWindow.ActivePane.View.Zoom.PageFit = wdPageFitBestFit
Selection.Font.Name = "Verdana"
Shapes(MyObj).Select
Selection.Cut
End Sub

johnske
10-07-2004, 09:57 PM
(This is probably a better way to do the job, but the original question is only avoided - not answered - and I may need to do something similar to the original prob at another time) :bink:

Sub Document_Open()
Dim Answer As VbMsgBoxResult
Answer = MsgBox("Your preferred page setup?", vbYesNo)
If Answer = vbNo Then End
Application.ScreenUpdating = False
CustomizationContext = NormalTemplate
With ActiveDocument.PageSetup
.LineNumbering.Active = False
.Orientation = wdOrientPortrait
.TopMargin = InchesToPoints(0.6)
.BottomMargin = InchesToPoints(0.97)
.LeftMargin = InchesToPoints(0.6)
.RightMargin = InchesToPoints(0.6)
.Gutter = InchesToPoints(0)
.HeaderDistance = InchesToPoints(0.5)
.FooterDistance = InchesToPoints(0.5)
.PageWidth = InchesToPoints(8.27)
.PageHeight = InchesToPoints(11.69)
.FirstPageTray = wdPrinterDefaultBin
.OtherPagesTray = wdPrinterDefaultBin
.SectionStart = wdSectionNewPage
.OddAndEvenPagesHeaderFooter = False
.DifferentFirstPageHeaderFooter = False
.VerticalAlignment = wdAlignVerticalTop
.SuppressEndnotes = False
.MirrorMargins = False
.TwoPagesOnOne = False
.GutterPos = wdGutterPosLeft
End With
Application.ScreenUpdating = True
ActiveWindow.ActivePane.View.Zoom.PageFit = wdPageFitBestFit
Selection.Font.Name = "Verdana"
End Sub

Kieran
10-07-2004, 10:11 PM
Try this


Sub CreateCommandButton()
Dim MyCtrl As Shape
Set MyCtrl = ActiveDocument.Shapes.AddOLEControl _
(ClassType:="Forms.CommandButton.1")
With MyCtrl.OLEFormat.Object
.Caption = "Page Setup"
.AutoSize = True
End With
End Sub
Private Sub CommandButton1_Click()
SetupPage
End Sub
Sub SetupPage()
ActiveDocument.Shapes(1).Delete
With ActiveDocument.PageSetup
.LineNumbering.Active = False
.Orientation = wdOrientPortrait
.TopMargin = InchesToPoints(0.6)
.BottomMargin = InchesToPoints(0.97)
.LeftMargin = InchesToPoints(0.6)
.RightMargin = InchesToPoints(0.6)
.Gutter = InchesToPoints(0)
.HeaderDistance = InchesToPoints(0.5)
.FooterDistance = InchesToPoints(0.5)
.PageWidth = InchesToPoints(8.27)
.PageHeight = InchesToPoints(11.69)
.FirstPageTray = wdPrinterDefaultBin
.OtherPagesTray = wdPrinterDefaultBin
.SectionStart = wdSectionNewPage
.OddAndEvenPagesHeaderFooter = False
.DifferentFirstPageHeaderFooter = False
.VerticalAlignment = wdAlignVerticalTop
.SuppressEndnotes = False
.MirrorMargins = Falsesdfg
.TwoPagesOnOne = False
.GutterPos = wdGutterPosLeft
End With
ActiveWindow.ActivePane.View.Zoom.PageFit = wdPageFitBestFit
Selection.Font.Name = "Verdana"
End Sub

johnske
10-08-2004, 01:17 AM
OK kieran, thanx, that's deleting the button now, but I'm getting an error message "Object variable or With block variable not set" that I cant get rid of..:confused:

Howard Kaikow
10-08-2004, 07:37 AM
(This is probably a better way to do the job, but the original question is only avoided - not answered - and I may need to do something similar to the original prob at another time) :bink:

Sub Document_Open()
Dim Answer As VbMsgBoxResult
Answer = MsgBox("Your preferred page setup?", vbYesNo)
If Answer = vbNo Then End

Application.ScreenUpdating = False
CustomizationContext = NormalTemplate
With ActiveDocument.PageSetup
.LineNumbering.Active = False
.Orientation = wdOrientPortrait
.TopMargin = InchesToPoints(0.6)
.BottomMargin = InchesToPoints(0.97)
.LeftMargin = InchesToPoints(0.6)
.RightMargin = InchesToPoints(0.6)
.Gutter = InchesToPoints(0)
.HeaderDistance = InchesToPoints(0.5)
.FooterDistance = InchesToPoints(0.5)
.PageWidth = InchesToPoints(8.27)
.PageHeight = InchesToPoints(11.69)
.FirstPageTray = wdPrinterDefaultBin
.OtherPagesTray = wdPrinterDefaultBin
.SectionStart = wdSectionNewPage
.OddAndEvenPagesHeaderFooter = False
.DifferentFirstPageHeaderFooter = False
.VerticalAlignment = wdAlignVerticalTop
.SuppressEndnotes = False
.MirrorMargins = False
.TwoPagesOnOne = False
.GutterPos = wdGutterPosLeft
End With
Application.ScreenUpdating = True
ActiveWindow.ActivePane.View.Zoom.PageFit = wdPageFitBestFit
Selection.Font.Name = "Verdana"
End Sub
Some comments:

1. Use
If Answer = vbNo Then Exit Sub

End should only be used in very restricted circumstances.
End has dire consequences. See the Help for the End statement.

2. CustomizationContext = NormalTemplate is not needed in this code.

3. Application.ScreenUpdating = False and Application.ScreenUpdating = True are not needed here.