Creating an Excel Workbook via PPT VBA - excel

I am able to open .xlsx file by CreateObject("Excel.Application").Workbooks.Open("path")
Something like this is not allowing me to create a new Excel Workbook via a PowerPoint Macro.
Set ExcelFile = CreateObject("Excel.Application")
ExcelFile.Workbooks.Add
ExcelFile.ActiveWorkbook.SaveAs "path"

sample code, just checked on PP 2016:
(remember to close xlsApp, set obj to nothing etc.)
Public Sub StackOverflow()
Dim xlsApp As Object
Dim wkbWorking As Object
Set xlsApp = CreateObject("Excel.Application") 'basically it opens excel application
Set wkbWorking = xlsApp.Workbooks.Add 'it creates new workbook in just opened excel
xlsApp.Visible = True 'makes excel visible
wkbWorking.SaveAs "C:\Temp\PesentationExcel.xlsx"
wkbWorking.Close 'closes workbook
xlsApp.Quit 'closes excel application
'sets variables to nothing
Set wkbWorking = Nothing
Set xlsApp = Nothing
End Sub

Related

Excel Variable Opens Outlook If I try To View It In Locals Window

I am trying to rename a copied worksheet in Excel using word VBA. When I try to set my worksheet variable to the new worksheet it tries to open Microsoft Outlook 2016. With each line after that (stepping through) it opens a dialogue box asking me to create a new Microsoft Outlook account.
The code does successfully create the copies of the template sheet but it fails to rename them.
** Edited to clarify the real issue and to add "Set oWB = oXL.Workbooks.Open(FileName:=WorkbookToWorkOn) 'Opens Excel" which was already in the workbook macro when the problem occurred.
Dim oXL As Excel.Application 'Requires loading "Microsoft Excel 16.0 Object Library" from Tools -> References
Dim oWB As Excel.Workbook
Dim oSheet As Excel.Worksheet
Set oXL = New Excel.Application
Set oWB = oXL.Workbooks.Open(FileName:=WorkbookToWorkOn) 'Opens Excel
oXL.Visible = True
For i = LBound(seller_names) To UBound(seller_names)
With oXL.ActiveWorkbook
.Sheets("Template").Copy After:=.Sheets(.Sheets.Count)
Set oSheet = Sheets(.Sheets.Count) 'set worksheet to be the template copy
If Len(seller_names(i)) > 31 Then
oSheet.name = Left(seller_names(i), 31)
Else
oSheet.name = seller_names(i)
End If
End With
Next i
The line "Set oSheet = Sheets(.Sheets.Count) is where the open Outlook bug occurs. Both lines oSheet.name = run without generating errors but fail to rename the worksheet.
I fixed this issue by changing
.Sheets("Template").Copy After:=.Sheets(.Sheets.Count)
to
.Sheets("Template").Copy After:=.Sheets("Template (2)")

Open Excel OLEObject in a new Excel instance

I just learned to open programmatically embedded OLEObjects by following VBA-Excel code:
mySheet.OLEObjects(myName).Verb xlVerbOpen
However, if "myName" corresponds to an Excel object, the Excel file is opened in the same Excel instance in which I am running the program. Since at that time there are a couple of forms opened, I would like this object to be opened in a new Excel instance (and not behind the forms, as it is now happening). Is this possible? How could I do it? Thanks a lot in advance.
You can create new Excel Instance in your code (in the below sample - xlApp) and refer to that instance in whatever you do next (however this will work only if OLEObject is created with link):
Dim xlApp As New Excel.Application, xlWb As Workbook
Set xlApp = CreateObject("Excel.Application")
xlApp.Visible = True 'Make new instance visible (by default it's not visible)
Set xlWb = xlApp.Workbooks.Open(Filename) ' Filename is path to the linked workbook
If OLEObject is not linked the only way to open in new Excel instance is to open with the main excel file, which is not what you want:
xlApp.Workbooks.Open(workbookpath).Worksheets("mySheet").OLEObjects(myName).Verb xlVerbOpen
Following Richard's suggestion, see below the code I built to be able to open an embedded excel object through VBA in a new workbook within a new Excel instance:
Dim xlApp As excel.Application
Dim xlWb As Workbook
Dim mySheet As Worksheet
Dim myName as String
Set mySheet = Sheets("write sheet name")
Set myName = Sheets("write object name")
ThisWorkbook.Sheets(mySheet).OLEObjects(myName).Copy
Set xlApp = New excel.Application
xlApp.Visible = True
Set xlWb = xlApp.Workbooks.Add
xlWb.Sheets(1).Paste
xlWb.Sheets(1).OLEObjects(myName).Verb xlVerbOpen
Regards

Embedding PDF in Spreadsheet

