Jump to content

Paste from SSMS or Visual Studio into OneNote with formatting


Go to solution Solved by DanielEms,

Recommended Posts

When I copy code from Visual Studio or SQL Server Management Studio (SSMS) and paste it into OneNote, it loses all formatting.

Here's how it originally looks in SSMS:

ssms_code.png.21f7111afad8ac9ab04a1af1c7b08373.png

 

Here's how it looks when pasted into OneNote no matter which Paste Option I select:

pasted_from_ssms.png.a1a0c0b19db337e70dd9a294293453d4.png

 

My workaround is to copy from SSMS and paste into a blank Word document, then copy from Word and paste into OneNote.

I'd like to use AutoIt override ctrl-v in OneNote to paste with the formatting, but I can't figure out how.

I've looked through the documentation for ClipGet, ClipSet, _ClipBoard_SetData, and _ClipBoard_GetData

Here's the code I've tried so far.  I copy the text from SSMS, run the script, press ctrl-v in OneOne, but nothing pastes.  Any idea what i'm doing wrong?

#include <Clipboard.au3>

_Example()
Func _Example()

    Local $iClipboardFormat = _ClipBoard_RegisterFormat("Rich Text Format")
    Local $vClip = _ClipBoard_GetData($iClipboardFormat)
    _ClipBoard_SetData($vClip, $iClipboardFormat)

EndFunc

The clipboard program Ditto shows that the clipboard has 3 formats: CF_TEXT, CF_UNICODETEXT, and Rich Text Format

This script on the _ClipBoard_EnumFormats page shows the Clipboard has 7 formats and I've tried them all in the code above:

Clipboard formats ..: 7
Clipboard format 1 .: DataObject
Clipboard format 2 .: Unicode Text
Clipboard format 3 .: Rich Text Format
Clipboard format 4 .: Ole Private Data
Clipboard format 5 .: Locale
Clipboard format 6 .: Text
Clipboard format 7 .: OEM Text
 

ssms_code.png

pasted_from_ssms.png

Link to post
Share on other sites
  • 3 years later...
  • Solution

I ended up writing this function.  It essentially uses a word document to paste and re-copy with the formatting.  It's not super quick, but it works.

Func SSMSCopySQLWithFormatting()
    ClipPut("")    ; empty the clipboard
    _SendEx("^c")
    While 1        ; wait for the clipboard to be populated
        If ClipGet() <> "" Then ExitLoop
        sleep(5)
    WEnd
    ; no need to capture the clipboard to a variable

    SplashTextOn("Copying as RTF", "Please wait while the selection is copied and converted to RTF", 500, 100)
    ; the word UDF in AutoIt cannot handle the methods we need, so the Word COM is used directly
    Local $oWord = ObjCreate("Word.Application")
    $oWord.Documents.Add()              ; I cant figure out how to hide the word document, so it will flash briefly
    $oWord.Selection.Paste()            ; paste from the clipboard
    $oWord.Selection.WholeStory         ; select the whole document
    $oWord.Selection.Font.Size = 10     ; set the font to 10 pt
    $oWord.Selection.Copy()             ; copy all content
    $oWord.Documents.Close(0)           ; close without saving changes
    $oWord.Application.Quit()           ; quit word
    $oWord = 0
    SplashOff()
EndFunc

 

Link to post
Share on other sites
Posted (edited)


It bugged you since December 14th, 2018? Ouch!

Is the problem with the contents of the variable as it arrives to AutoIT from VS or SSMS, as it is stored on the clipboard by Windoze, or when it is pasted to OneNote?

Has the advent of Windows 10 and clipboard changes upset the underlying code presumptions?

PS: Do you close and destroy your Word object?

Edited by Confuzzled
Link to post
Share on other sites

 

On 5/26/2022 at 6:36 PM, DanielEms said:

; I cant figure out how to hide the word document, so it will flash briefly

Do not make the document invisible but the word application. Have a look at function _Word_Create ($oWord.Visible = False)

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...