Adding page to multipage - vba

I use Excel 2016
I want to add a page to a Multi Page control.
For some reason I get the error "Run-time error '13': Type mismatch"
I have a blank Excel file with a UserForm. On the UserForm I have a CommandButton and a MultiPage, both are out of the box. I added the following code to the CommandButton
Private Sub CommandButton1_Click()
Dim myPage As Page
Set myPage = Me.MultiPage1.Pages.Add("testpage", "TestPage")
End Sub
Do I need to add references to some toolkit for this to work? For the moment I have the following references active
Visual Basic For Applications
Microsfot Excel 16.0 Object Library
OLE Automation
Microsoft Office 16.0 Object Library
Microsoft Forms 2.0 Object Library
When I write the Dim myPage declaration I get a list of objects.. I have two Page objects? Also when I write
myPage.
to get a list of available things for that object I see the following
CenterFooter
CenterHeader
LeftFooter
LeftHeader
RightFooter
RightHeader
So I assume myPage object is not from the right Page type

This works also:
MultiPage1.Pages.Add "tst1", "TestPage"

This works without getting a reference to the new Page object
Me.MultiPage1.Pages.Add
Me.MultiPage1.Pages(Me.MultiPage1.Pages.Count - 1).Name = "testpage"
Me.MultiPage1.Pages(Me.MultiPage1.Pages.Count - 1).Caption = "TestPage"
Debug.Print Me.MultiPage1.Pages(Me.MultiPage1.Pages.Count - 1).Name
Debug.Print Me.MultiPage1.Pages(Me.MultiPage1.Pages.Count - 1).Caption

The easy non code way:
Left click->New Page

Related

CommandBars("Text").Controls not deleted when exiting the document - VBA word add-in

I'm trying to create a add in for MS Word with VBA.
It has a "AutoExec" procedure that creates a new item in the CommandBar("Text") collection (right click menu) and a "AutoExit" that deletes this created item.
As an example, I tried the code below that create an item "How Many Pages?", which executes a macro displaying the number of pages in the active document.
This is the AutoExec Code:
Public Sub AutoExec()
Dim objcommandbutton As CommandBarButton
Call MsgBox("AutoExec")
Set objcommandbutton = Application.CommandBars("Text").Controls.Add _
(Type:=msoControlButton, Before:=1)
objcommandbutton.Caption = "How Many Pages?"
objcommandbutton.OnAction = "HowManyPages"
End Sub
This is the AutoExit Code:
Public Sub AutoExit()
Dim objcommandbutton As CommandBarControl
Call MsgBox("AutoExit")
For Each objcommandbutton In Application.CommandBars("Text").Controls
If objcommandbutton.Caption = "How Many Pages?" Then
objcommandbutton.Delete
End If
Next objcommandbutton
End Sub
This is the Main Macro Code:
Public Sub HowManyPages()
If Documents.Count > 0 Then
Call MsgBox(ActiveDocument.BuiltInDocumentProperties("Number of Pages"))
Else
Call MsgBox("No document is currently active.")
End If
End Sub
When exiting the document, the Button previously added in CommandBars("Text") collection is not deleted. I see this when I open a new blank Word document and the button remains in the right click menu.
I know that the routine is performed correctly because there is a MsgBox instruction to verify it.
This only happen with the AutoExit subroutine of a add-in, that is, loaded as a add-in: running the code in a macro with vba module works fine.
Any help?
When working with the CommandBars object model in Word it's necessary to always specify the Application.CustomizationContext.
Word can save keyboard layouts and CommandBar customizations in various places: the Normal.dotm template, the current template or the current document. The default when you create a CommandBar addition may not be the3 same as the default when trying to delete something.
Since this is an add-in, I assume you want the change for the entire Word environment (any open document). In that case, use the context NormalTemplate. Use this before any calls to CommandBar:
Application.CustomizationContext = NormalTemplate
Note: for saving a customization in the current document: = ActiveDocument; for saving in the template attached to the current document: = ActiveDocument.AttachedTemplate.
I solved my problem with a workaround:
I was trying to "add" a template (.dotm) as a add-in (in the "Templates and Add-ins" window) to use my VBA project in a new document. That's why I was using the AutoExec() and AutoExit() procedures.
But only now I figure out that just "attaching" the .dotm template to the active document (in the same "Templates and Add-ins" window, as show in the figure below) makes the functions Private Sub Document_Open() and Private Sub Document_Close() to run normally. Which solves my problem.
Even so, I think there is a certain "issue" with the AutoExit() procedure when trying to change the CommandBars itens. But that's ok for now.
enter image description here

