With my Tool we are scanning excel files and generating reports. My problem is some excel sheets are password protected and getting pup-up while scanning and it halts the Tool to run further.
Could someone let me know the solution, how to overcome with this problem. I just need to skip the sheet and continue with scanning without any user intervention.
Thank You,
Sugam
The workbook has a HasPassword property i.e. (C#):
Workbook book = GetYourWorkbook();
if (book.HasPassword)
{
// Ignore!
}
else
{
// Perform your operations here
}
Related
I am trying to run a simple script I have recorded on Excel which I want to run as a step in power automate.
When I try and run the script I get this error message in power automate
We were unable to run the script. Please try again. WorksheetCollection.getActiveWorksheet() is not allowed when run via Power Automate. Please modify your script to specify a target worksheet.
The script it copied below - I suspect this is something very easy - but have limited experience of scripts / VBA other than using the record function and cant find the solution anywhere - so any help would be most appreciated.
Many thanks
Matt
function main(workbook: ExcelScript.Workbook) {
let selectedSheet = workbook.getActiveWorksheet();
// Replace Stongly Agree with 5 on sheet student data
selectedSheet.replaceAll("Stongly Agree", "5", {
completeMatch: false,
matchCase: false
});
managed to solve it - the problem is the relative reference - I needed to change = workbook.getActiveWorksheet(); to = workbook.getWorksheet("worksheet name");
I have an Excel file, say Plano.xlsx and I am trying to run a VBA macro script on it using Autohotkey following the instructions stated here.
I don't want the Excel to be visible during this process. The VBA code is supposed to enter the value 99 in the cell C1 at the first sheet.
After hours of trial and error, the Autohotkey script runs smoothly without errors i.e. it opens an Excel process in the background supposedly
editing the Excel file and then exits. The problem is that the Excel file does not change at all. The VBA code works fine if I paste it manually
in a new VBA module in Excel without using Autohotkey.
Here is the code:
#SingleInstance force
#Include Acc.ahk
VBcode=
(
Sub myFunction()
Worksheets(1).Select
Worksheets(1).Range("C1").Select
Selection.Value = 99
End Sub
)
Excel_Run("myFunction")
Excel_Run(sFunction){
FilePath = C:\Users\KostasK\Desktop\Plano.xlsx
oExcel := ComObjCreate("Excel.Application")
Excel_ImportCode(VBcode)
oWorkbook := oExcel.Workbooks.Open(FilePath)
Excel_Get().Run(sFunction)
oWorkbook.Save
oExcel.Quit
}
Excel_ImportCode(VBcode){
if fileexist(A_ScriptDir . "\tempvbcode.txt")
FileDelete, %A_ScriptDir%\tempvbcode.txt
FileAppend, %VBcode%, %A_ScriptDir%\tempvbcode.txt
Excel_Get().ActiveWorkbook.VBProject
.VBComponents.Import(A_ScriptDir . "\tempvbcode.txt")
}
Excel_Get(WinTitle="ahk_class XLMAIN") { ; by Sean and Jethrow, minor modification by Learning one
ControlGet, hwnd, hwnd, , Excel71, %WinTitle%
if !hwnd
return
Window := Acc_ObjectFromWindow(hwnd, -16)
Loop
try
Application := Window.Application
catch
ControlSend, Excel71, {esc}, %WinTitle%
Until !!Application
return Application
}
To get the Acc.ahk library that is included in the script please see here. My Autohotkey version is v.1.1.23.05 and I use Excel 2013. I did not
take a closer look on Excel_Get() function but I used it instead of ComObjActive("Excel.Application") because the latter produces errors. There
is some useful info about that here. Finally, please note that I have enabled the following options in Excel Trust Center:
Enable all macros (not recommended, potentially dangerous code can run) and Trust access to the VBA project object model. Also, in Add-ins section
in COM Add-ins nothing is checked (I don't know if that matters). Finally, I always run the script as administrator.
This can be accomplished simply without VBA macro, while file being closed.
FilePath = C:\Users\KostasK\Desktop\Plano.xlsx
oExcel := ComObjGet(FilePath)
oExcel.Worksheets("Sheet1").Range("C1").VALUE := "99" ; you can put actual sheet name instead "sheet1"
oExcel.Save
oExcel.Quit
oExcel :=
I'm trying to automate some report generation where Excel VBA is doing all the work. My employer has a standardized set of templates of which all documents are supposed to be generated from. I need to populate one of these templates from Excel VBA. The Word templates utilize VBA extensively.
This is (some of) my Excel VBA code:
Sub GenerateReport() ' (Tables, InputDataObj)
' code generating the WordApp object (works!)
WordApp.Documents.Add Template:="Brev.dot"
' Getting user information from Utilities.Userinfo macro in Document
Call WordApp.Run("Autoexec") ' generating a public variable
Call WordApp.Run("Utilities.UserInfo")
' more code
End sub
In the Word VBA Autoexec module, a public variable named user is defined and declared. The Userinfo sub from the Utilities module populates user. Both these routines are run without any complaints from VBA. I would then like to be able to access the user variable in my Excel VBA, but I get the following error
Compile Error: Variable not yet created in this context.
How can I access the Word VBA variable in Excel VBA? I thought it more or less was the same?
EDIT: the user variable is a user defined Type with only String attributes. Copying the Word VBA functions that populate the user variable is absolutely doable, just more work than I though was necessary...
In a Word module:
Public Function GetUserVariable() As String '// or whatever data type
GetUserVariable = user
End Function
In an Excel module:
myUser = WordApp.Run("GetUserVariable")
Alternatively, you could be able to replicate the variables value - as it's called user I suspect it is returning some information about a user, or author, of a document. In which case one of the following might be what you're after:
'// Username assigned to the application
MsgBox WordApp.UserName
'// Username defined by the system
MsgBox Environ$("USERNAME")
'// Name of the author of the file specified
MsgBox CreateObject("Shell.Application").Namespace("C:\Users\Documents").GetDetailsOf("MyDocument.doc", 9)
Another option - if you could only add a line of code to the Utilities.UserInfo sub (after setting your public variable):
ActiveDocument.Variables("var_user") = user
Then you could access it easily afterwards in Excel:
Sub GenerateReport() ' (Tables, InputDataObj)
' code generating the WordApp object (works!)
'I am assuming your WordApp object is public, as you don't declare it.
'Capture the new document object
Dim newdoc as Object
set newdoc = WordApp.Documents.Add(Template:="Brev.dot")
' Getting user information from Utilities.Userinfo macro in Document
Call WordApp.Run("Autoexec") ' generating a public variable
Call WordApp.Run("Utilities.UserInfo")
'Get and show the value of "user"
Dim user as String
user = newdoc.Variables("var_user")
msgbox, user
End Sub
This is assuming that useris a string.
EDIT: As it is a requirement to work only on the Excel VBA, I would definely try the approach suggested by Scott and MacroMan - replicating the same functionality of the Word macros in Excel - if possible.
I assume that you've already ruled out the possibility of using an edited copy of the original template, set in a public folder...
For the sake of completness, there is another possibility: actually it is possible to inject VBA code in a Word document without the VBProject Object Model, by "brute force". If you rename a Word document as a .zip file and open it, you will notice a \word\vbaProject.bin file in it. This file contains the VBA project for the document and, in principle, one could add or change VBA code by modifying or replacing it.
I did some tests transplanting code from one document to another by simply copying the vbaProject.bin file, and the concept works. If you are interested in learning more about this file, this topic could be of use.
Notice, however, that to do what you want with such a technique would be somewhat complex (it would involve, for starters, updating zip files from your Excel VBA), and would require a lot of experimentation to mitigate the risk of accidentally corrupting your files. Definetly not recommended if you are looking for an easy and simple solution - but it is possible.
My goal is to use my application (written in visual c++ 2010) to open some files in excel and run a macro written in VBA
Here is the code I have right now
try
{
System::Object^ oMissing = System::Reflection::Missing::Value;
Excel::Application^ pExcel = gcnew Excel::Application();
pExcel->Visible = true;
Excel::Workbooks^ pBooks = pExcel->Workbooks;
Excel::_Workbook^ pBook = pBooks->Open(_XLSFile, ... );
Excel::_Workbook^ pBook2 = pBooks->Open(_XMLFile, ... );
Excel::_Workbook^ pBook3 = pBooks->Open(_MacroFile, ... );
pExcel->Run(macroName, ... );
// ....
}
catch(Exception^ e)
{
System::Windows::Forms::MessageBox::Show(e->Message);
}
I've omitted the 20ish "System::Reflection::Missing::Value" parameters required for Open() and Run().
The Run command throws an exception, gets caught, and the message box shows the error:
Cannot run the macro '<macroName>'. The macro may not be available in this workbook or all macros may be disabled
My problem is that this message appears for one macro file but not another, even though they are very similar and both do indeed have the macro defined in them.
I think it has to do with macros being enabled, which excel makes the user do manually. My confusion is why it works with one file and not the other.
Any suggestions on how to fix this are greatly appreciated!
EDIT: The solution to my particular problem (though I'm not sure why it worked) was to put my macro file into 'design mode' in the VBA IDE
Is it possible for an Excel VSTO add-in to read a constant from the vba code of a workbook? For example I have two constants stored in a module of all excel workbooks I have created
Public Const APPName As String = "test_app"
Public Const APPVersion As Double = 1.02
What I would like to do is pull these constants from the Application_WorkbookOpen sub of my VSTO add-in. I need this information to determine if the user should be able to open and modify the file, and log usage.
I know this question is quite old but for reference ... i just got the same requirement and solved it using a function which can be called from VSTO (in this example GetAppName) that returns the desired value.
string appName = (string)Application.Run(string.Format("'{0}'!GetAppName", Application.ActiveWorkbook.Name));