Excel VBA: Manipulate Shockwave Flash Property - vba

I'm working on a little project to make a 'memeplayer' in Excel with YouTube videos - here's a screenshot of the spreadsheet
The code I'm attempting to use is below:
Option Explicit
Sub PlaySamsMemes()
Dim sChosenTitle As String
Dim rngVideoData As Range
Dim lFinalRow As Long
Dim sLookupValue As String
lFinalRow = Range("C65536").End(xlUp).Row
sChosenTitle = Range("F4").Value
Set rngVideoData = Range("B2:C" & lFinalRow)
sLookupValue = Application.VLookup(sChosenTitle, rngVideoData, 2, False)
Memeplayer.SetVariable("Movie", sLookupValue)
End Sub
I'd be really grateful if you could tell me how to get the script to play nice with the embedded Shockwave Flash that I've named 'Memeplayer' in the Excel interface. The line I'm using to try and make it run is:
Memeplayer.SetVariable("Movie", sLookupValue)
but it just returns an 'Object required' error (Runtime Error 424). I've not worked with shapes and objects much as yet and I've no idea how to fix it. Help me please!

I've since solved the problem. I just hadn't Set the Object correctly - couldn't figure out the heirachy for it. The code is below:
Set MemePlayer = Sheets(1).MemePlayer
MemePlayer.Movie = sLookupValue

VBA has many limitations and trying to do something like this in Excel is probably not the best way to go about it. However, here are 2 websites that might shed some light on your problem:
http://chandoo.org/wp/2011/01/11/embed-youtube-videos-excel/ - this should help most I think
http://www.cpearson.com/excel/DownloadFile.aspx (from the website: http://www.pcreview.co.uk/forums/do-use-vba-play-sound-clip-website-t3893819.html)
Hope this helps.

Related

Receiving an error from trying to make a startup program shortcut

I am trying to make a shortcut for my program so that it starts automatically with windows. The following code is the sub that creates the shortcut and how it gets it's variables but it fails.
Dim ShortCutPath As String = Environment.GetFolderPath(Environment.SpecialFolder.Startup) & "/ScreenRot.lnk"
Shared Sub CreateShortCut(File As String, ShortCutPath As String)
Dim oShell As Object
Dim oLink As Object
'you don’t need to import anything in the project reference to create the Shell Object
Try
oShell = CreateObject("WScript.Shell")
oLink = oShell.CreateShortcut(ShortCutPath)
oLink.IconLocation = File
oLink.TargetPath = File
oLink.Arguments = ""
oLink.WindowStyle = 1
oLink.Save()
Catch ex As Exception
End Try
End Sub
The line it seems to fail on is.
oLink = oShell.CreateShortcut(ShortCutPath)
The error I am getting is
DirectCast(ex, System.MissingMemberException).Message
Public member 'CreateShortcut' on type 'IWshShell3' not found.
I am using this in my program.
Imports IWshRuntimeLibrary
I have tried a couple different ways to make the shortcut but this seems to be the one that should work for what I need. I've read a bit about using this code and watched a video but nothing talks about the error. I've googled the error but nothing resembles a solution. I've tried to adjust the code slightly by using other examples but it still fails with more or less the same error. I don't really understand what the error is saying, so I can try and figure it out. thanks for your time and any help you guys can give.
After reviewing many posts and trying a lot of different things I found a solution on Stackoverflow which I can confirm actually works.
Stackoverflow Post
This post has the solution, hopefully it helps other people with this problem.

Run-time error '3265': Item not found in this collection. VBA module in Access

I am trying to create a macro that will copy a table and its associated queries for each year's equipment audit. I copied some code that I found on another forum, but I receive the error in the title on one line. I'm not familiar enough with VBA to understand what the problem is, so I'm hoping I can get some help.
This code is supposed to replace the source table on a copied query so that the queries don't have to be remade each year. Here is the code:
Sub UpdateQuery(QueryName, CurrentSourceTable, NewSourceTable)
Dim strQryName, strCTbl, strNTbl, strCsql, strNsql As String
Dim defqry As DAO.QueryDef
strQryName = QueryName
strCTbl = CurrentSourceTable
strNTbl = NewSourceTable
Set defqry = CurrentDb.QueryDefs(strQryName)
strCsql = defqry.SQL
strNsql = Replace(strCsql, strCTbl, strNTbl)
defqry.SQL = strNsql
defqry.Close
End Sub
The error occurs on the "Set defqry" line. Can anyone tell me what is causing the error?
ETA:
When I try to run the code above, I'm also using the following code to fill in QueryName/CurrentSourceTable/NewSourceTable:
Sub Proc1()
Call UpdateQuery(YYYY_Count_of_items_by_floor, Building_Audit_2021, Building_Audit_YYYY)
End Sub
Again, I did not write this code, I copied code that someone else had written and am attempting to use it for what I need.
Thanks, HansUp! YYYY_Count_of_items_by_floor is indeed the name of the query, so enclosing it in quotes was all that I needed to do.

