No existing macros availble when Excel is opened from Outlook macro - excel

I have a macro in Outlook, and I have it opening an Excel file that is saved on my desktop. Once the file is open, I would like to run a macro that I have written in excel, but none of my excel macros are available. The macros are available whenever I open excel any other way, and macros are enabled when I open excel through outlook vba. My question is, how do I make these macros available when I open Excel via the Outlook macro? Please reference my code below.
'Pre:None
'Post:Excel will have been opened, and the macro "CreatePowerPoint()"
' will have been run on the excel document
Sub Gimba()
Dim xlApp As Object, xlWkb As Object
'open excel
Set xlApp = CreateObject("Excel.Application")
xlApp.Visible = True ' can be False if you do not wont see reaction,
' byt make sure is not fail
'Do not show any alerts
xlApp.DisplayAlerts = False
'open excel document
Set xlWkb = xlApp.Workbooks.Open(file path goes here)
'call macro on excel document
Call xlApp.Run("CreatePowerPoint")
End Sub

I assume that the macros that you have normally available are stored in your personal.xls workbook? If so, then you just need to load that up before you try and launch your CreatePowerPoint macro.
Try something like (depends on where your personal workbook is stored):
xlApp.Workbooks.Open ("C:\Documents and Settings\YourUserNameHere\Application Data\Microsoft\Excel\XLSTART\personal.xlsb")
As an aside, you might find it easier to write the VBA code if you use early binding. To do this, you need to add a reference to the Excel object model, then instead of using CreateObject, you could use Set xlApp = new Excel.Application. That way you get all the nice Intellitype assistance.

Related

Activating an Excel Workbook created by a Visio ShapeReport [duplicate]

So basically, I have a Visio file that has lots of shapes and data. I'm trying to create a shape report into a new excel file, and then have the excel file turn the exported data into a pivot table programmatically.
I have a macro running in Visio that generates the excel file with data in normal table form already. I want to be able to run a macro in Visio that activates the excel window of exported data and runs a macro to make it into a pivot table. However, any excel macro code I put into my visio macro modules is unrecognized (e.g. "Range"), presumably because they're not words recognized by Visio.
My question is this: How do I run a macro that alters an Excel file FROM a Visio module?
I know how to call a macro IN an Excel file from Visio (Excel.run "ModuleNAME"), but that would require the macro to already be in Excel. Since I'm creating new Excel files when I get shape reports, these files don't have any macros in them.
An alternative solution would be if I was able to export shape data report from Visio as a new sheet to an EXISTING Excel file that already contained the macro, but I'm not sure how to do this either... (export shape reports from Visio to existing Excel file)
My Code to Generate Excel Report:
Sub Excel2()
Visio.Application.Addons("VisRpt").Run
("/rptDefName=ReportDefinition_2.vrd/rptOutput=EXCEL")
End Sub
I would like to run this macro after running Excel2()
Sub NewMacro()
AppActivate "Microsoft Excel"
Dim AppExcel As Excel.Application
Set AppExcel = CreateObject("Excel.Application")
'Do all sorts of fancy stuff with making pivot tables
'Do all sorts of fancy stuff with making pivot tables
End Sub
You can just use the Get/CreateObject function to use an Excel application instance from Visio.
Have a look at this article:
http://msdn.microsoft.com/en-us/library/gg251785.aspx
You might also find this useful:
http://support.microsoft.com/kb/309603
...it describes the opposite direction (ie controling Visio from another app), but in your case you'd do something like:
Dim AppExcel As Excel.Application
On Error Resume Next
Set AppExcel = GetObject(, "excel.application")
If AppExcel Is Nothing Then
Set AppExcel = CreateObject("excel.application")
End If
Hope that helps.

Excel Macro Stored in Access

So I'm importing data every day into Access to use for reporting. The data comes from several spreadsheets created by different individuals. Because those individuals like to format things incorrectly I created a macro that reformats their document so that it can be imported cleanly into Access for me to use. Works great but it gets tedious having to open up each Excel sheet to run this Macro.
What I'm trying to do is place the Excel Macro in Access and then run the formatting code before importing it all at once. I am a bit lost in approaching this. I'm aware of ways to run Macros already placed in Excel sheets but is there a way to run a macro that is stored in Access that works in excel. I also thought to maybe inject the Macro into the excel document and then run it.
To sum things up, what I'm hoping to do is from Access, store a macro, that can be used to alter Excel Files.
Is this at all possible? If so How? Is there another approach?
What you are asking to do is automate Excel from Access. Yes, you can do this. In Access, add a module, add a reference to the Microsoft Excel object model (Tools: References), and use this framework code to get you started:
Sub PrepExcelFileForImport()
Dim xl As Excel.Application
Dim wbk As Excel.Workbook
Dim wst As Excel.Worksheet
Set xl = CreateObject("Excel.Application")
With xl
.Visible = True
Set wbk = .Workbooks.Open("c:\temp\temp.xlsx")
Set wst = wbk.Worksheets("data")
With wst
' add your formatting code here, be sure to use qualified references, e.g.
.Rows(1).Font.Bold = True
End With
End With
wbk.Close SaveChanges:=True
xl.Quit
End Sub

