Add Eventhandler to Object of type Word.Application - ms-word

With the following code I open a a new word document. To save the word document programmatically I want to add an event listener to the object, which occurs when word will be closed. Is this possible?
Set objWord = createobject("Word.Application")
objWord.visible = True
Set objDoc = objWord.documents.add()
Call objDoc.content.select()
Set selection = objWord.selection()
Call selection.collapse()
objDoc.Close
Set objDoc = Nothing
objWord.Quit
Set objWord = Nothing
EDIT:
I try to explain better what I want to do. My Lotusscript Agent should open a word document for the user. The user types in some text and then closes the word document. Instead of showing the save dialog of word I want to save the document programmatically to embedd the file as attachment to an notes document. Until now I have not found a solution to get an handle of the event when word is closing.

I think this link on one of the ldd wikis probably has what you need. If you are a little clearer on what you need, you may get better answers though.
I think you are wanting to be notified when word closes so you can force a save or something correct? You can already force the save using similar lotusscript to how you are closing it, you don't need to wait until they close it themselves.

Related

Close OpenXML Document Without Saving

From what I understand, when you open, edit, then close an OpenXML document such as an .DOCX file, the revised document is automatically saved. If you change your mind and decide not to save the edits, is there a way to close the document without saving? I can't seem to find anything.
The document has an AutoSave property, which is set to True by default. The Open method can take an OpenSettings object, which can be used to override the default behavior:
Imports DocumentFormat.OpenXml.Wordprocessing
Dim os As OpenSettings = New OpenSettings()
os.AutoSave = False
Dim doc as Doc = WordprocessingDocument.Open(Path, True, os)
In this case, the document will not be saved unless doc.Save is called. doc.Dispose will simply release the resources without the save.

VSTO Word add-in - new document event not firing if Word is launched from the executable

In my add-in, I need to create a task pane for each open document. In the add-in's startup method, I subscribe to the ApplicationEvents4_Event.NewDocument and Application.DocumentOpen events, and then create a task pane for each opened document:
((ApplicationEvents4_Event)Application).NewDocument += CreateTaskPaneWrapper;
Application.DocumentOpen += CreateTaskPaneWrapper;
foreach (Document document in Application.Documents)
{
CreateTaskPaneWrapper(document);
}
This covers cases for opening or creating a document through Word's menu, or opening an existing document file in the OS. However, if Word is already opened, launching WINWORD.EXE (or accessing it through a shortcut, which is a pretty common scenario) doesn't trigger either event, despite a new window with a new document being opened. How can I react to this scenario and create a task pane for a document created this way? I'm using VSTO 3 and Visual Studio 2008, targeting Word 2007.
If Word is started, a new document is created BEFORE the Add-In loads, therefore this event can not be trapped.
If you need to work with the initially created document, just take a look at the Documents collection - if Count is greater zero, this document is the one created by Word before your Add-In was loaded.
So I solved this problem in my solution, although I'm not sure it will be cross applicable. Sadly, mine is in VB.Net, so there may need to be some translation.
First, I ended up not using ApplicationEvents4_Event Instead there are other built in event triggers you can use via "ThisAddIn"
Private Sub Application_NewDocument(ByVal Doc As Word.Document) Handles Application.NewDocument
'MsgBox("I opened something")
myCustomTaskPane = Me.CustomTaskPanes.Add(New MyCustomTaskPaneClass, "TaskPane", Doc.ActiveWindow)
myCustomTaskPane.Visible = True
End Sub
Using this method I did have a similar challenge. Running winword.exe, and thereby opening a new word document, did not trigger the NewDocument event. Luckily, there was another event to use - Document change.
Private Sub Application_DocumentChange() Handles Application.DocumentChange
'function to test if the ActiveDocument has a taskpane from my add-in, and then a function to add one
If Not HasMyCustomTaskPane() then AddCustomTaskPane()
End Sub
So - bottom line, regardless of if you keep using ApplicationEvents4_Event you should see if you can use the DocumentChange event. It triggers when a new word window is selected.
Handling task panes for more than one window in Word is fairly complicated, because of how Word loads and re-uses open windows. To do it correctly, you have to consider different actions:
The user takes an action to display or hide a task pane.
The user creates a new document.
The user opens an existing document.
The user closes an open document.
There's a tutorial that explores all the details, both in VB and C#: https://msdn.microsoft.com/en-us/library/bb264456%28v=office.12%29.aspx
I also found a similar answer on SO.

