Consulting

Results 1 to 5 of 5

Thread: Solved: Can't delete button - Syntax Help

  1. #1
    Administrator
    Chat VP
    VBAX Guru johnske's Avatar
    Joined
    Jul 2004
    Location
    Townsville, Australia
    Posts
    2,872
    Location

    Solved: Can't delete button - Syntax Help

    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.....
    [VBA]
    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
    [/VBA]

  2. #2
    Administrator
    Chat VP VBAX Guru johnske's Avatar
    Joined
    Jul 2004
    Location
    Townsville, Australia
    Posts
    2,872
    Location
    (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)
    [vba]
    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
    [/vba]

  3. #3
    VBAX Regular
    Joined
    Sep 2004
    Location
    Perth, Western Australia
    Posts
    20
    Location
    Try this


    [VBA]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
    [/VBA]
    Kieran

  4. #4
    Administrator
    Chat VP VBAX Guru johnske's Avatar
    Joined
    Jul 2004
    Location
    Townsville, Australia
    Posts
    2,872
    Location
    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..

  5. #5
    VBAX Mentor
    Joined
    Sep 2004
    Location
    Nashua, NH, USA
    Posts
    489
    Location
    Quote Originally Posted by johnske
    (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)
    [vba]
    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
    [/vba]
    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •