Solved

autoit excel manipulation

Posted on 2014-11-27
5
2,117 Views
Last Modified: 2014-12-02
Hello Experts,

 I've had to ask this question again as the administrators closed off the original question before I could test out the answer.

 Hello MatrixNZ,
 I'm hoping that you can repost your proposed code as that looked to be spot on.

  I'm a programming noob and I am attempting to open an existing excel file with autoit, then populate specific cells with data and then closing the file with a new name.

  My attempt is as follows:


  ;CodeStart

  #include <ExcelCOM_UDF.au3>  ; Include the function collection

  Local $sFilePath = "C:\Prod"
  Local $sFileTempPath = "C:\Temp"
  Local $File1 = "File1.xls"
  Local $File2 = "File2.xls"

  Local $oExcel = _ExcelBookOpen(1)

   _ExcelWriteCell($oExcel, "26/11/2014", "G41")                        ; Write the current date to cell G41

   ; Now we save it into the temp directory; overwrite existing file if necessary
  _ExcelBookSaveAs($oExcel, $FileTempPath & "temp.xls", "xls", 0, 1)

  ; And finally we close out
  _ExcelBookClose($oExcel)

  ;CodeFinish

  It compiles successfully but does not seem to do anything.
  I do not get a temp file in the directory.

  Thanks in advance.

  Cheers,
  Joe
0
Comment
Question by:joedelapaz
  • 2
  • 2
5 Comments
 
LVL 18

Expert Comment

by:SimonAdept
Comment Utility
Hi, are you determined to do this with autoit or would you be happy with an Excel template that does the same?

From what I can see you are opening a file, writing a date to a cell and then saving the file to the temp directory. Is that the process or am I missing something?
0
 

Author Comment

by:joedelapaz
Comment Utility
Hi Simon,
Here's the scenario.
I have received an excel form from a vendor. I in order to renew access for my guys, I/we have to fill out the form and submit it each month. The vendor then renews our access. Obviously, this is a painfully repetitive and silly process. All we really need to do is to automatically change the date on the form and email it to the vendor.
This gives us another month before we have to go through the same process again.

So the idea is to automatically open this spreadsheet each month, go to the appropriate cell and set a new application date. Then go to another cell and calculate application date plus 30 days. Then save the form and another script will automatically email it to the vendor.

This vendor form is full of macros and is heavily locked down. And as you can tell from their bureaucracy if we mess around with their form, they will reject it.

I'm happy to consider any workable option.

Cheers,
Joe
0
 
LVL 18

Expert Comment

by:SimonAdept
Comment Utility
Hi Joe, I don't have autoit, so I can't help with that. Whether doing it in Excel VBA is the right option will depend on your environment and how automated you want this to be. I don't know if autoit can run scheduled tasks to make this process run completely unattended and whether that would be advisable or not.

Here's the basic code (that I'd put in my personal startup workbook and either link to a button or wrap with some date logic in an auto_open() macro to determine if today is a good day to send a renewal.

It opens the form, updates the two date fields, saves a copy of the workbook to the temp directory and then generates a draft email to a hard-coded recipient. To send instead of displaying draft email, just change .display to .send

Sub UpdateWorkbookAndEmail()
Dim objOutlook As outlook.Application

    Workbooks.Open Filename:="\\[Your Path Here]\EE.xlsx"
    Range("D4").Value = DateValue(Now)
    Range("D12").Value = DateAdd("m", 1, DateValue(Now()))
    Application.DisplayAlerts = False 'set this to false to suppress overwrite warning dialog
    ActiveWorkbook.SaveAs "C:\Temp\temp.xls", , , , , , , xlLocalSessionChanges
    Application.DisplayAlerts = True
    
    Set objOutlook = CreateObject("Outlook.Application")
    Set objMailItem = objOutlook.CreateItem(0)
    With objMailItem
        .To = "sbrown15@nhs.net"
        .CC = ""
        .BCC = ""
        .Subject = "Monthly access renewal"
        .Body = "Hi, please see attached form"
        .Attachments.Add ActiveWorkbook.FullName
        .Display 'or .send
    End With
    Set objMailItem = Nothing
    Set objOutlook = Nothing
    ActiveWorkbook.Close False
End Sub

Open in new window

0
 
LVL 29

Accepted Solution

by:
matrixnz earned 500 total points
Comment Utility
Just saw this reposted, fortunately still had the code in Scite.

Hope that helps.

Cheers

#include <Excel.au3>

;~ Source Directory - Note added "\"
	Global $SRC_FILEPATH = "C:\Prod\"
;~ Check if Source Directory Exists, create the directory if false
	If FileExists($SRC_FILEPATH) = 0 Then DirCreate($SRC_FILEPATH)
;~ Temp Directory - Note added "\"
	Global $TMP_FILEPATH = "C:\Temp\"
;~ Check if Temp Directory Exists, create the directory if false
	If FileExists($TMP_FILEPATH) = 0 Then DirCreate($TMP_FILEPATH)
;~ First XLS WorkBook to Open
	Global $File1 = "File1.xls"
;~ Second XLS WorkBook to Open
	Global $File2 = "File2.xls"

;~ Attempt to open Source Directory\First XLS WorkBook
	Global $oExcel = _ExcelBookOpen($SRC_FILEPATH & $File1)
	;~ If unable to open the WorkBook or the File does not exist, open Excel with a Blank Excel WorkBook
	If @error = 1 Or @error = 2 Then
		$oExcel = _ExcelBookNew ()
	EndIf
;~ Write Cell to G41
	_ExcelWriteCell($oExcel, "26/11/2014", "G41")
;~ Save the WorkBook as Temp Directory\Second XLS WorkBook
	_ExcelBookSaveAs($oExcel, $TMP_FILEPATH & $File2, "xls", 0, 1)
;~ Close Excel WorkBook
	_ExcelBookClose($oExcel)

Open in new window

0
 

Author Closing Comment

by:joedelapaz
Comment Utility
Thanks Matrix. That did the trick! Cheers.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…
This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now