Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

How to exit an Excel spreadsheet and scripting help


  • Please log in to reply
64 replies to this topic
Mickers
  • Members
  • 1239 posts
  • Last active: Sep 25 2015 03:03 PM
  • Joined: 11 Oct 2010

Thanks again for the explanation. Being new to AHK and VBA, I have a lot to learn, and it sure feels great when it works.

Just so you understand most of the code you are using is NOT ahk or vba. It is based on COM properties/objects/methods that are available to all scripting/programming languages. If you understand how to use it in one you can faily easily translate it to another. :wink:

As for your overall goal that's probably possible. My suggestion, and it may not be the best answer, is to set up a file that the hotkey can access and each time a pro# is updated the file an addition to the file is made. Then at the end of the week run a seperate script that reads from the file and updates that second system. :idea:

sinkfaze
  • Moderators
  • 6367 posts
  • Last active: Nov 30 2018 08:50 PM
  • Joined: 18 Mar 2008

Just so you understand most of the code you are using is NOT ahk or vba. It is based on COM properties/objects/methods that are available to all scripting/programming languages.


:?:

I'm very confused by this statement. We're writing code in AHK, which uses COM to access the Excel object and manipulate its various objects/methods/properties via VBA syntax (or quasi-VBA syntax anyway). That means you have to understand how VBA works and how AHK works (specifically how it communicates with another application through COM) to make things go.

tank
  • Administrators
  • 4345 posts
  • AutoHotkey Foundation
  • Last active: May 02 2019 09:16 PM
  • Joined: 21 Dec 2007
I THINK he meant that the properties methods objects are not defined by ahk or VBA but instead by the application itself. AHK and or VBA or C++ etc provides a means to use COM which provides a means to interact with the internals of an application
Never lose.
WIN or LEARN.

Mickers
  • Members
  • 1239 posts
  • Last active: Sep 25 2015 03:03 PM
  • Joined: 11 Oct 2010
^ :wink:

BigMan
  • Members
  • 137 posts
  • Last active: Apr 04 2012 09:18 PM
  • Joined: 10 Nov 2011
I appreciate learning anything I can about any language. I guess that may be what confuses many new AHKer's, in that many examples they find on these posts contain a variety of coding, and many try to help by just posting things like "study up on COM" or "go learn some VBS", etc..

Any help with my big picture goal, please post. I want to learn this stuff but I need some clear examples......thanks to everyone.

Mickers
  • Members
  • 1239 posts
  • Last active: Sep 25 2015 03:03 PM
  • Joined: 11 Oct 2010
Here's the MSDNfor Excel COM I use all the time.

Try to learn how ahk and vba differ that makes it a lot easier to translate. Then just use the macro button in the view tab (office 2007) and record yoru actions. Then all you have to do is translate the vba to ahk and it saves you a lot of time looking for a specific property or method. :wink:

BigMan
  • Members
  • 137 posts
  • Last active: Apr 04 2012 09:18 PM
  • Joined: 10 Nov 2011
Thanks Mickers....I had previously looked through this, and like I said before, it just seemed to confuse me.
No doubt I will reference the info again and again, but right now I am trying to learn from my previous post and learn how to make changes to it to get where I am trying to go (big picture goal).

Sinkfaze has been most helpful with his coding help and explanations how the code is working. I get 10 times the learning from "real life" examples than I do just looking through manual after manual. No offense, everyone learns differently.

I'm sure, in time, I will be able to get more understanding out of the many manuals and libraries that are available. (I have saved links to them all)

So, any ideas, coding help, even starting small and then adding later, to get my big picture?
That is why my previous code was done, because it was in line with final big picture goal.

jethrow
  • Moderators
  • 2854 posts
  • Last active: May 17 2017 01:57 AM
  • Joined: 24 May 2009

Any help with my big picture goal, please post.

When you think of COM, think of Interfaces. Think of the interface of a car - that is - the dashboard, stearing wheel, etc. These controls allow you to interact with the internals of the car. You don't speak car, and the car doesn't speak english. However, the interface allows you to control the car - no matter what language you speak.

There are lots of vba/javascript examples on the internet. These languages use COM quite a bit - hence why learning some vba/javascript will help you learn COM in AHK. The syntax is almost identical.

Also, you need to have some knowledge of object oriented programming.

BigMan
  • Members
  • 137 posts
  • Last active: Apr 04 2012 09:18 PM
  • Joined: 10 Nov 2011
Good informational learning tools. I started on them this morning.
I tried to download COM.zip and go through the training, but keep getting the error : Call to nonexistent function - COM_Init()
Do I have COM loaded to the correct area?
C:\Program Files\AutoHotkey\Lib\COM.zip

jethrow
  • Moderators
  • 2854 posts
  • Last active: May 17 2017 01:57 AM
  • Joined: 24 May 2009
The code presented in this thread uses the Built-in COM functionality of AutoHotkey_L. If you are using AutoHotkey_L, you don't need the COM Library.

