I've been searching for quite a while and can't seem to find a solution. I'm trying to copy multiple cells from Excel into a PPT textbox, keeping text only. The code I have seems to work well, but it will randomly error out with the message 'Method "PasteSpecial" of object "TextRange2" failed' When I hit debug, and play, it will continue as normal.
I've tried adding a delay wait since I know the clipboard can get flaky sometimes, but that hasn't seemed to help.
I also came across this post and have tried it but it still randomly errors out: https://answers.microsoft.com/en-us/msoffice/forum/all/powerpoint-vba-flaky-clipboard/3aee03af-b0be-4664-8d17-79e0507210e7
Does anyone know why this error happens randomly and it doesn't even seem to be a real error given I can just hit "play" and it will continue normally.
Here's a snippet of the code:
Sub AddText()
Set oPPTApp = GetObject(, "PowerPoint.Application")
oPPTApp.Visible = True
For x = 1 to 3
ActiveWorkbook.Worksheets("OpenEndChartingNEW").Range("A" & x & ":A" & x * 2).Copy
Application.Wait Now + TimeValue("00:00:05")
oPPTApp.ActivePresentation.slides(6).Shapes("OpenEnd").TextFrame2.TextRange.PasteSpecial msoClipboardFormatPlainText
Next x
Set oPPTApp = Nothing
End Sub
Thank you!
Related
I used VBA to generate the pictures before and the code is shown below, it never happened any error. But when I upgraded the Micro-Soft office version from 2010 to 2016, it happens
"Run-time error '-2147221040 (800401d0)'
occasionally, and it will ok if i try to re-run the same macro. Is there a way to fix this? Thanks in advance~~
Sub Chart_generate()
Dim j As Byte
Dim Max As Byte
Dim TOOL As String
Dim WCpath As String
Sheets("pivot").Select
ActiveSheet.PivotTables("PivotTable1").PivotCache.Refresh
ActiveSheet.PivotTables("PivotTable2").PivotCache.Refresh
ActiveWindow.Zoom = 130
Max = Range("AO1")
For j = 2 To Max + 1
TOOL = Range("AN" & j)
ActiveSheet.PivotTables("PivotTable1").PivotFields("TOOL").CurrentPage = TOOL
ActiveSheet.ChartObjects("Chart1").Activate
WCpath = "D:\users\Picture\" & TOOL & "_Pressure.png"
ActiveChart.ChartArea.Select
ActiveChart.Export (WCpath)
Next j
ActiveWindow.Zoom = 100
Range("A2").Select
End Sub
I have a similar issue when moving graphs generated on hidden worksheets over to a visible worksheet, and then resizing them.
My code is repeated multiple times in a large loop (not seen in code below), and it works more often than not but will periodically fail. If I use the debugger and single step it always works perfectly; it's only a problem when running full speed.
I've tried solutions from the web including DoEvents and the On Error tricks. I added a While loop and some verbal feedback to help identify the code causing the problem.
I hear "runtime 2" more often than "runtime 1". It always works fault-free single stepping in the debugger, but fails occasionally on some computers. During a fail, if I click on the pictures, the name of the object doesn't appear in the upper left box of the worksheet where it normally would.
Of the eleven machines tested, I've seen problems on three of them. Mine works better than most, which is a problem for a code developer!
histogram second
Sheets("Histogram").Activate
ActiveSheet.Shapes.Range(Array("pi_Histogram")).Select
DoEvents
On Error Resume Next
errflag = 0
Selection.Copy
While Err.Number = 1004 And errflag < 10
Err.Number = 0
Application.Speech.Speak "runtime 1"
Selection.Copy
errflag = errflag + 1
Wend
Sheets("Raw Data").Select
Range("V23").Select 'start insertion point for coefficients table
ActiveCell.Offset(1, 10 + segment).Select
DoEvents
errflag = 0
ActiveSheet.Pictures.Paste.Select
While Err.Number <> 0 And errflag < 10
Err.Number = 0
Application.Speech.Speak "runtime 2"
ActiveSheet.Pictures.Paste.Select
errflag = errflag + 1
Wend
On Error GoTo 0
Finally! The answer has been found by my companies IT professional. This issue has nothing to do with the actual VBA code chosen, the problem comes when Windows is managing the virtual memory. Office 2016 is a memory hog and the allocated virtual memory was not high enough. The problem completely goes away when on a machine with 16GB of memory the VM settings are manually changed to Custom size, Initial size=24000 and Maximum size=48000. Apparently when moving/resizing graphs and/or pictures there is a lot of memory being used, and the default setting of "Automatically manage paging file size for all drives" is just NOT the correct setting.
Turning off the clipboard function will help to resolve the problem:
Before this gets marked as a duplicate: It is not, as all the other saving errors seem to get a different MsgBox.
I am writing a macro that opens and closes a PowerPoint Presentation from Excel. Now I have the issue that when I am trying to save the PowerPoint file I get a pop up Message Box:
It says: "PowerPoint-Error while saving the file."
My code:
Dim pptPres As PowerPoint.Presentation
Dim pptApp As PowerPoint.Application
Set pptApp = New PowerPoint.Application
strPath = "S:\Folderxy\"
strFile = "filename.pptm"
strSave = "newFilename"
Set pptPres = pptApp.Presentations.Open(strPath & strFile, False, True, True)
Application.DisplayAlerts = False
On Error GoTo Errorhandler_tryAgain
tryAgain:
pptApp.DisplayAlerts = ppAlertsNone
strSave = "Test123"
pptPres.SaveAs strPath & strSave & ".pptx"
pptPres.Close
Exit Sub
Errorhandler_tryAgain:
Debug.Print "Errorhandler_tryAgain was opened!"
Application.Wait DateAdd("s", 1, Now) 'delay in seconds
GoTo TryAgain
First:
Even though I turned the DisplayAlerts off this one keeps popping up. However I can not easily reproduce this error. It occurs sometimes. Openening, closing and saving *.pptx files is part of a loop and surprisingly this error does not reoccur at the same file but it reoccurs about 2 times in a loop with 70 >files.
Second:
When I manually click enter the RuntimeError 70: Permission Denied is thrown. But then the VBE goes into the debug mode and my Errorhandler is not handling it. The Errohandler is an infinitive loop as I am saving the file on a server and sometimes it fails to save. However when I manually tried to save the document (both, on the server and on the desktop) I got the same "PowerPoint-Error while saving the file." MsgBox.
Now my question is how do I either get rid of the saving error (which seems to be impossible) or how to surppress that error so that my macro does not stop everytime it occurs. As I would like to run the macro overnight.
In case anyone has experienced such a thing before and can help me out I would be very happy.
Thanks in advance
Follow these two things and you should be ok...
Mention the File Format while saving. For example pptPres.SaveAs strPath & strSave & ".pptx",24 '<~~ ppSaveAsOpenXMLPresentation. Also ensure the strPath & strSave & ".pptx" is the extact name of the fiel as you wanted it. Else tweak the variables accordingly.
Always add DoEvents after you issue the save(or save as) statement and before the .Close statement so Excel can get enough time to finish it's tasks.
Application.Wait suspends all Excel activity including execution of your macro. It's not so useful for fixing timing problems. When I want to add a little time so that the program can finish I/O or clipboard tasks, Sleep is a better option. First add a declaration:
Public Declare PtrSafe Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As LongPtr)
Add error-trapping before the problem statement, including a statement to reset error handling after the problem:
TryCut1:
On Error GoTo TooFast1
objShape.TextFrame2.TextRange.Cut
On Error GoTo -1
Then add this for error handling. It waits for 10 milliseconds, then tries again:
TooFast1:
Sleep 10
Resume TryCut1
I have an Excel macro that takes an existing Powerpoint template with chart objects and edits the data for each chart with data from an Excel worksheet. The codes iterates thru 'n' number of rows in worksheet to create a series of 4 slides for each Excel row.
Oddly, for certain rows, one or more slides in the set produces an "Excel has stopped working" error, but the data seems to be installed correctly. However, the problem point is an intermittent and somewhat random "Powerpoint has stopped working" error. The powerpoint error crashes powerpoint entirely. Both seem to occur as I get to around the 50 slide mark or so.
Here is the code for the 4th one in the set as an example. The others are pretty much identical:
oPPT.ActiveWindow.View.GotoSlide Index:=5 + (4 * dIndex)
' SET OBJECT & CHART REFERENCES
Set shapeObject = oPPT.ActivePresentation.Slides(5 + (4 * dIndex)).Shapes("Chart 4")
Set myChart = shapeObject.Chart
Set gChartData = myChart.ChartData
Set gWorkbook = gChartData.Workbook
Set gWorksheet = gWorkbook.Worksheets(1)
' SET VALUES
gWorksheet.Range("B2").Value = myWs.Range("Y" & dRow).Value
gWorksheet.Range("B3").Value = myWs.Range("AA" & dRow).Value
gWorksheet.Range("B4").Value = myWs.Range("Z" & dRow).Value
gWorksheet.Range("B5").Value = myWs.Range("X" & dRow).Value
Set shapeObject = Nothing
Set myChart = Nothing
Set gChartData = Nothing
Set gWorksheet = Nothing
gWorkbook.Close
Set gWorkbook = Nothing
The errors are completely random and occur during different iterations each time. I have found that the 1st time I run never produces the PPT stopped working error. Subsequent ones may.
Any ideas? I am baffled by what is happening here.
Note that I duplicate the template set into as many sets as I need before I iterate the Excel workbook. E.g, if the Excel worksheet has 10 rows, I duplicate the 4 template slides 10 times before doing any edits to the underyling data.
Is there a timing issue going on perhaps?
One thing which seems to be missing is the activation call before accessing the embedded worksheet:
gChartData.Activate
This might explain the crashing issues.
cf. https://msdn.microsoft.com/de-de/vba/powerpoint-vba/articles/chartdata-activate-method-powerpoint
Cheers
Jens
I have been working with this issue for days. I tried multiple different ways. I am attempting to append multiple files into an Access linked table or even a temp table or even into a single excel file. At first EVERY SINGLE TIME on the first attempt the program works perfectly, then after that it stops functioning for a period of time and then starts operating again. When it stop functioning I get an Subscript out of range run-time error 9.I open the proper excel file but for some reason it won't let me set it... How can it OPEN THE FILE but in the next line CAN'T FIND IT??? It is driving me insane, it works, it stops working, then it works again... Any advice or hints would be very much appreciated.
This is just one way I tried to do this but they all end the same.
i = 2 'i is created through another loop previously.
j = 0
With MyXL
.Visible = True
.DisplayAlerts = True
End With
Do
Set MyXL = CreateObject("Excel.Application")
MyXL.Workbooks.Open Directory & fileArray(j), Notify:=False, ReadOnly:=False 'Tried True previously but changed since i was making changes to the file.
Set wb = Workbooks(fileArray(j)) 'DING DING DING!!! WHY??? You WORKED before!!!
If wb.Sheets("Sheet1").Range("A1") = "System Status" Then
wb.Sheets("Sheet1").Range("A1") = "PO System Status"
wb.Save
End If
wb.Close True
Set wb = Nothing
MyXL.Quit
Set MyXL = Nothing
Set wb = Nothing
j = j + 1
Loop Until j = i
Previously I thought I wasn't closing the workbook correctly, but I have closed the MyXL and previous wb but i still run into the error. I was wondering if this is something that Access/vba just can't do in succession as well. I changed the ReadOnly to true and it still ends up the same way.
Set your workbook to the return value from the Open method:
Set wb = MyXL.Workbooks.Open(Directory & fileArray(j), Notify:=False, ReadOnly:=False)
If wb.Sheets("Sheet1").Range("A1") = "System Status" Then
wb.Sheets("Sheet1").Range("A1") = "PO System Status"
wb.Save
End If
You don't need to/shouldn't create a new Excel application instance for every file - set that up before you enter the loop, and close it once you're done updating files. Check your Task Manager and make sure you don't have a bunch of Excel instances hanging around.
When i initially run the code below in debug mode, there is no problems. However when i stop and rerun it again, it says Subscript out of range. Why is that so? Not sure why such error is popping out.
Dim date1 As Double
strDate1 = Sheets("Part2").Cells(i, 1).Value
matchEndRow = Application.Match(CDbl(strDate1), Sheets("1.A").Range("A:A"), 1)
MsgBox matchEndRow
matchEndRow = matchEndRow - 1
MsgBox matchEndRow
Need some guidance.
As follow up from comments, there was another workbook opened when code runs.
So changing
Sheets("Part2") and Sheets("1.A")
to
ThisWorkbook.Sheets("Part2") and ThisWorkbook.Sheets("1.A") solves the problem.