-
[RESOLVED] Avoid Mutexing with my Instance of Word
How can I avoid mutexing of word with my instance. It should not open the documents opened by user (by dbl-clicking on icon) in my instance of Word.
This is because I open documents in my instance, do some manupulation and finally close it without saving any files. So chances of user losing his file is high.
Pradeep
-
Re: Avoid Mutexing with my Instance of Word
keep your instance hidden.
the user won't see it and can't manipulate it.
-
Re: Avoid Mutexing with my Instance of Word
That will not work since when you create your word object and set it to false for visibility and either shell a doc file or doubleclick a doc file it will open and show the app object. Then if you click on the Windows menu it will show both documents. The use can still switch to your programmed one and manipulate it etc.
VB Code:
Option Explicit
Private moApp As Word.Application
Private Sub Command1_Click()
Set moApp = New Word.Application
moApp.Visible = False
moApp.Documents.Add
moApp.Documents(1).Select
moApp.Selection.TypeText "Test"
End Sub
Private Sub Command2_Click()
'Or dont shell and just doubleclick on a doc file
Shell "D:\Program Files\Microsoft Office\OFFICE11\Winword.exe D:\Test.doc", vbMaximizedFocus
End Sub
-
Re: Avoid Mutexing with my Instance of Word
How do we avoid this?
Pradeep
-
Re: Avoid Mutexing with my Instance of Word
You can use the "/w" commandline switch to create a new separate instance of Word. If the user double clicks a desktop shortcut to Word or clicks a menu item for Word, it will still do the same. :(
VB Code:
Option Explicit
Private moApp As Word.Application
Private Sub Command1_Click()
Set moApp = New Word.Application
moApp.Visible = False
moApp.Documents.Add
moApp.Documents(1).Select
moApp.Selection.TypeText "Test"
End Sub
Private Sub Command2_Click()
'shell and create a new separate instance of Word.
'The Windows menu will not contain any other documents, etc.
Shell "D:\Program Files\Microsoft Office\OFFICE11\Winword.exe [b]/w[/b] D:\Test.doc", vbMaximizedFocus
End Sub
-
Re: Avoid Mutexing with my Instance of Word
But then it will not be a Word.Application object. So how will I reference this instance for the documents I want to open and manupulate from my code?
Pradeep
-
Re: Avoid Mutexing with my Instance of Word
You can create a separate app object variable/
VB Code:
Option Explicit
Private moApp1 As Word.Application
Private moApp2 As Word.Application
Private Sub Command1_Click()
Set moApp1 = New Word.Application
moApp1.Visible = True
moApp1.Documents.Add
moApp1.Documents(1).Select
moApp1.Selection.TypeText "Command1"
End Sub
Private Sub Command2_Click()
'shell and create a new separate instance of Word.
'The Windows menu will not contain any other documents, etc.
Shell "D:\Program Files\Microsoft Office\OFFICE11\Winword.exe /w D:\Test.doc", vbMaximizedFocus
End Sub
Private Sub Command3_Click()
Set moApp2 = New Word.Application
moApp2.Visible = True
moApp2.Documents.Add
moApp2.Documents(1).Select
moApp2.Selection.TypeText "Command3"
End Sub
This will create 3 separate Wod instances and all three will not have "Window" menu access to each other. ;)
-
Re: Avoid Mutexing with my Instance of Word
So when the user double clicks on any .doc file to open it, will it will open in a fourth instance and When I close these 3 instances, that document won't be closed?
Pradeep
-
Re: Avoid Mutexing with my Instance of Word
If a user doubleclicks a doc file or opens a new instance of Word from the start menu it will open in the first instance of Word, thus sharing the Window menu with the programmitacally opened docs. :(
You would have to add the "/w" switch to the standard Word menu item to prevent it. ;)
-
Re: Avoid Mutexing with my Instance of Word
I got the point of starting it with /w switch. But the problem is how to get a reference to the Word instance I started with /w switch to set to my object variable so that I can use it in my program?
Some kind of:
VB Code:
Set moApp2 = Shell ("D:\Program Files\Microsoft Office\OFFICE11\Winword.exe /w D:\Test.doc", vbMaximizedFocus)
End Sub
Pradeep
-
Re: Avoid Mutexing with my Instance of Word
The programmed instance is fine as well as the swith but it only depends on if the user manually opens another instance of Word after that. If they do, it will attach to what ever instance is open already.
VB Code:
Option Explicit
Private moApp3 As Word.Application
Private Sub Command4_Click()
Shell "D:\Program Files\Microsoft Office\OFFICE11\Winword.exe /w D:\Test.doc", vbMaximizedFocus
Dim i As Integer
Dim bFound As Boolean
i = 1
Do
Set moApp3 = GetObject(, "Word.Application")
If moApp3.Documents(i).Name = "Test.doc" Then
bFound = True
Exit Do
End If
i = i + 1
Loop
If bFound = True Then
moApp3.Documents(i).Select
Else
Set moApp3 = Nothing
MsgBox "Word Doc not found!"
End If
End Sub
This will get the shelled instance but again, if the user opens another instance of Word, it will share the app object.
-
Re: Avoid Mutexing with my Instance of Word
Is there no method to disable mutexing altogether (even if the changes are systemwide)?
Pradeep
-
Re: Avoid Mutexing with my Instance of Word
Not from within Word. I just searched. You will have to go to File Types, and modify the DOC filetype. Just add the "/w" switch to the OPEN property.
-
Re: Avoid Mutexing with my Instance of Word
That is what I have pointed out earlier. ;)
-
Re: Avoid Mutexing with my Instance of Word
Quote:
Originally Posted by robdog888
You would have to add the "/w" switch to the standard Word menu item to prevent it.
That one? I didn't even understand it. I went looking in Word for an option to open in the same window, but didn't find one, so I posted.
-
Re: Avoid Mutexing with my Instance of Word
Right, it doesnt have one. It would require you to add the switch to the start menu, quick launch (if added), and desktop shortcut. ;)
-
Re: Avoid Mutexing with my Instance of Word
Still wouldn't work to double-click on a .DOC file, without changing the filetype
-
Re: Avoid Mutexing with my Instance of Word
Yes, there too. Anyplace the user can start a Word instance you would have to add it.
Edit: Moved to Office Development
-
Re: Avoid Mutexing with my Instance of Word
Thanks a lot both of you. I got the point - just conquer all ways the user can open a document and use the /w switch there.
In file association with .DOC files, it is :
"C:\Program Files\Microsoft Office\Office10\WINWORD.EXE" /n /dde
Should I change it to:
"C:\Program Files\Microsoft Office\Office10\WINWORD.EXE" /w
Pradeep
-
Re: Avoid Mutexing with my Instance of Word
No, read this:
http://support.microsoft.com/default...b;en-us;210565
Here are the command switches, but may be for Word 2003:
Quote:
/safe
Start Word in Office Safe Mode.
/ttemplatename
Start Word with a new document based on a template other than the Normal template (Normal template: A global template that you can use for any type of document. You can modify this template to change the default document formatting or content.). Example: /tMyfax.dot
Note If the file name has spaces in it, enclose the complete name in quotation marks — for example, /t"Elegant Report.dot"
Security Because templates can store macro viruses, be careful about opening them or creating files based on new templates. Take the following precautions: run up-to-date antivirus software on your computer, set your macro security level to high, clear the Trust all installed add-ins and templates check box, use digital signatures, and maintain a list of trusted sources.
/pxslt
Start Word with a new XML document based on the specified Extensible Stylesheet Language Transformation (XSLT) (XSL Transformation (XSLT): A file that is used to transform XML documents into other types of documents, such as HTML or XML. It is designed for use as part of XSL.). Example: /p:c:\MyTransform.xsl
/a
Start Word and prevent add-ins (add-in: A supplemental program that adds custom commands or custom features to Microsoft Office.) and global templates (including the Normal template) from being loaded automatically. The /a switch also locks the setting files.
/laddinpath
Start Word and then load a specific Word add-in. Example: /lSales.dll
Security Use caution when running executable files or code in macros or applications. Executable files or code can be used to carry out actions that might compromise the security of your computer and data.
/m
Start Word without running any AutoExec macros (macro: An action or a set of actions you can use to automate tasks. Macros are recorded in the Visual Basic for Applications programming language.).
/mmacroname
Start Word and then run a specific macro. The /m switch also prevents Word from running any AutoExec macros. Example: /mSalelead
Security Because macros can contain viruses, be careful about running them. Take the following precautions: run up-to-date antivirus software on your computer; set your macro security level to high; clear the Trust all installed add-ins and templates check box; use digital signatures; maintain a list of trusted publishers.
/n
Start a new instance of Word with no document open. Documents opened in each instance of Word will not appear as choices in the Window menu of other instances.
/w
Start a new instance of Word with a blank document. Documents opened in each instance of Word will not appear as choices in the Window menu of the other instances.
-
Re: Avoid Mutexing with my Instance of Word
From command line or shortcut it's OK. But to disable mutexing alltogether on my system
should I edit my file associations to:
"C:\Program Files\Microsoft Office\Office10\WINWORD.EXE" /w
Usually users just double-click the DOC file to open it.
Pradeep
-
Re: Avoid Mutexing with my Instance of Word
Yes, thats the link I got the /w from. As long as your not needing to distribute your app then it doesnt matter if you place it all over. It would be a different story if you were though. ;)
-
Re: Avoid Mutexing with my Instance of Word
Thanks a lot. It's resolved for now.
One last question before finally closing this thread :
How much do I pay in terms of memory and CPU to avoid mutexing? Does mutexing reduce CPU and memory load to a great extent?
Pradeep
-
Re: Avoid Mutexing with my Instance of Word
It could depending on th number of instances of Word you create. When you dont mutate, ut costs you about 300~500 Kb for each additional instance, but when you create separate instances it wil cost a whole lot more like 15 Mb each. ;)
-
Re: Avoid Mutexing with my Instance of Word
Got it :thumb:
Mutexing : First Instance 15MB, successive instances - 500KB
Individual : 15MB per instance !!
So I'll just have to keep in mind to keep the number of open documents as low as possible. right?
Pradeep :)
-
Re: Avoid Mutexing with my Instance of Word
Problem resolved :thumb:
Thanks a lot RobDog and David
I wish I could rate the threads here, but the system just prevents it. But please accept my sincere thanks anyway.
Pradeep :)
-
Re: [RESOLVED] Avoid Mutexing with my Instance of Word
:thumb: Thanks for the Thanks :D
The "system" is just probably the spread. You need to give out a few more Reps before you can Rep again. ;)