Exporting Visio data as text via the clipboard!

Started by Visisthebest, February 20, 2022, 11:48:53 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Visisthebest

I was looking for the easiest way, with minimal code and fuss, to export CSV text (comma/other delimiter-separated data) out of Visio with some VBA code.

I needed data about shapes in a custom format for a specific solution, exporting data to a file would work here as well but this was even simpler. Nothing faster than copy-pasting between applications.

I found the code below, developed for Excel and fortunately it also works like a charm in Visio:


Function Clipboard(Optional StoreText As String) As String
'PURPOSE: Read/Write to Clipboard
'Source: ExcelHero.com (Daniel Ferry)

Dim x As Variant

'Store as variant for 64-bit VBA support
  x = StoreText

'Create HTMLFile Object
  With CreateObject("htmlfile")
    With .parentWindow.clipboardData
      Select Case True
        Case Len(StoreText)
          'Write to the clipboard
            .setData "text", x
        Case Else
          'Read from the clipboard (no variable passed through)
            Clipboard = .GetData("text")
      End Select
    End With
  End With

End Function


In the Visio VBA editor, via the Tools -> References menu add this item:
Microsoft HTML Object Library

because the code above needs it.

Then with this simple sub you can test putting text on the clipboard:


Sub ExampeMacro()

'Copy text to the clipboard
  Clipboard "I can copy to the Clipboard!"

'To read text from the clipboard:
  MsgBox Clipboard
 
End Sub


There may be even simpler ways to get data out of Visio quickly in the exact format you want (to Excel, Google Sheets etc), but this one works well for me!

Source : https://www.thespreadsheetguru.com/blog/2015/1/13/how-to-use-vba-code-to-copy-text-to-the-clipboard
Visio 2021 Professional