0

Using the "online" version of Microsoft Excel and its automation tool "Code Editor" I want to create a script which automatically selects a range and copies the values to the clipboard.

The script creation and the selection are already done, no need to discuss that please. :)

The main issue is the "copy to clipboard" aspect of the script. Everything I see online is either for VBA or just points me to the well known keyboard shortcut.

Can I have a keyboard sequence run from within the script? Is there an explicit command for copying to clipboard? Any advice? Maybe another approach? I feel like I am missing something obvious...

I've tried using workbook.getApplication.call but nothing seems to work.

I also attempted... Dim objData As New MSForms.DataObject objData.SetText txt objData.PutInClipboard

6
  • Are you able to access Power Automate via the Flows? If so, you can create a record to copy to clipboard and execute that flow via the scripts. Commented Oct 4, 2024 at 19:40
  • @Cyril Good thought but doesn't seem to work. I hit macro record, then select the desired cell, finally the "copy" button from the menu ribbon... but the "stop record" button remains ghosted. I've recorded other actions and is easy to use but for some reason the copy command is not recognized. Commented Oct 4, 2024 at 20:45
  • @LongTimeReaderFirstTimePoster so the Automate Work button should have predetermined Flows for Copy to Clipboard as the action. That's the only way I can think to get a copy to clipboard, considering Office Script is intended to interact with the cloud-based system and not utilize desktop functions, so should technically be improbable without some specific finagling which I am unaware. Commented Oct 4, 2024 at 20:53
  • Is there any specific reason why you need to copy the selection range to clipboard? Commented Oct 4, 2024 at 20:59
  • @taller The users requested that feature. A complex set of cells are part of the selection process using various criteria. Any comments on a solution? Commented Oct 4, 2024 at 21:50

1 Answer 1

0

My understanding is that in general you cannot use OfficeScript to copy to clipboard, as it's maintaining the data in the cloud. Take this copy script:

function main(workbook: ExcelScript.Workbook) {
    const rng:ExcelScript.Range = workbook.getSelectedRange();
    rng.getOffsetRange(1,1).copyFrom(rng);
};

Note that is copies to a defined range rng.getOffsetRange(1,1) from rng. To my knowledge, there is not a way to say CopyToClipboard.copyFrom(rng) within OfficeScript.

There exists a Flow (previous name to Power Automate) that allows CopyToClipboard, which are found via Automate Work.

Sign up to request clarification or add additional context in comments.

2 Comments

This copies from one cell to the next - not the clipboard. Offering solutions that are known not to be an answer can be misleading to the reader.
@LongTimeReaderFirstTimePoster the point was to show working code to indicate how office script works. I very clearly state "you cannot use OfficeScript to copy to clipboard" prior to the script, then explaining how the listed script works for its intended purpose, "not a way to say CopyToClipboard.copyFrom(rng) within OfficeScript". I then go on to explain an alternate path.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.