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

爱⌒轻易说出口 提交于 2019-12-04 06:10:25

问题


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

回答1:


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.



来源:https://stackoverflow.com/questions/11729959/vba-macros-exporting-visio-shape-report-into-new-excel-file-then-creating-a-pi

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!