News:

BB code in posts seems to be working again!
I haven't turned on every single tag, so please let me know if there are any that are used/needed but not activated.

Main Menu

VBA to save as PDF and PNG

Started by miless2111s, September 17, 2024, 12:06:10 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

miless2111s

I have a number of visio files that I update and then:
* Select everything that is on the page and avoid anything that is off the page
* File / save as / PNG, save I want to oversave, OK to the default settings
* File / save as / PDF, remove the option to open
* File close and save on the way out.

This is a simple process but when I am updating 10's of files it would be nice to have a macro which does it.

Looking around it doesn't seem to be a simple process :(  Any help you can suggest would be gratefully received. :)

TIA

Miles

miless2111s

Update:   
Having used record I got the following but trying to run it again caused it to fail at the "Application.ActiveDocument.ServerPublishOptions.SetPagesToPublish visPublishPageAll, PageNamesU, visLangUniversal" line.
Sub Test_save()

    'Enable diagram services
    Dim DiagramServices As Integer
    DiagramServices = ActiveDocument.DiagramServicesEnabled
    ActiveDocument.DiagramServicesEnabled = visServiceVersion140 + visServiceVersion150

    Application.Settings.SetRasterExportResolution visRasterUseScreenResolution, 144#, 144#, visRasterPixelsPerInch
    Application.Settings.SetRasterExportSize visRasterFitToSourceSize, 3.486111, 2.819444, visRasterInch
    Application.Settings.RasterExportDataFormat = visRasterInterlace
    Application.Settings.RasterExportColorFormat = visRaster24Bit
    Application.Settings.RasterExportRotation = visRasterNoRotation
    Application.Settings.RasterExportFlip = visRasterNoFlip
    Application.Settings.RasterExportBackgroundColor = 16777215
    Application.Settings.RasterExportTransparencyColor = 16777215
    Application.Settings.RasterExportUseTransparencyColor = False
    ActiveWindow.DeselectAll
    ActiveWindow.Select Application.ActiveWindow.Page.Shapes.ItemFromID(1), visSelect
    ActiveWindow.Select Application.ActiveWindow.Page.Shapes.ItemFromID(2), visSelect
    ActiveWindow.Select Application.ActiveWindow.Page.Shapes.ItemFromID(3), visSelect
    Application.ActiveWindow.Selection.Export "\\baestnodc001v04\Frimley_miles.goodchild$\Documents\SummaryPro\Drawing1.png"

    Application.ActiveDocument.ExportAsFixedFormat visFixedFormatPDF, "\\baestnodc001v04\Frimley_miles.goodchild$\Documents\SummaryPro\Drawing1.pdf", visDocExIntentPrint, visPrintAll, 1, 1, False, True, True, True, False

    Dim PageNamesU() As String
    Application.ActiveDocument.ServerPublishOptions.SetPagesToPublish visPublishPageAll, PageNamesU, visLangUniversal
    Dim RecordsetIDs() As Long
    Application.ActiveDocument.ServerPublishOptions.SetRecordsetsToPublish visPublishDataRecordsetAll, RecordsetIDs
    Application.ActiveDocument.SaveAsEx "\\baestnodc001v04\Frimley_miles.goodchild$\Documents\SummaryPro\test.vsdx", visSaveAsWS + visSaveAsListInMRU

    'Restore diagram services
    ActiveDocument.DiagramServicesEnabled = DiagramServices

End Sub
I trimmed out some lines and it seemed to start working:
Sub test_save2()

    'Enable diagram services
    Dim DiagramServices As Integer
    DiagramServices = ActiveDocument.DiagramServicesEnabled
    ActiveDocument.DiagramServicesEnabled = visServiceVersion140 + visServiceVersion150

'gather file name


'save as PNG
    Application.Settings.SetRasterExportResolution visRasterUseScreenResolution, 144#, 144#, visRasterPixelsPerInch
    Application.Settings.SetRasterExportSize visRasterFitToSourceSize, 3.486111, 2.819444, visRasterInch
    Application.Settings.RasterExportDataFormat = visRasterInterlace
    Application.Settings.RasterExportColorFormat = visRaster24Bit
    Application.Settings.RasterExportRotation = visRasterNoRotation
    Application.Settings.RasterExportFlip = visRasterNoFlip
    Application.Settings.RasterExportBackgroundColor = 16777215
    Application.Settings.RasterExportTransparencyColor = 16777215
    Application.Settings.RasterExportUseTransparencyColor = False

    Application.ActiveWindow.Selection.Export "\\baestnodc001v04\Frimley_miles.goodchild$\Documents\SummaryPro\test.png"
