I use the Private Sub Workbook_Open() for some VBA code in order to run those code when I open the workbook. I try it on my laptop, it works fine but when I send the file to my colleague the file, it opens in protected view, and then when clicking the enable editing, it got Error code 1004: Method ‘Sheets’ of Object ‘_Global’ Failed. Try to surf the internet, it said that because the file opens on protected view so the VBA won't run.
How can I solve this problem?
Thank you
Related
I'm getting reports of VBA "Run-time error '1004': Method 'OnKey' of object '_Application' failed" at least on Excel 2016 under Windows in this code:
Private Sub Workbook_Deactivate()
Application.OnKey "^x"
End Sub
(The code overrides certain hotkeys when my VBA-enabled workbook is active, and restores the default behavior when the user switches back to another workbook.)
What could be the reason?
Unexpectedly, many API calls fail when the active workbook is in the Protected view mode. I wasn't even able to switch the active workbook to ThisWorkbook via Activate.
The workaround I came up with is to dismiss "Protected view" when the user switches to such a document from my app/workbook:
If Not (Application.ActiveProtectedViewWindow Is Nothing) Then
Application.ActiveProtectedViewWindow.Edit
End If
Application.OnKey "^x"
This gives up some of its protection, but as my users are working mainly with internal documents, this tradeoff makes sense.
Previously this code for opening and activating(bringing workbook to front) has worked for other builds of Microsoft office, however Microsoft's latest build(16.0.11126.20234) seemed to create a problem with opening an existing excel workbook.
testWorkbook = this.Application.Workbooks.Open(path);
testWorkbook.Activate();
The issue is when launching excel and using an 'Open' method on a workbook:
Excel application launches
The title of on the top of the window changes to the workbook name you are trying to open, however the main body of the window is still the default Excel Home screen( home, options, recent workbooks, templates)
Selecting options or any other item on the splash screen results in the splash screen closing and displays the workbook that was intended to be open
I've tried removing any existing code, and created a new AddIn solution with just the code Microsoft documentation provides for loading and displaying a workbook.
private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
Workbook testWorkbook;
string path = "C:\\Path\\To\\Workbook\\TestingBookOpen.xlsx";
try
{
testWorkbook = this.Application.Workbooks.Open(path);
testWorkbook.Activate();
}
catch (Exception ex){
string message = ex.Message;
}
}
I know there are workarounds including:
You can disable the start screen from excel directly.
You can click into options and then exit it which will bring the sheet up
You can have excel already open and it'll work fine
But I'm looking for a programmatic solution/workaround for when my AddIn launches, and to see whether other's have come across the same problem in Microsoft Office's latest build.
I am trying this to activate a custom tab:
Globals.Ribbons.MyRibbon.RibbonUI.ActivateTab("TabAddin")
It simply does nothing. No error, no tab activation, nothing.
If I change tab name to a non existing tab, it throws an exception, what it is obvious. That means the ActivateTab method is doing something, but not what it is intended to do.
Any help please? this is VSTO for Excel 2016.
It turned out that I was opening the book programmatically and immediately after calling Open method I was trying to activate the custom tab. At that stage, tab isn't created yet (maybe Open method is asychronous), that is why tab was not activated.
Finally I have used this to open the book:
var excelApp = Globals.ThisAddIn.Application;
excelApp.WorkbookActivate += excelApp_WorkbookActivate;
Excel.Workbook workbook = excelApp.Workbooks.Open(Filename: fileToOpen);
And in other part of the source file, I have:
void excelApp_WorkbookActivate(Excel.Workbook Wb)
{
Globals.ThisAddIn.Application.Wait(DateTime.Now.AddSeconds(1));
this.RibbonUI.ActivateTab("TabLeanAddin");
Globals.ThisAddIn.Application.WorkbookActivate -= excelApp_WorkbookActivate;
}
All the above caused the add-in to work as expected.
Cheers
Jaime
I can writing VBA in Mac os. The below code in excel (windows) check an option button.
ActiveSheet.Shapes("Otion button 1").OLEFormat.Object.Value=true
This code does not work in Mac excel.
Can anyone help to get the code for Mac excel to do this function.
Thank you,
The code you pasted doesn't work in the Windows environment either. It results in an error: "Runtime error '438': Object doesn't support this property or method"
After exploring the locals window, I found that the correct way to access this value is through a double .object ... So your code would be:
ActiveSheet.Shapes("Otion button 1").OLEFormat.Object.Object.Value = True
So I have been able to load a PDF into my userform (Excel 2013 on all machines) setting reference Adobe Acrobat Webrowser Control and adding control to userform. When i open the file on another computer or try to create another excel userform and pdf I get the following errors "Run-time error-459" or "Element not found" or "Compile error: Method or data member not found"
I am using Acrobat Adobe Reader DC
I also noticed when adding control to form it labels it as Control1 instead of AcroPDF1
Private Sub Workbook_Open()
With UserForm1.Control1
.LoadFile ThisWorkbook.Path & "\sample.pdf"
End With
UserForm1.Show
End Sub
Please any help much appreciated
Make sure that the Additional Control for Adobe has been added. Also take a quick look into this post for workaround and suggestions. http://www.ozgrid.com/forum/showthread.php?t=88177
After some trial and error i found a solution. I was currently using google chrome when experiencing this issue. The problem was that i had a google app conflicting with the pdf. So i disabled google apps and problem dissappeared. Sorry comunity i didnt resopnd earlier..thanks for all the input