Adding shapes/pages to visio on shape drop

Hello I am fairly new to VBA in visio, and I am trying to add functionality to a visio template so that a page will be added to the active document whenever a specific shape is dropped onto a page. I looked through MSDN and found an example using Application.ShapeAdded function but the active document I am working in doesn't seem to be responding to my modified code.
Private Sub Document_ShapeAdded(ByVal vsoShape As Visio.IVShape)
Dim vsoMaster As Visio.Master
'Get the Master property of the shape.
Set vsoMaster = vsoShape.Master
'If Visio shape added is named "SC" add a new page
If vsoMaster.Name = "SC" Then
NewPage
End If
End Sub
I drop the shape master "SC", which I confirmed is the name of the shape master, and nothing happens. The MSDN verbage describes Application.ShapeAdded as an event listener to the open application. Am I missing something or is there possibly a better way to do this I am not thinking of?
Here is the MSDN description: https://msdn.microsoft.com/en-us/library/office/ff766392.aspx
The Document_ShapeAdded event will only apply to the document in which the VBA code is located.
You'd have to declare an application object withevents and have it watch for that event.
Example (in an object or ThisDocument module):
Private WithEvents App as Visio.Application
Private Sub App_ShapeAdded(ByVal Shape As IVShape)
Call ActiveDocument.Pages.Add() ' etc..
End Sub
Alternatively, if it's something simple, you could just add a shapesheet CALLTHIS function on the master shape in question, which just fires a VBA routine to add a new page or whatever you have to do.

VB 2013, modify word document property/custom property

I wish to make an add-in to update word document properties. I’m all ready to go with the addin / ribbon / etc. as I have other ribbon functions that are working. I used the MSVS wizard to create the word ribbon project.
I’m stuck on how to; access the active word document, and access the properties/custom properties. I can’t figure out the; declarations, calls, library, etc. I have not been able to make any of the MSDN samples work…. I’m totally missing something.
For example: ‘ActiveDocument.CustomDocumentProperties’ does not work.
Disclaimer - I’m not a coder. I had this all working with vba, I’m trying to port it over to vb. I’m also still reading through the posted help, and trying samples.
Any suggestions would be appreciated. Kind regards,
I got it figured.
Outside the module declare:
Imports moDoc = Microsoft.Office.Interop.Word
Within the sub - This associates the open app with an object:
Dim oActiveApp As moDoc.Application
oActiveApp = GetObject(, "Word.Application")
Now to associate the open app as a document:
Dim mocCustProperties As Microsoft.Office.Core.DocumentProperties
Dim odpProp As Office.DocumentProperty
Now odpProp is an available to read/add properties:
For Each odpProp In mocCustProperties
If odpProp.Name = “something” Then
‘do dtuff
End If
Next
There must be a way to do this by referencing the active document as a document rather than an application, but I was unable to make this work.
Cheers,
Drat - missed a couple lines above - please ignore.
I got it figured.
Outside the module declare:
Imports moDoc = Microsoft.Office.Interop.Word
Within the sub, This associated the open app with the object
Dim oActiveApp As moDoc.Application
oActiveApp = GetObject(, "Word.Application")
oDocCustomProperty = oActiveApp.ActiveDocument.CustomDocumentProperties
Now to associate the open app as a document
Dim mocCustProperties As Microsoft.Office.Core.DocumentProperties
Dim odpProp As Office.DocumentProperty
mocCustProperties = CType(oDocCustomProperty, Office.DocumentProperties)
Now odpProp is an available to read/add propoeties
For Each odpProp In mocCustProperties
If odpProp.Name = “something” Then
‘do dtuff
End If
Next
Learned some more.
I no longer need the:
Imports moDoc = Microsoft.Office.Interop.Word
Using the Office.Core. rather than the Office.Core.Interop
Dim Prop As Microsoft.Office.Core.DocumentProperty
Dim oBuiltInProperties As Microsoft.Office.Core.DocumentProperties
Dim oCustomProperties As Microsoft.Office.Core.DocumentProperties
oBuiltInProperties = DirectCast(Globals.DocSelect.Application.ActiveDocument.BuiltInDocumentProperties, Microsoft.Office.Core.DocumentProperties)
oCustomProperties = DirectCast(Globals.DocSelect.Application.ActiveDocument.CustomDocumentProperties, Microsoft.Office.Core.DocumentProperties)
For Each Prop In oBuiltInProperties
'do stuff
Prop.Name = sx
sy=Prop.Value.ToString
next
'create properties
sx="New Property"
sy="New Property Value"
oCustomProperties.Add(sx, False, Microsoft.Office.Core.MsoDocProperties.msoPropertyTypeString, sy)
All works now.