Goal is to run this subroutine in an Access application, which embeds a PDF document in a new Excel workbook. The sub works properly with a text file, but will give error "Run-time error '1004': Document not saved." with a PDF file. The code breaks on the SaveAs method. Why does one file-type embed, and the PDF file-type not work? Any help will be appreciated.
Sub SavePDFInWorkBook()
Dim xlObj As New Excel.Application ' Excel application
Dim wbkObj As Excel.workbook ' workbook object
Dim wstObj As Excel.Worksheet ' worksheet object
Dim oleObj As Excel.OLEObject ' embeded object
Dim EmbedFile, MyWbk As String
MyWbk = "M:\SS\DD\Projects\cash_dep2000\TEST.xlsx"
EmbedFile = "M:\SS\DD\Projects\cash_dep2000\DaytonFreight.pdf"
Set wbkObj = xlObj.Workbooks.Add ' workbook is added in open condition in memory
Set wstObj = wbkObj.Worksheets(1) ' workbook is created with one worksheet
Set oleObj = wstObj.OLEObjects.Add(, EmbedFile, False, True)
wbkObj.SaveAs (MyWbk) ' saves workbook to folder
wbkObj.Close ' closes workbook in folder
Set oleObj = Nothing
Set wstObj = Nothing
Set wbkObj = Nothing
Set xlObj = Nothing
MsgBox ("Test Complete")
End Sub
This works to embed pdf icon/link but it also opens the pdf. Aggravating.
ActiveSheet.OLEObjects.Add(Filename:="C:\Users\June\MyStuff\DMV.pdf", Link:=False, _
DisplayAsIcon:=True, IconFileName:= _
"C:\WINDOWS\Installer\{AC76BA86-7AD7-1033-7B44-AB0000000001}\PDFFile_8.ico", _
IconIndex:=0, IconLabel:="whatever you want").Activate
I can manually save the file but SaveAs code for macro-enabled workbook errors. More aggravating. Maybe modifying to your object manipulation code will work.

Editing Excel spreadsheats from Word with VBA

How do I edit excel spreadsheets from word using VBA?
First you need to set a reference to the version of Excel you are running. In the VBE go to Tools>References and click Microsoft Excel 12.0 Object Library (12.0 for 2007, 11.0 for 2003) etc.
Then you can code something like this (opens a new instance of Excel, opens, edits and saves a new workbook). You'd use GetObject to access a running instance of Excel:
Sub EditExcelFromWord()
Dim appExcel As Excel.Application
Dim wb As Excel.Workbook
Dim ws As Excel.Worksheet
Set appExcel = CreateObject("Excel.Application")
With appExcel
.Visible = True
Set wb = .Workbooks.Add
Set ws = wb.Worksheets(1)
ws.Range("A1").Value2 = "Test"
wb.SaveAs ThisDocument.Path & Application.PathSeparator & "temp.xls"
Stop 'admire your work and then click F5 to continue
Set ws = Nothing
Set wb = Nothing
Set appExcel = Nothing
End With
End Sub

Excel 2007 ODBC data populates when opened via Windows Explorer, but not when opened via Access VBA

I have an Excel 2007 workbook that contains an ODBC data connection (to FoxPro, if that matters). The connection is set to "refresh data when opening the file."
When I go into File Explorer and open the workbook, the data populates into the spreadsheet as it should. However, when I execute a function in Access VBA that opens the workbook, the data from the ODBC connection does not populate.
Why would it make a difference which way the workbook is opened? And more importantly, how can I get the data to populate when the workbook is opened via Access VBA?
Here is the Access VBA code that opens the workbook:
Public Sub Subform_cmdOpenFile_Click(frm As Form)
Dim rs As Recordset
Dim ftiSuperclass As FilingTemplateInterface
Set rs = frm.RecordsetClone
If (rs.BOF Or rs.EOF) Then GoTo PROC_EXIT
Set ftiSuperclass = New FilingTemplateInterface
ftiSuperclass.ShowWorkbook rs!Directory & frm!Filename
PROC_EXIT:
On Error Resume Next
rs.Close
Set rs = Nothing
ftiSuperclass.QuitExcel
Set ftiSuperclass = Nothing
Exit Sub
PROC_ERROR:
Resume PROC_EXIT
End Sub
Friend Sub ShowWorkbook(strFilename As String)
Dim fso As New Scripting.FileSystemObject
Dim appExcel As New Excel.Application
appExcel.Workbooks.Open Filename:=strFilename, AddToMRU:=True
appExcel.visible = True
Set appExcel = Nothing
End Sub
Resolved by adding the line of code noted below, to force connection refresh on open:
Friend Sub ShowWorkbook(strFilename As String)
Dim fso As New Scripting.FileSystemObject
Dim appExcel As New Excel.Application
appExcel.Workbooks.Open Filename:=strFilename, AddToMRU:=True
appExcel.ActiveWorkbook.Connections("ConnectionName").Refresh 'added this line
appExcel.visible = True
Set appExcel = Nothing
End Sub

Resources