Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I am writing a script that I want to pull cell values from an Excel spreadsheet containing usernames and passwords. I wish to use two variables USERNAME and PASSWORD. I would like to use the data on each row and move down a row with each pass of the loop.
I have looked over Excel UDF included in AutoIT and ExcelCOM_UDF written by a third party. I cannot seem to find the answers I am looking for. This should be a very basic function of either of these, but I am having trouble.
I am not looking for a handout, so just a nice reference page is fine. BUT if you have a snippet of what I need, it will not hurt my feelings as I am not determined to write this code solely on my own.
My "A" column will contain the USERNAME variables, and the "B" column will contain my PASSWORD variables.
Added snippet of some code of what I have so far. I have not tried to pull anything from Excel yet, because I cannot find a command that seems to do what I am looking to do. Hopefully seeing the code will give a better picture of my goal.
I DO NOT WISH TO MODIFY THE SPREADSHEET PROGRAMMATICALLY, ONLY SET STRING VARIABLES FROM CELL TEXT.
...
$USERNAME = "usernameHere" ;I want to set this variable as the text from A1 of c:\test.xls
$PASSWORD = "passwordHere" ;I want to set this variable as the text from B1 of c:\test.xls
;Username Box
MouseClick("left",1179,488,1)
;Type username
Send($USERNAME)
;Password Box
MouseClick("left",1179,578,1)
;Type Password
Send($PASSWORD)
...
In order to read a spreadsheet containing a list of usernames and password. This function create an array with two dimensions. The first column contains usernames, the second the password. With this
#include <Array.au3>
#include <Excel.au3>
Func get_data_Excel($filePathExcel)
Local $error
Local $oExcel = _ExcelBookOpen($filePathExcel, 0)
$oExcel.Application.DisplayAlerts = False
$oExcel.Application.ScreenUpdating = False
$error = Int(#error)
Select
Case $error = 1
msgbox(16, "Erreur : Fermeture de l'application", "Code erreur _ExcelBookOpen : 1"&#CRLF&#CRLF&"Impossible de creer l'objet $oExcel avec _ExcelBookOpen")
Case $error = 2
msgbox(16, "Erreur : Fermeture de l'application", "Code erreur _ExcelBookOpen : 2"&#CRLF&#CRLF&"Impossible d'ouvrir le fichier "&$filePathExcel&" avec _ExcelBookOpen")
EndSelect
If $error > 0 Then
_ExcelBookClose($oExcel, 0) ; Close without save
Exit
EndIf
; Read column A (login) and column B (pwd)
_ExcelSheetActivate($oExcel, "sheet1")
$nb_columns = $oExcel.ActiveSheet.UsedRange.Columns.Count
$nb_rows = $oExcel.ActiveSheet.UsedRange.Rows.Count
Local $array_data_excel[$nb_rows][2]
Local $idx
For $idx = 0 To $nb_rows-1
$array_data_excel[$idx][0] = $oExcel.Activesheet.Cells($idx+1, 1).Value
$array_data_excel[$idx][1] = $oExcel.Activesheet.Cells($idx+1, 2).Value
Next
_ExcelBookClose($oExcel, 0)
_ArrayDisplay($array_data_excel, "Data from excel file")
EndFunc
And to use this function :
get_data_Excel("d:\users.xlsx")
#include <Excel.au3>
Local $MyUsernameArray[5] = [ "Hans", "Gerd", "Walter", "Klaus", "Peter" ] ;Your Username Array
Local $MyPasswordArray[5] = [ "12345", "I", "am", "so", "creative" ] ;Your Password Array
$oExcel = _ExcelBookNew(1) ;Set this to 0 if you want it to work in background
_ExcelWriteCell ( $oExcel, "Usernames", "A1" ) ;self explaining (i hope)
_ExcelWriteArray ( $oExcel, 2, 1, $MyUsernameArray ,1) ;^
_ExcelWriteCell ( $oExcel, "Passwords", "B1" );^
_ExcelWriteArray ( $oExcel, 2, 2, $MyPasswordArray ,1);^
_ExcelBookSaveAs($oExcel, #ScriptDir & "\PleaseAtleastPostSomeCodeNextTimeSoWeSeeYouTried", "xls") ;save the file
_ExcelBookClose($oExcel, 1, 0) ;exit and save
If you want to get all control with COM/Automation Excel, don't use the UDF , andd deep inside the Com/Automation with the function AutoIt ObjCreate. With this function you can get the control of the Object Excel in AutoIt
COM/Automation with Excel
1) Create reference on COM Object of target application (here this is Excel)
$oExcel = ObjCreate("Excel.Application")
2) Add some options on this instance
With $oExcel
.Visible = 1
.WorkBooks.Add
EndWith
3) And now the work can be begin (add some worksheet or data, save the workbook...)
With $oExcel
.ActiveWorkbook.Worksheets(1).Name = "SHEET_ONE"
.ActiveWorkbook.Worksheets(1).Cells(1,1) = "I believe i can fly !!!"
EndWith
Hacks 1 : Save As
; Create new file with one sheet named "SHEET_ONE"
Local $oExcel = ObjCreate("Excel.Application")
With $oExcel
.SheetsInNewWorkbook = 1
.Visible = 1
.WorkBooks.Add
.ActiveWorkbook.Worksheets(1).Name = "SHEET_ONE"
.ActiveWorkbook.Worksheets(1).Cells(1,1) = "I believe i can fly !!!"
EndWith
Local $filename = #WorkingDir&"\saveas_excel.xls"
$oExcel.Application.DisplayAlerts = 0
$oExcel.Application.ScreenUpdating = 0
$oExcel.ActiveWorkBook.SaveAs($filename, 1, Default, Default, Default, Default, 2, 1)
$oExcel.Quit
More information (in french) on http://www.autoitscript.fr/forum/viewtopic.php?t=3272&f=11
Related
I have an Excel sheet with 10 columns each containing two data fields separated by a space. I want to separate the data fields into two columns. But I don't want to replicate the code 10 times. I want code I can loop through 10 times. Sounds simple right? The problem is that I can't set the ranges properly for the texttocolumns method. It seems to insist on having literal references, i.e. "A1".
Trying to use any kind of counter for the range settings throws a COMexception error.
This works:
`$range = $sheet.Range("A1").entirecolumn;
$target = $sheet.Range("A1");
$xlDelimited = 1
$xlTextQualifier = -4142
$xlTextFormat = 2
$result = $range.texttocolumns($target,$xlDelimited,$xlTextQualifier,$false,$false,$false,$false,$false,$true," ")`
This doesn't work:
`$range = $sheet.Range(1).entirecolumn;
$target = $sheet.Range(1);
$xlDelimited = 1
$xlTextQualifier = -4142
$xlTextFormat = 2
$result = $range.texttocolumns($target,$xlDelimited,$xlTextQualifier,$false,$false,$false,$false,$false,$true," ")`
I need some help with vba code. I'm self-lerning so please be understanding for simply cases ;)
I'm trying to create macro which will be looking for some records/cells in one workbook (FileA) (determined by 3 conditions) and then paste values to another workbook (FileB) and after that find in another tab in FileB some values where condition will be pasted value to match them with looking value (I belivie it could be done somehow with Vlookup but I get stuck).
Below problematic for me part of code (I'm working on some files found in work, no one use it now).
First issue is with Set Update, I don't know why it takes it as "Nothing"... conditions are true - I mean "pp1" is existing in column A in FileA.
Second issue shows when I change i start range to some "later" numbers, eg From i = 2280, macro is ignoring last line where should assign some values (again shows update2 as "nothing") but value if pp2 is existing in W column in tab data...
Dim name As String
name = "[path to file on sharepoint]"
Application.ScreenUpdating = False
Workbooks.Open Filename:=name
a = 1
For i = 2263 To 14000
If Workbooks("FileA").Sheets("Main").Cells(i, 11) = "CANCEL" And Workbooks("FileA").Sheets("Main").Cells(i, 6) = "DENIS" And Workbooks("FileA").Sheets("Main").Cells(i, 5) > 1301358454 Then
pp1 = Workbooks("FileA").Sheets("Main").Cells(i, 1)
If pp1 > 0 Then
Set Update = Workbooks("FileA").Worksheets("Main").Range("A:A").Find(pp1, lookat:=xlPart)
If Update > 0 Then
Update = Update.Row
Workbooks("FileB").Worksheets("lost").Cells(a, 1).Value = Workbooks("FileA").Worksheets("Main").Cells(Update, 5)
pp2 = Workbooks("FileB").Worksheets("lost").Cells(a, 1)
update2 = Workbooks("FileB").Worksheets("data").Range("W:W").Find(pp2, lookat:=xlPart).Row
Workbooks("FileB").Worksheets("lost").Cells(a, 5) = Workbooks("FileB").Worksheets("data").Cells(update2, 43)
I am trying to export some text from annotation fields in Nuance Power PDF to Excel using VBA. I added the Nuance Power PDF reference to Excel VBA (PDF Plus).
I used it and it works well but the text returned from fields is empty.
Set PDFApp = CreateObject("NuancePDF.App")
Set dvDoc = CreateObject("NuancePDF.DVDoc")
dvDoc.Open("\\adpdc-2\Users$\a.goudinoux\Documents\Macro Formulaire\fiche.pdf")
Set ddDoc = dvDoc.GetDDDoc()
Set ddPage = ddDoc.AcquirePage(0)
nbannots = ddPage.GetNumAnnots() - 1
For i = 0 To nbannots
Texte = ""
Set ddAnnot = ddPage.GetAnnot(i)
Set ddText = ddDoc.CreateTextSelect(0, ddAnnot.GetRect())
ThisWorkbook.Sheets(1).Cells(1, i) = ddAnnot.GetTitle()
For k = 0 To ddText.GetNumText()
Texte = Texte & ddText.GetText(k)
Next
ThisWorkbook.Sheets(1).Cells(2, i) = Texte
Next
Part of the PDF document :
Results :
As you can see the first line is working but not the second one.
I thought the problem was with ddText but ddText.GetNumText() gives the right number of text elements in the text selection (ex : 2, 5, 4, etc...) when I run my program in Debug Mode.
I think the problem is from the function GetText(k).
I made it work once but I can't find my code back..
Do you see any mistake ?
Either your annotations are more complex than indicated or you're overthinking it.
Just read the annotation with GetContents and call it a day.
Set PDFApp = CreateObject("NuancePDF.App")
Set dvDoc = CreateObject("NuancePDF.DVDoc")
dvDoc.Open("\\adpdc-2\Users$\a.goudinoux\Documents\Macro Formulaire\fiche.pdf")
Set ddDoc = dvDoc.GetDDDoc()
Set ddPage = ddDoc.AcquirePage(0)
nbannots = ddPage.GetNumAnnots() - 1
For i = 0 To nbannots
Set ddAnnot = ddPage.GetAnnot(i)
ThisWorkbook.Sheets(1).Cells(1, i) = ddAnnot.GetTitle
ThisWorkbook.Sheets(1).Cells(2, i) = ddAnnot.GetContents
Next i
I'm trying to copy some cells from Microsoft Excel to another program using AutoIt, but I can just copy one cell at a time. Is there a way to copy a group or range of cells?
My code:
Local $Resultcpf = Excel_RangeRead($oWorkbook, default, "L4")
ClipPut($Resultcpf)
$Data = ClipGet()
"Is there a another way to copy a group of range?"
As per Documentation - User Defined Function Reference - _Excel_RangeRead() :
Reads the value, formula or displayed text from a cell or range of cells of the specified workbook and worksheet
Example :
#include <MsgBoxConstants.au3>
#include <Array.au3>
#include <Excel.au3>
Global Const $g_sFileDoc = 'C:\document.xlsx', _
$g_sRange = 'L:L', _; 'L1:L4'
$g_sPrompt = 'Press [CTRL] + [V] to paste contents of %s.'
Global $g_oExcel = _Excel_Open(), _
$g_oBook = _Excel_BookOpen($g_oExcel, $g_sFileDoc)
Global $g_aData = _Excel_RangeRead($g_oBook, Default, $g_oBook.ActiveSheet.Usedrange.Columns($g_sRange))
Global $g_sData = _ArrayToString($g_aData)
_ArrayDisplay($g_aData)
ClipPut($g_sData)
MsgBox($MB_SYSTEMMODAL, #ScriptName, StringFormat($g_sPrompt, $g_sRange))
You have to define the range by B3:C5 for two columns or A1:A17 for one column and so on. The range has to be divided by colon ":" sign.
#include-once
#include <Array.au3>
#include <Excel.au3>
Global $sFileExcel = #DesktopDir & '\MyExcelSheet.xlsx'
Global $sExcelRange = 'B3:C5'
Func _readExcelContent()
Local $oExcel = _Excel_Open()
Local $oWorkbook = _Excel_BookOpen($oExcel, $sFileExcel)
Local $aData = _Excel_RangeRead($oWorkbook, Default, $sExcelRange, 2)
_Excel_Close($oExcel, Default, True) ; excel will be closed
Return $aData
EndFunc
Global $aData = _readExcelContent()
_ArrayDisplay($aData) ; this is just a preview of the read data
ClipPut(_ArrayToString($aData))
The result/output could be :
text1|
text2|text4
text3|
Depending on Excel content of course.
I am developing an excel add-in. I have serial numbers(ex. 100 psc) and I want to check when excel add-in installing on pc. But I cant do it with VS2010 setup project because it is not supporting serial number list storing and checking.
so I want to do this with setup factory and I did it like this link:
link
but I have a problem excel ;
if I select "Yes", excel working for opening .dll, if select "No", it do anything.
and my setup factory list like this.
and my setup factory "on post install script", my Addinfilename value is "Posta Guvercini Excel AddIn For 2010.dll"
-- Determine registry key (2 = HK CURRENT USER)
sVersions = Registry.GetKeyNames(2, "Software\\Microsoft\\Office");
-- Iterate through the registry keys per MS Office-version
--Next line has SetupFactory 8 code
--for iCount1, sVersion in sVersions do
for iCount1, sVersion in pairs(sVersions) do
-- Try opening the registry key
sSubKey = "Software\\Microsoft\\Office\\"..sVersion..
"\\Excel\\Options\\"
sValues = Registry.GetValueNames(2, sSubKey);
--initialize index counter
iIndex = -2
if sValues then
--Determine the index of the maximimum OPEN registry entry
--Next line has SetupFactory 8 code
--for iCount2, sValue in sValues do
for iCount2, sValue in pairs(sValues) do
if (String.Left(sValue, 4) == "OPEN") then
--Check whether the user did not already install
--the same add-in to prevent errors when opening Excel
sKeysValue = Registry.GetValue(2, sSubKey, sValue, true)
if String.Find(sKeysValue, SessionVar.Expand(
"%AddinFileName%"), 1, false) > 0 then
iIndex = -1
-- leave loop
break;
else
if (sValue == "OPEN") then
iIndex = 0
else
iIndex = String.ToNumber(String.Mid(
sValue, 5, String.Length(sValue)-4))
end;
end;
end;
end;
-- -1 means: This add-in is already installed; we're done
if iIndex ~= -1 then
--Determine path based on variable "%AddinFileName%
sAppPath = String.Char(34)..
SessionVar.Expand("%AppFolder%")..
"\\"..
SessionVar.Expand("%AddinFileName%")..
String.Char(34)
-- -2 is the initialized value of the index counter
if (iIndex == -2) then
-- OPEN-key does not exist
Registry.SetValue(2, sSubKey, "OPEN",
sAppPath, REG_SZ)
else
Registry.SetValue(2, sSubKey, "OPEN"..(iIndex + 1),
sAppPath, REG_SZ)
end;
end;
end;
end;
Looks like you are building an Automation addin. If so you need to prefix the addin name with /A to tell Excel that its an automation addin. otherwise its expecting an XLL or an XLA or an XLAM
i solve this problem. When doing excel installiation, we must use registry record.
result = Registry.SetValue( HKEY_LOCAL_MACHINE, "Software\\Microsoft\\Office\\Excel\\Addins\\company" , "LoadBehavior" , "3" , REG_DWORD );
result = Registry.SetValue(HKEY_LOCAL_MACHINE, "Software\\Microsoft\\Office\\Excel\\Addins\\company" ,"FriendlyName", "program name", REG_SZ);
result = Registry.SetValue( HKEY_LOCAL_MACHINE, "Software\\Microsoft\\Office\\Excel\\Addins\\company" , "Description" , "program name" , REG_SZ );
result = Registry.SetValue(HKEY_LOCAL_MACHINE, "Software\\Microsoft\\Office\\Excel\\Addins\\company" ,"Manifest", SessionVar.Expand("%AppFolder%\\myvtofile.vsto|vstolocal"), REG_SZ);
for add-in start when excel startup.
LoadBehavior key's value should be "3".