Setting / accessing the Application.CutCopyMode property via UI

We are facing an error of "This method or property is not available because the clipboard is empty or not valid" in one of our Lotus Notes applications. The code basically opens a document, selects all the text, copies it and then pastes it in MS Word document. It does this continuously for a bunch of documents. I found this link which basically suggests adding Application.CutCopyMode = False to the code to resolve the issue.
I need to know can this property be accessed and set via UI for MS Word?
In the example you reference, that Keyword is actually part of the Excel object model, not the Word object model. So you are unlikely to find it in Word's UI. Perhaps you are doing something with Excel as well, or perhaps it is a keyword in the relevant Lotus object model as well?
[
The OP mentioned a link, http://www.tech-archive.net/Archive/Word/microsoft.public.word.vba.general/2007-06/msg00446.html
that suggested changing variables names, but that that did not work.
The linked article also suggests "sleeping for a few milliseconds between copying and pasting"
]

how to stop macros running when opening a Word document using OLE Interop?

As the title suggests, I have a .Net application which uses interop to open documents in Word. I have set
app.AutomationSecurity = Microsoft.Office.Core.MsoAutomationSecurity.msoAutomationSecurityForceDisable
before opening the document. According to the documentation, thhis "Disables all macros in all files opened programmatically, without showing any security alerts"
However, when I attempt to open one specific document I get a dialog box on the screen that says "could not load an object because it is not available on this machine". It's a customer document but I believe it contains a macro with references to a COM object which I don't have installed.
Am I doing something stupid? is there any way to actually disable macros when opening a Word document?
Try:
WordBasic.DisableAutoMacros 1
Bizarrely, this relies on a throwback to pre-VBA days, but still seems to be the most-reliable way to ensure that no auto macros are triggered (in any document - you may want to turn it back using the parameter "0").
I recently had a project where I had to process 6,000 Word templates (yes, templates, not documents) many of which had oddball stuff like macros, etc. I was able to process all but 6 using this technique. (I never did figure out what the problem was with those 6).
EDIT: for a discussion of how to call this from C#, see: http://www.dotnet247.com/247reference/msgs/56/281785.aspx
For c# you can use
(_wordApp.WordBasic as dynamic).DisableAutoMacros();
The whole code I'm using is:
using Word = Microsoft.Office.Interop.Word;
private Word.Application _wordApp;
...
_wordApp = new Word.Application
{
Visible = false,
ScreenUpdating = false,
DisplayAlerts = Word.WdAlertLevel.wdAlertsNone,
FileValidation = MsoFileValidationMode.msoFileValidationSkip
};
_wordApp.Application.AutomationSecurity = MsoAutomationSecurity.msoAutomationSecurityForceDisable;
(_wordApp.WordBasic as dynamic).DisableAutoMacros();

Lotus Notes #command to mark the current email as read?

Does anyone know of a way to mark the current email as read using an #command?
If by "current document" you mean a the selected document in a view, you can use
ToolsMarkSelectedRead #Command
In a view or folder, marks all of the selected documents as read.
Syntax
#Command( [ToolsMarkSelectedRead] )
Usage
A database must be open at the view level
or
A document must be open in Read or Edit mode
Note In some databases, there is no difference in the display of read and unread documents in views and folders.
This command does not work on the Web.
Also, unless it's a new method in R8, NotesDocument.MarkRead is not an existing method. The only way you could do this would be to call NotesDocument.Save, and pass in true for the markRead parameter:
flag = notesDocument.Save( force, createResponse [, markRead ] )