Gruppe
Allgemein
Bereich
Internet
Thema
Webseite öffnen und eine Schaltfläche mit Makro hinzufügen
Problem
Es soll eine Webseite geöffnet und dieser ein Makro mit Schaltfläche hinzugefügt werden.
Lösung
Geben Sie den nachfolgenden Code in ein Standardmodul ein und weisen Sie ihn einer Schaltfläche zu.
StandardModule: Modul1
Sub MacroToWeb()
Dim mdl As Object
Dim oBtn As Button
Dim sFile As String, sSource As String, sTarget As String
Dim sCode As String
On Error GoTo ERRORHANDLER
Application.VBE.MainWindow.Visible = False
Application.ScreenUpdating = False
sCode = "Sub Message()" & vbLf
sCode = sCode & " MsgBox ""Hallo!""" & vbLf
sCode = sCode & "End Sub" & vbLf
sSource = "https://www.herber.de"
sTarget = ThisWorkbook.Path
sFile = "flash"
Workbooks.Open sSource & "/" & sFile & ".htm"
With ActiveWorkbook.VBProject
Set mdl = .VBComponents.Add(1)
mdl.codemodule.addfromstring sCode
End With
Set oBtn = ActiveSheet.Buttons.Add(100, 150, 80, 20)
Application.DisplayAlerts = False
ActiveWorkbook.SaveAs sTarget & "\" & sFile & ".xls"
Application.DisplayAlerts = True
With oBtn
.Caption = "Meldung"
.OnAction = sFile & ".xls!Message"
End With
ERRORHANDLER:
Application.ScreenUpdating = True
End Sub