'save as PDF
    Application.ActiveDocument.ExportAsFixedFormat visFixedFormatPDF, "\\baestnodc001v04\Frimley_miles.goodchild$\Documents\SummaryPro\test.pdf", visDocExIntentPrint, visPrintAll, 1, 1, False, True, True, True, False


'Restore diagram services
    ActiveWindow.DeselectAll
    ActiveDocument.DiagramServicesEnabled = DiagramServices

End Sub

I wish I knew why it was able to work with the elements removed; they look very technical and it worries me that they don't seem to be needed :)

Now to edit it so that I can gather the file name and make it work for any file and not just "test" and save the macro to a stencil so that the files don't have to be macro enabled. :)

miless2111s

The final code in case it is useful to anyone else:
Sub save_and_close()
'saves the selected items as PNG, then PDF and then closes the diagram with a save.

'Enable diagram services
    Dim DiagramServices As Integer
    DiagramServices = ActiveDocument.DiagramServicesEnabled
    ActiveDocument.DiagramServicesEnabled = visServiceVersion140 + visServiceVersion150

'add error handing and file freeze (if required to speed up)
On Error GoTo finalise


'gather file name
    Dim File_name As String
    full_file_name = ActiveDocument.Name
    File_name = Left(full_file_name, Len(full_file_name) - 5)
   
    Dim path As String
    path = ActiveDocument.path

'save as PNG
    Application.Settings.SetRasterExportResolution visRasterUseScreenResolution, 144#, 144#, visRasterPixelsPerInch
    Application.Settings.SetRasterExportSize visRasterFitToSourceSize, 3.486111, 2.819444, visRasterInch
    Application.Settings.RasterExportDataFormat = visRasterInterlace
    Application.Settings.RasterExportColorFormat = visRaster24Bit
    Application.Settings.RasterExportRotation = visRasterNoRotation
    Application.Settings.RasterExportFlip = visRasterNoFlip
    Application.Settings.RasterExportBackgroundColor = 16777215
    Application.Settings.RasterExportTransparencyColor = 16777215
    Application.Settings.RasterExportUseTransparencyColor = False

    'Application.ActiveWindow.Selection.Export "\\baestnodc001v04\Frimley_miles.goodchild$\Documents\SummaryPro\test.png"
    Application.ActiveWindow.Selection.Export path & File_name & ".png"
'save as PDF
    'Application.ActiveDocument.ExportAsFixedFormat visFixedFormatPDF, "\\baestnodc001v04\Frimley_miles.goodchild$\Documents\SummaryPro\test.pdf", visDocExIntentPrint, visPrintAll, 1, 1, False, True, True, True, False
    Application.ActiveDocument.ExportAsFixedFormat visFixedFormatPDF, path & File_name & ".pdf", visDocExIntentPrint, visPrintAll, 1, 1, False, True, True, True, False

'Restore diagram services
    ActiveWindow.DeselectAll
    ActiveDocument.DiagramServicesEnabled = DiagramServices

'save and close file
    ActiveDocument.Save
    ActiveDocument.Close 'savechanges:=True, FileName:=path & full_file_name
    Exit Sub
finalise:
    ActiveWindow.DeselectAll
    ActiveDocument.DiagramServicesEnabled = DiagramServices
    MsgBox "there was an error"
End Sub


Any improvements or suggests gratefully received :)

Browser ID: smf (possibly_robot)
Templates: 4: index (default), Display (default), GenericControls (default), GenericControls (default).
Sub templates: 6: init, html_above, body_above, main, body_below, html_below.
Language files: 4: index+Modifications.english (default), Post.english (default), Editor.english (default), Drafts.english (default).
Style sheets: 4: index.css, attachments.css, jquery.sceditor.css, responsive.css.
Hooks called: 170 (show)
Files included: 33 - 1308KB. (show)
Memory used: 1031KB.
Tokens: post-login.
Cache hits: 16: 0.00266s for 26,733 bytes (show)
Cache misses: 5: (show)
Queries used: 17.

[Show Queries]