Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Find everything you need to get certified on Fabric—skills challenges, live sessions, exam prep, role guidance, and more. Get started

Reply
smpa01
Super User
Super User

Automate printing after data refresh

Hi,
Is it possible to automate printing the outputs as soon as the query gets refreshed. I process an input using power query and generate the output in excel. I want my outputs to get printed automatically everytime they get refreshed.

Is it possible to achieve?

Thank you in advance.
Did I answer your question? Mark my post as a solution!
Proud to be a Super User!
My custom visualization projects
Plotting Live Sound: Viz1
Beautiful News:Viz1, Viz2, Viz3
Visual Capitalist: Working Hrs
1 ACCEPTED SOLUTION
smpa01
Super User
Super User

I managed to reach a solution by using Macro. Not sure if this can be achieved by any other way.

 

The task was to import an input table into PQWRY and generating two outputs and print them upon refresh.

 

Sheet 1 contains Raw data and Sheet 2 and 3 contains output generated by PQWRY- https://drive.google.com/open?id=0B7imabOIHE8QTTFBLWw4aFU5dzg

 

Sub Macro1()
'
' Macro1 Macro
'

'
    With ActiveWorkbook.Connections("Query - Table1").OLEDBConnection
        .BackgroundQuery = False
        .CommandText = Array("SELECT * FROM [Table1]")
        .CommandType = xlCmdSql
        .Connection = _
        "OLEDB;Provider=Microsoft.Mashup.OleDb.1;Data Source=$Workbook$;Location=Table1;Extended Properties="""""
        .RefreshOnFileOpen = False
        .SavePassword = False
        .SourceConnectionFile = ""
        .SourceDataFile = ""
        .ServerCredentialsMethod = xlCredentialsMethodIntegrated
        .AlwaysUseConnectionFile = False
    End With
    With ActiveWorkbook.Connections("Query - Table1")
        .Name = "Query - Table1"
        .Description = "Connection to the 'Table1' query in the workbook."
    End With
    With ActiveWorkbook.Connections("Query - Table1 (2)").OLEDBConnection
        .BackgroundQuery = False
        .CommandText = Array("SELECT * FROM [Table1 (2)]")
        .CommandType = xlCmdSql
        .Connection = _
        "OLEDB;Provider=Microsoft.Mashup.OleDb.1;Data Source=$Workbook$;Location=Table1 (2);Extended Properties="""""
        .RefreshOnFileOpen = False
        .SavePassword = False
        .SourceConnectionFile = ""
        .SourceDataFile = ""
        .ServerCredentialsMethod = xlCredentialsMethodIntegrated
        .AlwaysUseConnectionFile = False
    End With
    With ActiveWorkbook.Connections("Query - Table1 (2)")
        .Name = "Query - Table1 (2)"
        .Description = "Connection to the 'Table1 (2)' query in the workbook."
    End With
    ActiveWorkbook.RefreshAll
    Sheets("Sheet2").Select
    ActiveWindow.SelectedSheets.PrintOut Copies:=1, Collate:=True, _
        IgnorePrintAreas:=False
    Sheets("Sheet3").Select
    Range("Table1__2[[#Headers],[Name]]").Select
    ActiveWindow.SelectedSheets.PrintOut Copies:=1, Collate:=True, _
        IgnorePrintAreas:=False
End Sub

If there any more ways to achieve I would be very intersted to know.

Did I answer your question? Mark my post as a solution!
Proud to be a Super User!
My custom visualization projects
Plotting Live Sound: Viz1
Beautiful News:Viz1, Viz2, Viz3
Visual Capitalist: Working Hrs

View solution in original post

1 REPLY 1
smpa01
Super User
Super User

I managed to reach a solution by using Macro. Not sure if this can be achieved by any other way.

 

The task was to import an input table into PQWRY and generating two outputs and print them upon refresh.

 

Sheet 1 contains Raw data and Sheet 2 and 3 contains output generated by PQWRY- https://drive.google.com/open?id=0B7imabOIHE8QTTFBLWw4aFU5dzg

 

Sub Macro1()
'
' Macro1 Macro
'

'
    With ActiveWorkbook.Connections("Query - Table1").OLEDBConnection
        .BackgroundQuery = False
        .CommandText = Array("SELECT * FROM [Table1]")
        .CommandType = xlCmdSql
        .Connection = _
        "OLEDB;Provider=Microsoft.Mashup.OleDb.1;Data Source=$Workbook$;Location=Table1;Extended Properties="""""
        .RefreshOnFileOpen = False
        .SavePassword = False
        .SourceConnectionFile = ""
        .SourceDataFile = ""
        .ServerCredentialsMethod = xlCredentialsMethodIntegrated
        .AlwaysUseConnectionFile = False
    End With
    With ActiveWorkbook.Connections("Query - Table1")
        .Name = "Query - Table1"
        .Description = "Connection to the 'Table1' query in the workbook."
    End With
    With ActiveWorkbook.Connections("Query - Table1 (2)").OLEDBConnection
        .BackgroundQuery = False
        .CommandText = Array("SELECT * FROM [Table1 (2)]")
        .CommandType = xlCmdSql
        .Connection = _
        "OLEDB;Provider=Microsoft.Mashup.OleDb.1;Data Source=$Workbook$;Location=Table1 (2);Extended Properties="""""
        .RefreshOnFileOpen = False
        .SavePassword = False
        .SourceConnectionFile = ""
        .SourceDataFile = ""
        .ServerCredentialsMethod = xlCredentialsMethodIntegrated
        .AlwaysUseConnectionFile = False
    End With
    With ActiveWorkbook.Connections("Query - Table1 (2)")
        .Name = "Query - Table1 (2)"
        .Description = "Connection to the 'Table1 (2)' query in the workbook."
    End With
    ActiveWorkbook.RefreshAll
    Sheets("Sheet2").Select
    ActiveWindow.SelectedSheets.PrintOut Copies:=1, Collate:=True, _
        IgnorePrintAreas:=False
    Sheets("Sheet3").Select
    Range("Table1__2[[#Headers],[Name]]").Select
    ActiveWindow.SelectedSheets.PrintOut Copies:=1, Collate:=True, _
        IgnorePrintAreas:=False
End Sub

If there any more ways to achieve I would be very intersted to know.

Did I answer your question? Mark my post as a solution!
Proud to be a Super User!
My custom visualization projects
Plotting Live Sound: Viz1
Beautiful News:Viz1, Viz2, Viz3
Visual Capitalist: Working Hrs

Helpful resources

Announcements
July 2024 Power BI Update

Power BI Monthly Update - July 2024

Check out the July 2024 Power BI update to learn about new features.

PBI_Carousel_NL_June

Fabric Community Update - June 2024

Get the latest Fabric updates from Build 2024, key Skills Challenge voucher deadlines, top blogs, forum posts, and product ideas.

Top Solution Authors