Reset ToggleButtons

I am hoping to set all of the togglebuttons in a powerpoint to "false" (unpressed) upon starting the program. Any ideas why this sort of code is working?
Sub Start()
ActivePresentation.Slides(4).ToggleButton1.Value = 0
ActivePresentation.Slides(4).ToggleButton4.Value = 0
ActivePresentation.SlideShowWindow.View.Next
End Sub
Thank you!
The syntax
ActivePresentation.Slides(4).ToggleButton1.Value = 0
is correct; I've just tested it and it works.
Are you actually calling the Sub at any point though? Simply naming it Start() is not enough.
Related question re. running code on startup, could be helpful.
I had an issue calling this
ActivePresentation.Slides(4).ToggleButton1.Value = 0
Getting the error Method or data member not found which im assuming is a scope issue. The VBA editor doesnt autocomplete .ToggleButton1 which supports the error i was getting.
So, it appears im referencing the object incorrectly.
To correct this I have done the following.
Public Sub ToggleThisButton()
' This is code on Slide 1 referencing control on slide 4
ActivePresentation.Slides(4).Shapes("ToggleButton1").OLEFormat.Object.Value = 1
End Sub
Excerpt from MSDN
Use the OLEFormat property for a shape, inline shape, or field to
return the OLEFormat object. The following example displays the class
type for the first shape on the active document.
Use the Object
property to return an object that represents an ActiveX control or OLE
object. With this object, you can use the properties and methods of
the container application or the ActiveX control.

VBA IE automation, To do events like displaying message,etc in excel when a link in webpage is clicked

My requirement is i need to have a VBA code in excel which runs to do some action in excel when a link in webpage is clicked.For example when i click a link "login" in webpage my VBA code should press printscreen button in keyboard(like we use sendkeys).
I have been searching for this solution since two weeks over google but failed to get one.This is urgent project requirement, please save my day,thank you very much in advance.
Here's an edited version of an answer I gave to a similar question (http://stackoverflow.com/questions/12877872/possible-to-intercept-onchange-event/12927960#12927960).
Project references set for "Microsoft HTML object library" and "Microsoft internet controls"
In a class module "clsEvents":
Option Explicit
Public WithEvents href As MSHTML.HTMLAnchorElement
'Note that having defined "href" as "WithEvents", if you choose "href"
' from the left-hand dropdown at the top of the class code module
' then the right-hand dropdown will populate with events you can select
' from to create an event-handling procedure in the class
Private Function href_onclick() As Boolean
Debug.Print "link clicked"
href_onclick = False 'cancels the navigation
End Function
In a regular module:
Option Explicit
Dim evt As clsEvents
Sub Setup()
Dim IE As New InternetExplorer
Dim el2 As Object
Set evt = New clsEvents
IE.Visible = True
IE.navigate "http://www.csee.wvu.edu/~riggs/html/select_example.html"
Do While IE.Busy
Loop
Set el2 = IE.document.getElementsByTagName("a")(1)
If Not el2 Is Nothing Then
Debug.Print "setting event capture on link:" & el2.innerText
Set evt.href = el2
End If
End Sub
If you run the Setup sub and then click the "Javascript" link on the page in IE you should see output in the Immediate window of the VB editor.
Hope that helps get you started.
There is nothing native you can do inside Internet Explorer to enable this functionality.
You would need to write an ActiveX control (using C#, C++, VB6, etc.), which would perform the Excel automation.
A few useful links:
How to automate Excel from VB6
How to write an updatable ActiveX control in VB6