Generate Excel Spreadsheet with Macros using Microsoft Access

While generating hundreds of Office Excel spreadsheets with Office Access is certainly possible, it would be great to add macros to the generated workbooks.
I would like to add the functions to the object "ThisWorkbook" in the VBA project for each spreadsheet on generation. How would one go about doing this?
Thank you in advance!
Under the assumption that the macro's in all generated workbooks are the same,
create a template containing all VBA code (and optionally constant text like headers, footers, print range definitions, etc. - i.e. "everything except data")
create any new workbook from the template
insert your data into the WB object
save as macro enabled worksheet (Excel 2007/2010)
close it
example
Sub CreateWB()
Dim WB As Workbook
Set WB = Workbooks.Add("MacroTemp.xltm") ' contains VBA, ActiveX, etc.
WB.Worksheets("Sheet1").[A1] = "co-cooo!" ' adding data
WB.SaveAs "MyGenWB", xlOpenXMLWorkbookMacroEnabled
WB.Close
End Sub
In Excel 2007/2010 do not forget to save the template as macro enabled template (*.xltm").

Find if workbook was opened using VBA or by user

I have a macro workbook Excel 2007 with a workbook open event.
Inside the workbook open event is it possible for me find if the workbook was opened by the user from windows or from another workbook using vba...
I am not sure if it fits your needs, but you can open a workbook in your VBA code without firing the events:
'Disable Events.
Application.EnableEvents = False
'Open your book.
Workbooks.open(Filename)
'Enable Events.
Application.EnableEvents = True
If this suits your purpose:
When opening with vba, change the name of the workbook before opening. Then change it back when done. Workbook_Open procedure just needs to check for the name of the workbook it is in.
To change the name of the workbook
you can open it, then use SaveAs method to close it again with the new name
use the Shell function
use a Microsoft Scripting Runtime object.

VBA Macros: Exporting Visio Shape Report into New Excel File, then Creating a Pivot Table

So basically, I have a Visio file that has lots of shapes and data. I'm trying to create a shape report into a new excel file, and then have the excel file turn the exported data into a pivot table programmatically.
I have a macro running in Visio that generates the excel file with data in normal table form already. I want to be able to run a macro in Visio that activates the excel window of exported data and runs a macro to make it into a pivot table. However, any excel macro code I put into my visio macro modules is unrecognized (e.g. "Range"), presumably because they're not words recognized by Visio.
My question is this: How do I run a macro that alters an Excel file FROM a Visio module?
I know how to call a macro IN an Excel file from Visio (Excel.run "ModuleNAME"), but that would require the macro to already be in Excel. Since I'm creating new Excel files when I get shape reports, these files don't have any macros in them.
An alternative solution would be if I was able to export shape data report from Visio as a new sheet to an EXISTING Excel file that already contained the macro, but I'm not sure how to do this either... (export shape reports from Visio to existing Excel file)
My Code to Generate Excel Report:
Sub Excel2()
Visio.Application.Addons("VisRpt").Run
("/rptDefName=ReportDefinition_2.vrd/rptOutput=EXCEL")
End Sub
I would like to run this macro after running Excel2()
Sub NewMacro()
AppActivate "Microsoft Excel"
Dim AppExcel As Excel.Application
Set AppExcel = CreateObject("Excel.Application")
'Do all sorts of fancy stuff with making pivot tables
'Do all sorts of fancy stuff with making pivot tables
End Sub
You can just use the Get/CreateObject function to use an Excel application instance from Visio.
Have a look at this article:
http://msdn.microsoft.com/en-us/library/gg251785.aspx
You might also find this useful:
http://support.microsoft.com/kb/309603
...it describes the opposite direction (ie controling Visio from another app), but in your case you'd do something like:
Dim AppExcel As Excel.Application
On Error Resume Next
Set AppExcel = GetObject(, "excel.application")
If AppExcel Is Nothing Then
Set AppExcel = CreateObject("excel.application")
End If
Hope that helps.

Resources