I'm asking this question, because when the document is opened in safe mode, my macro crashes, pointing to this line:
strInput = ActiveDocument.Content
So, to prevent this error message:
This command is not available because no document is open. Error code 4248
I want to check whether there are opened documents or not. And if not, then to set some timer and loop until the user changes the mode of the document.
Do you mean the protected mode?
In that case you can handle the ProtectedViewWindowActivate event of the Application class which is fired when any protected view window is activated.
See Working with Document Protected View Events in Word for more information.
Related
I'm hoping someone on here can point me in the right direction with some expertise. I have a situation with a client's outlook where we need to close any open draft windows after a new message/draft is opened. Ideally after the 4th new window has opened.
Any "new message" window that opens after this we need the script to close 1st window that opened. Either killing the process, or something similar.
Recently have been looking into Outlook macros, but am unsure if they will help in this instance. (Maybe they are?). Being more familiar with Powershell, figured we could start there.
Looking to get help writing a powershell script, macro, etc to do this on the backend.
The Outlook object model provides all the required events, methods and properties for that. So, VBA macros is the right choice if you don't need to distribute the solution on multiple machines. Otherwise, you need to consider developing a COM add-in instead (for example, a VSTO based one should work for you). See Walkthrough: Create your first VSTO Add-in for Outlook for more information.
You can handle the NewInspector event which is fired whenever a new inspector window is opened, either as a result of user action or through program code. The event occurs after the new Inspector object is created but before the inspector window appears.
You can also check the number of opened inspector windows in Outlook by using the Inspectors.Count property which returns a long indicating the count of objects in the specified collection.
Finally, the Inspector.Close method closes the Inspector and optionally saves changes to the displayed Outlook item. For example, a VBA sample which closes the active inspector instance:
Sub CloseItem()
Dim myinspector As Outlook.Inspector
Dim myItem As Outlook.MailItem
Set myinspector = Application.ActiveInspector
Set myItem = myinspector.CurrentItem
myItem.Close olSave
End Sub
Back in this question, I was looking for a way of disabling a particularly troublesome dialog from Outlook that warned me the attachments I was sending might be unsafe. Having concluded that the only security-setting options were unavailable because of administrator privileges, I've instead been trying to look for a workaround to have a script active within Outlook that does something like the following (pseudo VBA):
sub PollForDialog()
if dialogbox = open and dialogbox.name = "This outgoing message may..." then
Sendkeys arrowleft + enter
I've tried exhaustively to search for how to have scripts execute upon a messagebox popping up, and I haven't had any luck. Plenty of stuff about dialog boxes opening when a script is run, but nothing I can find on how to trigger on a dialog opening. Anyone able to help?
Thanks muchly.
I am making an app using Excel, with all user interaction happening in userforms. I would like my app to run in a separate instance of excel than any other file already opened, or any file opened manualy by the user while my app runs. I found the folowing link to be relevant and interesting but I am not sure how to implement this in my application: Answers.microsoft.com Can I create an instance of Excel that can only be accessed by my VBA project?
Here is how I would like to implement it:
I use two files: StartFile.xlsb, SystemFile.xlsb
The user opens StartFile normaly by clicking on the file
Sheet1 of StartFile displays a warning if macros are not enabled.
Once macros are enabled, StartFile checks excel version etc...
If all is good StartFile then opens SystemFile in a new instance of excel, and closes itself
SystemFile opens and somehow makes its excel instance private (Using "Application.IgnoreRemoteRequests = True" ?)
Clarification: The user will not be able to interact with the private excel instance application at all. Its .visible setting will remain false. It is used to display userforms, so to the user this will look like a standalone app
Sidenote: How do I make sure my new instance of excel has macros enabled no matter the macro security settings? (the user has already enabled macros once) (I have not noticed this happening so far, could it?)
Is it something like this you are looking for?
You cannot change the macro security settings as that would undermine the whole purpose of having these settings. Alternative, you can properly search for a VBA module to changing the Windows registry for this.
Furthermore, be careful if you use IgnoreRemoteRequeststhen you have to reset it again everytime.
** Edited ** Hide the SystemFile workbook and just show the userform? And then close the SystemFile workbook when the userform is closed.
Private Sub Workbook_Open()
Application.Visible = False
UserForm1.Show
End Sub
'UserForm module
Private Sub CommandButton1_Click()
Unload UserForm1
Application.Quit
End Sub
Good morning, I need my VBA code to run right before the TaskItem is saved, is there any way I can handle the event of saving ? I cannot find anything I could use in documentation. I am using MS-Office 2010.
Edit: I have tried
Private Sub TaskItem_Quit()
The macro disappears from macro list, but the code still does not run.
Try the Write event.
https://msdn.microsoft.com/en-us/library/office/ff868664.aspx
"Occurs when an instance of the parent object is saved, either explicitly (for example, using the Save or SaveAs methods) or implicitly (for example, in response to a prompt when closing the item's inspector)."
I have a macros that starts running when a document opens. It looks like this:
Private Sub Document_Open()
....
Dim strInput As String
strInput = ActiveDocument.Content
....
In office 2010 this macros works nicely, however, in office 2013 I get this error message (translation to English):
This command is not available, because there are no documents opened
And when I hit on Debug button, I see that this line of code is highligthed:
strInput = ActiveDocument.Content
I think it's a bug of office 2013, because for some insane reason it invokes this macros before the document is opened, even though I clearly force it to do right after opening - Private Sub Document_Open(). So, what is wrong with that and how can I fix that?
If you're seeing the yellow message bar then this isn't a bug. It's because your document isn't "trusted" - it's "sandboxed". You need to check whether it's a ProtectedViewWindow - introduced in Office 2010 - and take appropriate action.
The Protected View is explained in this article: https://support.office.com/en-us/article/What-is-Protected-View-d6f09ac7-e6b9-4495-8e43-2bbcdbcb6653. Basically, it prevents code from running in any document that comes from an origin / author that isn't trusted. The user needs to make an explicit decision to trust the document in order to go into Edit mode.
Obviously, this is a major impediment to VBA code, so you have the ProtectedViewWindow object in the object model. There are application-level events that can be used to manage this state in the entire Word environment, such as ProtectedViewWindowOpen that triggers when a document is opened "sandboxed". After determining whether the document is trustworthy, the code can make the document editable using the ProtectedViewWindow.Edit method.
For example, to trust all documents that come from a specific file path:
Private Sub app_ProtectedViewWindowOpen(ByVal PvWindow _
As ProtectedViewWindow)
If PvWindow.SourcePath = "C:\Test" Then
PvWindow.Edit
End If
End Sub
The above needs to be in an application-level class module and events for this class need to be active on the application. If you're not familiar with using application-level events, see https://msdn.microsoft.com/en-us/library/office/ff821218.aspx