SolidWorks VBA - Translating API Help into useable code

I'd like to do what feels like a fairly simple task, and I've found the specific API Help pages which should make it clear, but, I can't actually make things work.
The Key steps that I would like to achieve are:
Rename the active document
Update References to this document to accommodate new name
Save active document.
This help page shows the Usage for renaming the doc, and under the "Remarks" heading, includes links to the next two steps, mentioning them off hand as if implementing them would be easy.
https://help.solidworks.com/2020/English/api/sldworksapi/SolidWorks.Interop.sldworks~SolidWorks.Interop.sldworks.IModelDocExtension~RenameDocument.html?verRedirect=1
The trouble is, I'm a bit of a VBA beginner - usually I get by with the 'record' function, and then tidying things up from there - but undertaking the steps above manually doesn't result in anything being recorded at all for one reason or another.
Assuming I am able to pass in the item to be renamed (I'll define a variable at the start of the Sub for this e.g. swModel = swApp.ActiveDoc), and the new name (NewName = "NEW NAME HERE"), How would I translate the Help API into a Sub that I can actually run?
Two of them suggest declaring as a Function, and one as a Public Interface - I've never used these before - do these just run in a standard Module? Do I need to write a 'master Sub' to call the different functions sequentially, or could these be included directly in the sub, if they're only to be used once?
[Feeling a little lost - it's demoralizing when the help files aren't all that helpful]
Let me know if there's any more information missing that I can add to improve my question - as I said, I'm fairly new to this coding thing...
The "record" function is sometimes a good point to start but there are a lot of functions it can't recognize while you execute them manually.
The API Help is then useful to find out how to use a specific function.
In almost every example the use of a specific method (e.g. RenameDocument) is only shown abstract. There is always a instance variable which shows you the object-type needed to call this method. So you can use these in every sub you want, but beforehand need access to the specific instance objects.
For your example the RenameDocument method is called with an object of the type IModelDocExtension. First thing for you to do is to get this object and then you can call the method as described in the help article.
Under Remarks in the article you find additional information for what you maybe have to do before or after calling a method.
For your example it is mentioned that the renaming takes permanently place after saving the document.
And finally here is what you want to do with some VBA code:
Dim swApp As SldWorks.SldWorks
Dim swModel As ModelDoc2
Sub main()
' get the solidworks application object
Set swApp = Application.SldWorks
'get the current opened document object
Set swModel = swApp.ActiveDoc
' get the modeldocextension object
Dim swModelExtension As ModelDocExtension
Set swModelExtension = swModel.Extension
Dim lRet As Long
lRet = swModelExtension.RenameDocument("NEW NAME")
If lRet = swRenameDocumentError_e.swRenameDocumentError_None Then
MsgBox "success renaming"
Else
MsgBox "failed with error: " & lRet
End If
End Sub
Afterwars you have to process the return value to check for errors described in this article: https://help.solidworks.com/2020/English/api/swconst/SolidWorks.Interop.swconst~SolidWorks.Interop.swconst.swRenameDocumentError_e.html

VB.Net equivalent for (CustomizationContext) in VBA

I'm busy with some word automation and have run into an issue whereby a context menu within a document has items in, that I wish to remove.
Once the document is open, through vba I can remove these items by running the following code;
[VBA]
Dim oContextMenu As CommandBar
Dim oContextMenuItem As CommandBarControl
'Make changes to the ActiceDocument only (this is needed to make any changes to this document).
CustomizationContext = ActiveDocument
For Each oContextMenu In ActiveDocument.CommandBars
If oContextMenu.Type = MsoBarType.msoBarTypePopup Then 'Loop through all the context menus of type (msoBarTypePopup)
For Each oContextMenuItem In oContextMenu.Controls
If (InStr(oContextMenuItem.Caption, "Smokeball")) Then
oContextMenuItem.Delete
End If
Next
End If
Next
If I execute this code and check the document, all contextMenu sub items that contain the text "smokeball" are removed.
When I try move this code to my VB.NET solution (I have no choice of language, so VB it is), I get errors on the CustomizationContext = ActiveDocument line (this line has to be there for it to affect the current document).
The error I get is CustomizationContext' is not a by reference property.
Does anyone know how to get just that ONE line equivalent for vb.net?
Thanks in advance.
EDIT: In case you need to see the vb.net sub:
Private Sub RemoveUnwantedContextMenuItems()
Dim oContextMenu As CommandBar
Dim oContextMenuItem As CommandBarControl
'Make changes to the ActiceDocument only (this is needed to make any changes to this document).
WordApplication.CustomizationContext = WordApplication.ActiveDocument 'This is the error.
For Each oContextMenu In WordApplication.CommandBars
If oContextMenu.Type = MsoBarType.msoBarTypePopup Then 'Loop through all the context menus of type (msoBarTypePopup)
For Each oContextMenuItem In oContextMenu.Controls
If (InStr(oContextMenuItem.Caption, "Smokeball")) Then
oContextMenuItem.Delete()
End If
Next
End If
Next
End Sub
PS - I have also already tried using the .AttachedTemplate as well as .Normal / .NormalTemplate
Jules pointed me in the right direction with his sample code.
After lots of playing around I noticed that somewhere in the solution, the [TYPE] of WordApplication was getting changed to a dynamic type of sorts, hence, it couldn't use CustomizationContext.
My solution was this:
I changed this line;
WordApplication.CustomizationContext = WordApplication.ActiveDocument
To this:
CType(WordApplication, Microsoft.Office.Interop.Word.Application).CustomizationContext = WordApplication.ActiveDocument
Forcing the types to be correct.
Simple solution but took some time.
Thanks to Jules for pointing me in the right direction.
(Points should go to you).

Powerpoint VBA Runtime Error 438 (easy)

I am all new to powerpoint vba. All i want do is to write a little piece of code wich allows me to change the layout of my slide depending on a selection made by clicking a button.
The Problem with my code is that i get the runtimeerror 438.
Here is what i have:
Private Sub CommandButton1_Click() 'Klick Button 1'
ActivePresentation.Slides(10).Delete
ActivePresentation.Slides(9).Delete
ActivePresentation.Slides(8).Delete
Dim x As Integer
For x = 1 To 100
With ActivePresentation.Slides(x)
If .CustomLayout = .CustomLayout(8) Then
Set .CustomLayout = .CustomLayout(12)
End If
End With
Next x
End Sub
EDIT: Error Description is: "object does not support this property or method"
I'd really appreciate any kind of help and constructive input.
EDIT II: I understand now that .CustomLayout returns a custom layout. But how can i set/change the Layout of a certrain slide? How do i need to adress it?
Thank you very much
EDIT III: I still have no solution and i am really frustrated right now. You guys are my last chance for help I guess. So here again my code right now:
Dim x As Integer
For x = 7 To 100
If ActivePresentation.Slides(x).CustomLayout = ActivePresentation.Designs(1).SlideMaster.CustomLayouts(8) Then ActivePresentation.Slides(x).CustomLayout = ActivePresentation.Designs(1).SlideMaster.CustomLayouts(12)
End If
Next x
I still get the runtime error descriped above. How can i get rid of it and make my code work?
Thank you very much!
CustomLayout is an object tha define a custom layout, in the interface they are:
In vba they can be accessed using the ActivePresentation.Designs.SlideMaster object.
Every Slide object can have, obviously, only 1 CustomLayout applied, and you can access it using the property CustomLayout.
So, if you want to change the slide 1 CustomLayout using the CustomLayout n. 3 you have to do:
ActivePresentation.Slides(1).CustomLayout = ActivePresentation.Designs(1).SlideMaster.CustomLayouts(3)
See : MSDN
Regarding your code, you MUST use Names in your If block comaprison, so:
If ActivePresentation.Slides(x).CustomLayout.Name = ActivePresentation.Designs(1).SlideMaster.CustomLayouts(3).Name Then
ActivePresentation.Slides(x).CustomLayout = ActivePresentation.Designs(1).SlideMaster.CustomLayouts(7)
End If