Copy picture form excel to onenote with VBA

周林 陈 20 Reputation points
2023-07-24T04:42:39.26+00:00

Hello,

I want to copy the picture to onenote used by VBA. But I only get the way that transform picture into BASE64. Is there have any other way?

Wait you feedback, thanks.

OneNote
OneNote
A family of Microsoft products that enable users to capture, organize, and reuse notes electronically.
141 questions
Office Development
Office Development
Office: A suite of Microsoft productivity software that supports common business tasks, including word processing, email, presentations, and data management and analysis.Development: The process of researching, productizing, and refining new or existing technologies.
3,307 questions
Excel Management
Excel Management
Excel: A family of Microsoft spreadsheet software with tools for analyzing, charting, and communicating data.Management: The act or process of organizing, handling, directing or controlling something.
1,618 questions
0 comments No comments
{count} votes

Accepted answer
  1. AllenXu-MSFT 14,661 Reputation points Microsoft Vendor
    2023-07-24T07:52:25.52+00:00

    Hi @周林 陈,

    Yes, there is another option to copy a picture from Excel to OneNote using VBA. You can use the CopyPicture method to copy the picture as it appears in Excel, and then paste it into OneNote.

    Here's an example code snippet to get you started:

    Sub CopyExcelPictureToOneNote()
    
        ' Copy the picture in Sheet1 to the clipboard
        Sheet1.Range("A1:D5").CopyPicture xlScreen, xlBitmap
    
        ' Get a reference to the OneNote application
        Dim onApp As OneNote.Application
        Set onApp = CreateObject("OneNote.Application")
    
        ' Open a new page in the default notebook
        Dim onSection As OneNote.Section
        Set onSection = onApp.GetHierarchy(Null, hsPages, False).Pages(1).ParentSection
        Dim onPage As OneNote.Page
        Set onPage = onSection.AddPage(Null)
    
        ' Paste the picture into the page
        onPage.Placeholder(Null, False).Select
        onApp.ActiveWindow.CurrentPage.Paste
        onApp.ActiveWindow.CurrentPage.RefreshContent
    
    End Sub
    

    In this example, the CopyPicture method is used to copy the range "A1:D5" as it appears on the screen (using the xlScreen and xlBitmap arguments). The picture is then pasted into a new page in OneNote using the Paste method. Note that this code requires a reference to the OneNote type library, which you can add in the VBA editor by going to Tools > References and selecting "Microsoft OneNote 16.0 Object Library" (or a similar version, depending on your installation).


    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful