VBA code Powerpoint with run-time error 13

riahon

New Member
Joined
Aug 9, 2015
Messages
3
I have found this code to fetch random words from txt file. Both Powerpoint and txt file are in the same folder. I am getting run-time error 13 whenever I run it. Can someone help please

The original code had 2 words, but I only needed one so edited, but I get same error.
I got this from youtube and others have commented the same error but I am not able to resolve.


Public myArray, Word1
Sub OnSlideShowPageChange(ByVal SSW As SlideShowWindow)

If SSW.Presentation.SlideShowSettings.StartingSlide Then

Randomize

Label1.Caption = ""

Dim path

path = ActivePresentation.path & "\words.txt"

Open path For Input As #1

filecontent = Input(LOF(1), #1)

Close #1

myArray = Split(filecontent, vbCrLf)

End If

End Sub
Private Sub CommandButton1_Click()

Word1 = Int((UBound(myArray)) * Rnd)

Label1.Caption = myArray(Word1)
End Sub
 

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
Suggestions to improve your chances of getting a solution:
- Always state what line causes the error
- provide the error description as well rather than making anyone google the number (there are thousands of error numbers)
- post code within code tags (vba button on posting toolbar) and maintain proper indentation. Makes code easier to follow.

I happen to remember maybe 3 code numbers at the moment - 5, 13, 91 and 1004. In your case I have no idea what line causes error 13 in that code but even if I did I might not know the cause of it.
 
Upvote 0
Suggestions to improve your chances of getting a solution:
- Always state what line causes the error
- provide the error description as well rather than making anyone google the number (there are thousands of error numbers)
- post code within code tags (vba button on posting toolbar) and maintain proper indentation. Makes code easier to follow.

I happen to remember maybe 3 code numbers at the moment - 5, 13, 91 and 1004. In your case I have no idea what line causes error 13 in that code but even if I did I might not know the cause of it.
Thanks for tips.
The error is on this line Word1 = Int((UBound(myArray)) * Rnd) when I click the commandbutton1 in the presentation.
The error message is Type Mismatch
Sorry dont know what you mean by code tags and proper indentation. I just pasted it from VBA
 
Upvote 0
1652821149563.png


Just a guess but since you didn't declare your variable types they are both variants. If you're passing strings to Word1 you should be able to get away with that, but myArray as a variant may not be creating an array. I might have written that as follows, but cannot say for sure as I don't have your file to work with:
VBA Code:
Public myArray() As String, Word1 As String
Dim path

Sub OnSlideShowPageChange(ByVal SSW As SlideShowWindow)

If SSW.Presentation.SlideShowSettings.StartingSlide Then
  Randomize
  Label1.Caption = ""
  path = ActivePresentation.path & "\words.txt"
  Open path For Input As #1
  filecontent = Input(LOF(1), #1)
  Close #1
  myArray = Split(filecontent, vbCrLf)
End If

End Sub

Private Sub CommandButton1_Click()

Word1 = Int((UBound(myArray)) * Rnd)
Label1.Caption = myArray(Word1)

End Sub
However, I don't know which event runs first either. If button click, then myArray is Empty? You can set a break point at the beginning of both subs and step through them. Mouse over variables to see their value after a line has processed. Can also inquire in immediate window as in

?Ubound(myArray) If 0, there's nothing in it - assuming this array was populated somewhere else first.
 
Upvote 0
Thanks for responding. It is clear that I have not provided enough data to get help here and apologise. I saw the youtube vid I posted an and thought this would be a fun activity to try with powerpoint. I don't want to learn all the intricacies of VBA as it is not something I need to use in my day to day. I have tried your suggestion Micron and that hasn't worked but thank you for trying. I can move on from this, not a big deal. Sorry if I have wasted anyones time.
 
Upvote 0

Forum statistics

Threads
1,223,963
Messages
6,175,656
Members
452,664
Latest member
alpserbetli

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top