Jump to content

Remove figure from the open workbook Excel


Recommended Posts

You need two variables. One holding the string ($s...) of the workbook name and the other ($o...) holding the workbook object as returned by _Excel_BookAttach.

#include <Excel.au3>
#include <MsgBoxConstants.au3>

Local $sWorkBook = "1.xls"
Locla $oExcel = _Excel_Open()
If @error Then Exit Msgbox($MB_ICONERROR, "Error", "Error creating Excel object! @error = " & @error & ", @extended = " & @extended)
Local $oWorkBook = _Excel_BookAttach($sWorkbook, "title")
If @error Then Exit Msgbox($MB_ICONERROR, "Error", "Error attaching to the workbook! @error = " & @error & ", @extended = " & @extended)
$oWorkBook.ActiveSheet.Shapes.Range("Oval 1").Delete
If @error Then Exit Msgbox($MB_ICONERROR, "Error", "Error deleting shape! @error = " & @error & ", @extended = " & @extended)

 

Edited by water

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2024-07-28 - Version 1.6.3.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 (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 comment
Share on other sites

waterI'm using your script.

#include <Excel.au3>
#include <MsgBoxConstants.au3>

Local $sWorkBook = "1.xls"
Local $oExcel = _Excel_Open()
If @error Then Exit Msgbox($MB_ICONERROR, "Error", "Error creating Excel object! @error = " & @error & ", @extended = " & @extended)
Local $oWorkBook = _Excel_BookAttach($sWorkbook, "title")
If @error Then Exit Msgbox($MB_ICONERROR, "Error", "Error attaching to the workbook! @error = " & @error & ", @extended = " & @extended)
$oWorkBook.ActiveSheet.Shapes.Range("Oval 1").Delete
If @error Then Exit Msgbox($MB_ICONERROR, "Error", "Error deleting shape! @error = " & @error & ", @extended = " & @extended)

The script still can't find the window "1.xls".

Shows the error "Error attaching to the workbook". 

Link to comment
Share on other sites

At the moment I have no Windows sytem available for testing. So we need to play a bit.
How about this:

#include <Excel.au3>
#include <MsgBoxConstants.au3>

Local $sWorkBook = "1.xls"
Local $oExcel = _Excel_Open()
If @error Then Exit Msgbox($MB_ICONERROR, "Error", "Error creating Excel object! @error = " & @error & ", @extended = " & @extended)
Local $oWorkBook = _Excel_BookAttach($sWorkbook, "filename")
If @error Then Exit Msgbox($MB_ICONERROR, "Error", "Error attaching to the workbook! @error = " & @error & ", @extended = " & @extended)
$oWorkBook.ActiveSheet.Shapes.Range("Oval 1").Delete
If @error Then Exit Msgbox($MB_ICONERROR, "Error", "Error deleting shape! @error = " & @error & ", @extended = " & @extended)

 

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2024-07-28 - Version 1.6.3.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 (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 comment
Share on other sites

1 hour ago, LazyVasily said:

Why do you write about "filename" ?

because it does not matter how we access the workbook - as long as it works :)
Maybe tomorrow I have access to a Windows system and will then post a working example.

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2024-07-28 - Version 1.6.3.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 (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 comment
Share on other sites

The example script I posted above runs without error when I manually open the "1.xls" (not XLSX) workbook which is stored in the same directory as the script.
Can you please verify that this is true for you as well?


I run AutoIt 3.3.14.4 with Office 2016 on a Windows 7 system.

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2024-07-28 - Version 1.6.3.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 (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 comment
Share on other sites

2 hours ago, water said:

The example script I posted above runs without error when I manually open the "1.xls"

Confirming the same works perfectly for me.  Can also work thru _Excel_BookList ($oExcel) !

Link to comment
Share on other sites

@Lazy Why do you write about "filename" ?  This is as used in _Excel_BookAttach. Look at the help example.

Tested and working as requested:

#include <Excel.au3>
#include <MsgBoxConstants.au3>

Local $sWorkBook = "1.xls"
Local $oExcel = _Excel_Open()
If @error Then Exit Msgbox($MB_ICONERROR, "Error", "Error creating Excel object! @error = " & @error & ", @extended = " & @extended)
Local $oWorkbook = _Excel_BookOpen($oExcel, @ScriptDir & "\1.xls")
If @error Then
    MsgBox($MB_SYSTEMMODAL, "Excel UDF: _Excel_BookAttach Example", "Error opening workbook '" & @ScriptDir & "\1.xls'." & @CRLF & "@error = " & @error & ", @extended = " & @extended)
    _Excel_Close($oExcel)
    Exit
EndIf
Local $oWorkBook = _Excel_BookAttach($sWorkbook, "filename")
If @error Then Exit Msgbox($MB_ICONERROR, "Error", "Error attaching to the workbook! @error = " & @error & ", @extended = " & @extended)
$oWorkBook.ActiveSheet.Shapes.Range("Oval 1").Delete
If @error Then Exit Msgbox($MB_ICONERROR, "Error", "Error deleting shape! @error = " & @error & ", @extended = " & @extended)

 

Link to comment
Share on other sites

JoHanatCentYour script does not work.

The script opens an additional copy of file 1.xls (read-only).

In this copy - it removes the shape "Oval 1".

The script then stops running.

As a result - I have one open file 1.xls with two shapes, and one open file 1.xls (read-only) with one shape.

No need to open file 1.xls because it's already open.

You used the "filename" parameter again.

I asked about using $sMode "Title".

(https://www.autoitscript.com/autoit3/docs/libfunctions/_Excel_BookAttach.htm )

Link to comment
Share on other sites

@LazyVasily - You should keep your variables unique. Now you have both the same, so you did not understand what water was telling you.

You had your two different variables the wrong way round.

Instead of -

$oWorkBook = _Excel_BookAttach($sWorkbook, "title")
$oWorkBook.ActiveSheet.Shapes.Range("Oval 1").Delete

It should have been -

$sWorkBook = _Excel_BookAttach($oWorkbook, "Title")
$sWorkBook.ActiveSheet.Shapes.Range("Oval 1").Delete

As you had already declared $oWorkbook as the Title variable with this line -

Local $oWorkBook = "1.xls"

Or you could have just changed that instead, to -

Local $sWorkBook = "1.xls"

Please note, that I have also made "title" as propercase "Title", as case might be important ... not sure. It is propercase (or titlecase) in the Help file anyway.

Have you tried specifying the full path and using the FilePath option instead? _Excel_BookAttach
Get at least one variant working and then you can help troubleshoot why the other isn't working.

Never mind, I was referring to posts on Page 1. This is the second time that an additional Page has not been shown to me. Something screwy going on. I'd scrolled right down to see if you had been replied to. Now that I recollect it did seem odd that your last reply was Wednesday.

Either that or I am losing my marbles. :lmao:

Edited by TheSaint

Make sure brain is in gear before opening mouth!
Remember, what is not said, can be just as important as what is said.

Spoiler

What is the Secret Key? Life is like a Donut

If I put effort into communication, I expect you to read properly & fully, or just not comment.
Ignoring those who try to divert conversation with irrelevancies.
If I'm intent on insulting you or being rude, I will be obvious, not ambiguous about it.
I'm only big and bad, to those who have an over-active imagination.

I may have the Artistic Liesense ;) to disagree with you. TheSaint's Toolbox (be advised many downloads are not working due to ISP screwup with my storage)

userbar.png

Link to comment
Share on other sites

14 minutes ago, TheSaint said:

Please note, that I have also made "title" as propercase "Title", as case might be important ... not sure. It is propercase (or titlecase) in the Help file anyway.

The case of the parameter does not matter as AutoIt works case-insensitive ;)

Edited by water

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2024-07-28 - Version 1.6.3.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 (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 comment
Share on other sites

3 hours ago, water said:

The case of the parameter does not matter as AutoIt works case-insensitive ;)

One of those things I keep forgetting. It always throws me when the Help file shows propercase instead of lowercase, which logically should be the default. Propercase or Uppercase should only be shown when they are actually required. A small failing of our wonderful Help file.

Thanks for the reminder. :D

Make sure brain is in gear before opening mouth!
Remember, what is not said, can be just as important as what is said.

Spoiler

What is the Secret Key? Life is like a Donut

If I put effort into communication, I expect you to read properly & fully, or just not comment.
Ignoring those who try to divert conversation with irrelevancies.
If I'm intent on insulting you or being rude, I will be obvious, not ambiguous about it.
I'm only big and bad, to those who have an over-active imagination.

I may have the Artistic Liesense ;) to disagree with you. TheSaint's Toolbox (be advised many downloads are not working due to ISP screwup with my storage)

userbar.png

Link to comment
Share on other sites

I have started a thread in the MVP forum to discuss this (and another) issue with the Excel (and maybe the Word) UDF.
Thanks for the heads up :)

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2024-07-28 - Version 1.6.3.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 (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 comment
Share on other sites

TheSaintDid everything you told me to do.

#include <Excel.au3>
#include <MsgBoxConstants.au3>

Local $sWorkBook = "1.xls"
Local $oExcel = _Excel_Open()
If @error Then Exit Msgbox($MB_ICONERROR, "Error", "Error creating Excel object! @error = " & @error & ", @extended = " & @extended)
Local $oWorkBook = _Excel_BookAttach($sWorkbook, "Title")
If @error Then Exit Msgbox($MB_ICONERROR, "Error", "Error attaching to the workbook! @error = " & @error & ", @extended = " & @extended)
$oWorkBook.ActiveSheet.Shapes.Range("Oval 1").Delete
If @error Then Exit Msgbox($MB_ICONERROR, "Error", "Error deleting shape! @error = " & @error & ", @extended = " & @extended)

This script does not work.

Displays a window with the error: "Error attaching to the workbook! @error=1, extended = -2147352570"

Link to comment
Share on other sites

When we try to attach to a workbook, it works. When you do, it doesn't.
So we need more information:

  • Which version of Excel do you run?
  • Which version of AutoIt do you run?
  • Can you post your workbook so we can try in our environment?

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2024-07-28 - Version 1.6.3.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 (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 comment
Share on other sites

water,

4 hours ago, water said:

Which version of Excel do you run?

Excel 2013x64 and Excel2007

Autoit 3.3.14.0

Again specify. File 1.xls-not started with a click.

And at the time of running the script file 1.xls is ALREADY open.

 

The script should find the window-not by the file path, but by the name "Title".

1.xls

Link to comment
Share on other sites

To work with the x64 version of Excel your AutoIt script needs to be compiled for 64 bit as well.
Could you please give this a try?

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2024-07-28 - Version 1.6.3.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 (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 comment
Share on other sites

Your title text is wrong isn't it? Wouldn't it be "1.xls - Excel", or even "1.xls  [Compatibility Mode] - Excel" if opened in a newer version of Excel? From what I can see the UDF doesn't do partial text matches, it's looking for the full actual title of the window.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...