i'm using visual studio excel page
when i try copy page from my book im getting error on this line,
error:
System.Runtime.InteropServices.COMException (0x800A03EC): The Copy
method of the Worksheet class failed
Dim xlWB = Globals.ThisWorkbook
xlWB.Worksheets("RAPOR").Copy(before:=newwb.Sheets(1))
Related
I am using Access 2007. In my Access file I have VBA code that opens an Excel
workbook when the Access program begins. The Excel workbook remains open
while the program is running. I use this code to open the workbook:
Public xl As Object
Public wb1 As Object
Set xl = New Excel.Application
Set wb1 = xl.Workbooks.Open("c:\Book1.xlsx")
Now while the program is running I may need to open a second Excel workbook,
get some data from it, then close it. I used this code:
Public wb2 As Object
Set wb2 = xl.Workbooks.Open("c:\Book2.xlsx")
But this doesn't work. It seems that the first workbook may have closed because
if I do debug.print Workbooks.Count the response is "0".
Any suggestions on how to open a second workbook and still have the first workbook open?
Should I create a second instance of Excel and use that to open the second workbook?
Thank you.
You may have more luck with this procedure instead:
Sub OpenExcel(fName As String)
Dim xlApp As Object
On Error Resume Next
Set xlApp = GetObject(, "Excel.Application")
If Err.Number <> 0 Then
'Excel wasn't running, start it from code
Set xlApp = CreateObject("Excel.Application")
End If
On Error GoTo 0
xlApp.Workbooks.Open (fName)
End Sub
With your code it's possible that Excel was opening but the second workbook did not default to visible, which could be set with xl.Visible = True. Alternatively, a second xl object could have helped.
Regardless, this method is safer is a number of ways.
Make sure when you're done, make sure you clean up (and free up memory) with something like xlApp.Quit and Set xlApp = Nothing.
In fact, I'd suggest you reboot right now... before you do, take a look at the running processes in your Task Manager - I wouldn't be surprised if you have several "invisible" instances of Excel running. :)
I'm trying to pull together a marco that add data and charts from Excel into a powerpoint template and save it in a directory. The macro is being run from Excel.
After running it, the following message appears:
Run-time error '-2147467259 (80004005)':
Presentation (unknown member): PowerPoint was unable to open or save this document. Please ensure that you have access priviliges to read or write the document that is not encrypted.
Below is the VBA code:
Sub CreateMPP()
Dim DestinationTemplate As String
Dim PowerPointApp As PowerPoint.Application
Dim myTemplate As PowerPoint.Presentation
DestinationTemplate = "C:\Users\Me\Documents\MPP_Template.potx"
Set PowerPointApp = CreateObject("PowerPoint.Application")
PowerPointApp.Visible = True
Set myTemplate = PowerPointApp.Presentations.Open(DestinationTemplate, ReadOnly:=msoFalse)
'Other macro that pulls the data and charts, which works perfectly fine
Call CreatePowerPointTest
myTemplate.SaveAs "TestTest", ppSaveAsPresentation
myTemplate.Close
PowerPointApp.Quit
End Sub
We embed pptx files in Excel, then use Excel VBA code (like below) to open, then SaveAs the pptx file to the user's drive, then programmatically modify the pptx content based on Excel calculations.
The Excel VBA code below works fine to control PowerPoint 2010 and 2013, but no longer works for PowerPoint 2016.
Note: I have similar code for Word and it works fine for Word 2016 (and prior versions).
Sub OpenCopyOfEmbeddedPPTFile() 'works with Office 2010 and 2013, but not on Office 2016
Dim oOleObj As OLEObject
Dim PPTApp As Object
Dim PPTPres As Object
Dim sFileName As String
Set oOleObj = ActiveSheet.Shapes("PPTObj").OLEFormat.Object 'name of the embedded pptx object
Set PPTApp = CreateObject("Powerpoint.Application")
PPTApp.Visible = True
sFileName = "C:\Users\Me\Documents\testPPT.pptx"
OleObj.Verb Verb:=xlVerbOpen 'it opens successfully
Set PPTPres = oOleObj.Object
PPTPres.SaveAs Filename:=sFileName 'fails here (in Office 2016)
PPTPres.Close
GetObject(, "PowerPoint.Application").Presentations.Open sFileName
'code to modify the Presentation (copy of the embedded pptx) based on Excel calculations
End Sub
Error:
Run-time error '-2147467259 (80004005)':
Presentation.SaveAs : An error occurred while PowerPoint was saving the file.
Also, the following PowerPoint VBA (not Excel VBA) works fine for normal documents (not embedded in Excel), but fails when I run it in an opened embedded pptm. Works fine in 2013 and 2010 embedded pptm's.
ActivePresentation.SaveAs FileName:="C:\Users\Me\Documents\testPPT.pptm"
Error:
Run-time error '-2147467259 (80004005)': Presentation (unknown member) : An error occurred while PowerPoint was saving the file.
Windows OS version does not seem to matter. Does not work on Office for Mac.
Any way to resolve or workaround this error? Or is there another way to do the same thing (modify a copy of the embedded pptx so the embedded pptx is not modified)? Or does this error only occur on our Office 2016 PCs?
PowerPoint 2016 appears to fail when using SaveAs or SaveCopyAs against an embedded presentation.
The workaround is to open the presentation, create a new presentation, and then copy the content from the embedded presentation to the new presentation. You can then close the embedded presentation, and save the new presentation as you wish.
I've demonstrated copying the slides, but you may need to programmatically copy BuiltInDocumentProperties and other non-slide content.
Option Explicit
Sub OpenCopyOfEmbeddedPPTFile() 'works with Office 2010 and 2013, but not on Office 2016
Dim oOleObj As OLEObject
Dim PPTApp As Object
Dim PPTPres As Object
Dim PPTNewPres As Object
Dim sFileName As String
Set oOleObj = ActiveSheet.Shapes("PPTObj").OLEFormat.Object 'name of the embedded pptx object
oOleObj.Verb 3
Set PPTPres = oOleObj.Object
Set PPTApp = PPTPres.Application
PPTApp.Visible = True
'We can't save the embedded presentation in 2016, so let's copy the clides to a new presentation
Set PPTNewPres = PPTApp.Presentations.Add
PPTPres.Slides.Range.Copy
PPTNewPres.Slides.Paste
'We may need to copy other things, like BuiltInDocumentProperties
'TODO
'Close the original
PPTPres.Close
sFileName = "C:\Users\Me\Documents\testPPT121.pptx"
sFileName = "C:\users\andrew\desktop\testPPT12111-FOOJANE.pptx"
PPTNewPres.SaveAs sFileName
'code to modify the Presentation (copy of the embedded pptx) based on Excel calculations
'Quit PPT
'PPTNewPres.Close
'PPTApp.Quit
End Sub
I found this possible answer in another forum and it worked for me saving a PPTM file as a PPTX file as a copy
Change
PPTPres.SaveAs Filename:=sFileName
to
PPTPres.SaveAs sFileName
Mine was :
PPTPres.SaveCopyAs sFileName
I then open the new file and close the PPTM file
I currently have the below code which opens a workbook in a new instance of excel (The workbook that is opened has an on open macro).
However when the code is running in the new instance, this also takes up the existing instance of excel and only frees it once the workbook open macro has run.Is there a way to open excel in a new instance and detach/stop the code from the existing workbook immediately?
Please see code below.
Sub OpenWBInNewInstance()
Dim xApp As Excel.Application
Set xApp = New Excel.Application
xApp.Visible = True
Dim wb As Excel.Workbook
Set wb = xApp.Workbooks.Open("Z:\MI\Not Purchased Detail\Not Purchased Detail Disposal Marketing Template.xlsb")
End Sub
Code execution runs on a single thread in VBA, so you can't do what you're asking because the execution won't move to the next line until the entire Workbooks.Open line has been processed - which includes the Workbook_Open event in the other workbook.
A possible workaround it to use VBScript instead:
Sub OpenWBInNewInstance()
Open Environ$("USERPROFILE") & "\Desktop\temp.vbs" For Output As #1
Print #1, "Set xl = CreateObject(""Excel.Application"")"
Print #1, "xl.Workbooks.Open ""Z:\MI\Not Purchased Detail\Not Purchased Detail Disposal Marketing Template.xlsb"""
Close #1
Shell Environ$("USERPROFILE") & "\Desktop\temp.vbs"
'// You will need some kind of hook here to set the workbook in other instance...
''''''''Public Variables Module'''''''
Public oXL As Microsoft.Office.Interop.Excel.Application
Public oWB As Microsoft.Office.Interop.Excel.Workbook
Public oSheet As Microsoft.Office.Interop.Excel.Worksheet
''''''''End Public Variables Module''''''''
'''''''''Form1.vb'''''''''''
oXL = New Excel.Application
oXL.Visible = True
oWB = oXL.Workbooks.Add
oSheet = oWB.ActiveSheet
oWB.Worksheets(1).range("a1").select()
oWB.Worksheets(1).Paste()
oSheet.UsedRange.Copy()
oWB.Worksheets.Add()
oWB.Worksheets("Sheet2").range("a1").select()
oWB.Worksheets("Sheet2").PasteSpecial(Transpose:=True)
'''''''''End Form1.vb'''''''''''
It's crashing on the pastespecial line with error:
Additional information: Exception from HRESULT: 0x800A03EC
I've tried skipping the range("a1").select step on sheet2 to no avail.
I should add that the reason I am pasting data to the first worksheet before re-copying it and pasting to the second is because the data is already on the the clipboard from an external application, and I am assuming it isn't possible to transpose it on the initial paste when it's data from an external source.
If I change the pastespecial to a normal paste, it works fine. So I am assuming something is wrong with my pastespecial command.
I have tried copying multiple forms of data to my clipboard before running the app so it's not related to a specific set of data being untransposable.
I have also verified that it is in fact selecting the usedrange before copying (vs the entire sheet).
Figured it out, needed to specify a range as well:
oWB.Worksheets("Sheet2").range("a1").PasteSpecial(Transpose:=True)