BigMan
  • Members
  • 137 posts
  • Last active: Apr 04 2012 09:18 PM
  • Joined: 10 Nov 2011
We are using "basic AHK"

Mickers
  • Members
  • 1239 posts
  • Last active: Sep 25 2015 03:03 PM
  • Joined: 11 Oct 2010
If your using any of the previously posted code in this thead than you can't be using basic as it's not compatible. You wouldn't be able to run past the first line.

Although I could be missing something. I do that sometimes. :wink:

BigMan
  • Members
  • 137 posts
  • Last active: Apr 04 2012 09:18 PM
  • Joined: 10 Nov 2011
here is my code:
#E::            ;;;;HOTKEY is window key and the E key
#Persistent     ;;;;leave HOTKEY available and running.  The ExitApp would kill the HOTKEY


FormatTime, Time, ,M/dd/yyyy h:mmtt     ;;;add this for time stamp but need it to append to pasted comment

TryAgain:
InputBox, PRO, PRO Number, Please enter PRO number.
If   ErrorLevel || !PRO   ;;;;If the user presses Cancel or enters nothing in the box
     Return

Path :=   "C:\Documents and Settings\xxx1234\My Documents\testcsv.xlsx" ;;;Document getting information from

;;;MsgBox, 262148, Get Information, Open File TESTCSV.XLSX     ;;;uncomment to create pop-up
;;;IfMsgBox No  ;;uncomment if MsgBox is uncommented for pop-up 
;;;Return       ;;leaves HOTKEY running and allows user to repeat process/use ExitApp to end Hotkey function

xl :=   ComObjCreate("Excel.Application")
Workbook :=   xl.Workbooks.Open(Path)

;;;;;;;;;;;;;;;;;;matches the entire cell to the entrire string;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
If   r :=   Workbook.ActiveSheet.Columns("A").Find[PRO,ComObjMissing(),ComObjMissing(),1].Row

{
;;;MsgBox, 262148, Copy, Comments for selected PRO # copied to Clipboard?   ;;;uncomment to create pop-up
;;;IfMsgBox Yes                                 ;;;uncomment if MsgBox is uncommented for pop-up
     Clipboard :=                               ;;;empties Clipboard
     Workbook.ActiveSheet.Range["B" r].Copy()   ;;;copies what corresponds in column B
     Send, "^v%time%"                           ;;;(ctrl'v') auto copy where cursor is & adds date/time stamp
     Return      ;;leaves HOTKEY running and allows user to repeat process/use ExitApp to end Hotkey function
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;**NOTE - If they are not in a screen, they can go to screen and hit ctrl+v;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 
      
}
else   
{    
   MsgBox, 48, Warning, PRO number not found.
     Workbook.Close(false), xl.Quit()
     Goto TryAgain
     ;Return      ;;leaves HOTKEY running and allows user to repeat process/use ExitApp to end Hotkey function
}
Return

Question on this is: Seems to all work correctly, except that after you run it through and test everything, and close the place where you are actually "pasting" the copied information (I was copying to another excel sheet); If you then double click any excel file, the "Path" file pops up along with the excel file you are opening. "Path" file is still hung up in a 'Read Only' state for awhile.
It eventually clears up and stops doing it, but how do you stop this from happening. You would think once you have successfully copied the cell information that the "Path" file would be free and this wouldn't happen.
Thoughts?

**Still accepting ideas or whatever on my picture goal.....

Mickers
  • Members
  • 1239 posts
  • Last active: Sep 25 2015 03:03 PM
  • Joined: 11 Oct 2010
I don't have an answer for that. Mabye someone else can shed some light on it.

I wonder what happens if you made some slight modifications:
#SingleInstance force
#Persistent
Path := "C:\Documents and Settings\" . A_UserName . "\My Documents\testcsv.xlsx" ; this will work for any user so you don't have to hard code a username

#E::
FormatTime, Time, ,M/dd/yyyy h:mmtt
If !xl
	xl := ComObjCreate("Excel.Application")

TryAgain:
InputBox, PRO, PRO Number, Please enter PRO number.
If (ErrorLevel) or (!PRO)
     return

Workbook := xl.Workbooks.Open(Path)

If (r := Workbook.ActiveSheet.Columns("A").Find[PRO, ComObjMissing(), ComObjMissing(), 1].Row) {
	Clipboard := ""
	Workbook.ActiveSheet.Range["B" r].Copy()
	Send, "^v%time%"
	xl.CutCopyMode := False ; removes cells from clipboard
} else {   
	MsgBox, 48, Warning, PRO number not found.
	Workbook.Close(false)
	goto, TryAgain
} return
Unable to test this but lemme know. :wink:

sinkfaze
  • Moderators
  • 6367 posts
  • Last active: Nov 30 2018 08:50 PM
  • Joined: 18 Mar 2008
You might check Task Manager to see if the Excel process is still out in memory before you attempt to open another spreadsheet.