B
Bruce Hutfless
I need multiple instances of Word running, so that I don't muck with other
intances that maybe ruuning
Assume, Office 2003 (Word in particuliar)
VB6 code here -----
Dim oApp As Word.Application
Dim oDoc As Word.Document
Private Sub Command1_Click()
Command1.Enabled = False
' Start Word.
Set oApp = CreateObject("Word.Application")
'create document from template
Set oDoc = oApp.Documents.Add(Template:="C:\Source
Code\Dev\WordTstVB6\MyNormal.dot")
'This function demostrates above command not as expected
Dim oTmpl As Word.Template
For Each oTmpl In oApp.Templates
If oTmpl.Name = "Normal.dot" Then
'what goes here to remove Normal.dot from collection?
End If
Next
'Save our newly created document which should only have our template
oDoc.SaveAs ("C:\Source Code\Dev\WordTstVb6\MyDocument.doc")
'Open document
Set oDoc = oApp.Documents.Open("C:\Source
Code\Dev\WordTstVB6\MyDocument.doc")
'Set customazition context
oDoc.Application.CustomizationContext =
oDoc.Application.ActiveDocument()
'Supposedly, in our context only standard toolbar (commandBar) is
removed
For Each Item In oApp.CommandBars
If Item.Name = "Standard" Then
Item.Enabled = False
End If
Next
oApp.Visible = True
Command1.Enabled = False
Command2.Enabled = True
Command2.SetFocus
End Sub
Private Sub Command2_Click()
'Restore commadBars
For Each Item In oApp.CommandBars
Item.Enabled = True
Next
'Next line of code does function as expected.
For Each Item In oApp.Documents
If Item.Name = oDoc.Name Then
Item.Close Word.WdSaveOptions.wdSaveChanges
End If
Next
oApp.Quit Word.WdSaveOptions.wdDoNotSaveChanges
Set oApp = Nothing
Command1.Enabled = True
Command2.Enabled = False
Command1.SetFocus
End Sub
VB .Net code here ----
Private WithEvents ThisApplication As MSWord.Application
Private WithEvents ThisDocument As MSWord.Document
'This function launches MS Word and Opens A Dcoument
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Button1.Enabled = False
Try
' Start Word.
ThisApplication = New MSWord.Application
'create document from template
ThisDocument = ThisApplication.Documents.Add(Template:="C:\Source
Code\Dev\WFBruce\MyNormal.dot")
'This function demostrates above command not as expected
For Each oTmpl As MSWord.Template In ThisApplication.Templates
If oTmpl.Name = "Normal.dot" Then
End If
Next
'Save our newly created document which should only have our template
ThisDocument.SaveAs("C:\Source Code\Dev\WFBruce\MyDocument.doc")
'Open document
ThisDocument = ThisApplication.Documents.Open("C:\Source
Code\Dev\WFBruce\MyDocument.doc")
'Set customazition context
ThisDocument.Application.CustomizationContext =
ThisDocument.Application.ActiveDocument()
'Supposedly, in our context only standard toolbar (commandBar) is removed
For Each oCommandBar As Office.CommandBar In ThisApplication.CommandBars
If oCommandBar.Name = "Standard" Then
oCommandBar.Enabled = False
End If
Next
ThisApplication.Visible = True
Button1.Enabled = False
Button3.Enabled = True
Catch ex As Exception
Button1.Enabled = True
MsgBox(ex.ToString, MsgBoxStyle.Critical)
ThisApplication.Quit(MSWord.WdSaveOptions.wdDoNotSaveChanges)
End Try
End Sub
' this function closes our open document, restores CommandBars and Quits MS
Word
Private Sub Button3_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Button3.Click
If Button1.Enabled Then
Button3.Enabled = False
Exit Sub
End If
If Not ThisApplication Is Nothing Then
For Each oCommandBar As Office.CommandBar In ThisApplication.CommandBars
oCommandBar.Enabled = True
Next
'Next line of code does function as expected.
For Each oDoc As MSWord.Document In ThisApplication.Documents
If oDoc.Name = ThisDocument.Name Then
oDoc.Close(MSWord.WdSaveOptions.wdSaveChanges)
' this could does not work correct becuase this instance of Word
' should only contain single document
'instead every open document for every instace in collection
'next, line of code can not be executed other all documents in every
instance of Word closed
'Else
' oDoc.Close(MSWord.WdSaveOptions.wdDoNotSaveChanges)
End If
Next
'This line of code does not function correctly
'if another instances is openned after button1 clicked, then both instances
close.
'also, must include close option other Normal.dot change dialog pops up.
'ThisApplication.Quit()
ThisApplication.Quit(MSWord.WdSaveOptions.wdDoNotSaveChanges)
ThisApplication = Nothing
End If
Button1.Enabled = True
Button3.Enabled = False
End Sub
Steps to repeat problem ----
First test expected behavior ----
1) Launch Word by start->All Programs->Microsoft Office->Microsoft Office
Word 2003,
result: a) MDI windows opens with Document 1 window
2) Launch WFBruce.sln and build project and run project
result: a) Visual Studio .Net launches, project builds, Windows Form
Displayed,
b) previous Word MDI window on screen
3) Click Open Document button on WFBruce windows form
result: a) second instance of Word MDI window with MyDocument displayed only
Standard CommandBar (ToolBar missing)
b) previous Word MDI window on screen full commandBars
4) Click Close Document button on WFBruce windows form
result: a) second instance of Word closes correctly
b) previous Word MDI window on screen
5) Close Word launched in step 1.
So far so good --->
Second test, unexpected behavior ----
1) launch WFBruce.sln and build project and run program
2) Click Open Document button on WFBruce windows form
result: instance of Word MDI window with MyDocument displayed only Standard
CommandBar (ToolBar missing)
3) Launch Word by start->All Programs->Microsoft Office->Microsoft Office
Word 2003,
result: a) MDI windows opens with Document 1 window, however "We have a
problem Huston!", (Toolbar missing)
4) Click Close Document button on WFBruce windows form
result: a) all Word windows close!, "Huston, we have a serious prolbem1",
Both instances of Word closed. Very unexpected behavior.
Anyone know how to get around this?
Anyone else need to have something like this work? How did you accomplish
it?
Regards,
Bruce Hutfless
intances that maybe ruuning
Assume, Office 2003 (Word in particuliar)
VB6 code here -----
Dim oApp As Word.Application
Dim oDoc As Word.Document
Private Sub Command1_Click()
Command1.Enabled = False
' Start Word.
Set oApp = CreateObject("Word.Application")
'create document from template
Set oDoc = oApp.Documents.Add(Template:="C:\Source
Code\Dev\WordTstVB6\MyNormal.dot")
'This function demostrates above command not as expected
Dim oTmpl As Word.Template
For Each oTmpl In oApp.Templates
If oTmpl.Name = "Normal.dot" Then
'what goes here to remove Normal.dot from collection?
End If
Next
'Save our newly created document which should only have our template
oDoc.SaveAs ("C:\Source Code\Dev\WordTstVb6\MyDocument.doc")
'Open document
Set oDoc = oApp.Documents.Open("C:\Source
Code\Dev\WordTstVB6\MyDocument.doc")
'Set customazition context
oDoc.Application.CustomizationContext =
oDoc.Application.ActiveDocument()
'Supposedly, in our context only standard toolbar (commandBar) is
removed
For Each Item In oApp.CommandBars
If Item.Name = "Standard" Then
Item.Enabled = False
End If
Next
oApp.Visible = True
Command1.Enabled = False
Command2.Enabled = True
Command2.SetFocus
End Sub
Private Sub Command2_Click()
'Restore commadBars
For Each Item In oApp.CommandBars
Item.Enabled = True
Next
'Next line of code does function as expected.
For Each Item In oApp.Documents
If Item.Name = oDoc.Name Then
Item.Close Word.WdSaveOptions.wdSaveChanges
End If
Next
oApp.Quit Word.WdSaveOptions.wdDoNotSaveChanges
Set oApp = Nothing
Command1.Enabled = True
Command2.Enabled = False
Command1.SetFocus
End Sub
VB .Net code here ----
Private WithEvents ThisApplication As MSWord.Application
Private WithEvents ThisDocument As MSWord.Document
'This function launches MS Word and Opens A Dcoument
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Button1.Enabled = False
Try
' Start Word.
ThisApplication = New MSWord.Application
'create document from template
ThisDocument = ThisApplication.Documents.Add(Template:="C:\Source
Code\Dev\WFBruce\MyNormal.dot")
'This function demostrates above command not as expected
For Each oTmpl As MSWord.Template In ThisApplication.Templates
If oTmpl.Name = "Normal.dot" Then
End If
Next
'Save our newly created document which should only have our template
ThisDocument.SaveAs("C:\Source Code\Dev\WFBruce\MyDocument.doc")
'Open document
ThisDocument = ThisApplication.Documents.Open("C:\Source
Code\Dev\WFBruce\MyDocument.doc")
'Set customazition context
ThisDocument.Application.CustomizationContext =
ThisDocument.Application.ActiveDocument()
'Supposedly, in our context only standard toolbar (commandBar) is removed
For Each oCommandBar As Office.CommandBar In ThisApplication.CommandBars
If oCommandBar.Name = "Standard" Then
oCommandBar.Enabled = False
End If
Next
ThisApplication.Visible = True
Button1.Enabled = False
Button3.Enabled = True
Catch ex As Exception
Button1.Enabled = True
MsgBox(ex.ToString, MsgBoxStyle.Critical)
ThisApplication.Quit(MSWord.WdSaveOptions.wdDoNotSaveChanges)
End Try
End Sub
' this function closes our open document, restores CommandBars and Quits MS
Word
Private Sub Button3_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Button3.Click
If Button1.Enabled Then
Button3.Enabled = False
Exit Sub
End If
If Not ThisApplication Is Nothing Then
For Each oCommandBar As Office.CommandBar In ThisApplication.CommandBars
oCommandBar.Enabled = True
Next
'Next line of code does function as expected.
For Each oDoc As MSWord.Document In ThisApplication.Documents
If oDoc.Name = ThisDocument.Name Then
oDoc.Close(MSWord.WdSaveOptions.wdSaveChanges)
' this could does not work correct becuase this instance of Word
' should only contain single document
'instead every open document for every instace in collection
'next, line of code can not be executed other all documents in every
instance of Word closed
'Else
' oDoc.Close(MSWord.WdSaveOptions.wdDoNotSaveChanges)
End If
Next
'This line of code does not function correctly
'if another instances is openned after button1 clicked, then both instances
close.
'also, must include close option other Normal.dot change dialog pops up.
'ThisApplication.Quit()
ThisApplication.Quit(MSWord.WdSaveOptions.wdDoNotSaveChanges)
ThisApplication = Nothing
End If
Button1.Enabled = True
Button3.Enabled = False
End Sub
Steps to repeat problem ----
First test expected behavior ----
1) Launch Word by start->All Programs->Microsoft Office->Microsoft Office
Word 2003,
result: a) MDI windows opens with Document 1 window
2) Launch WFBruce.sln and build project and run project
result: a) Visual Studio .Net launches, project builds, Windows Form
Displayed,
b) previous Word MDI window on screen
3) Click Open Document button on WFBruce windows form
result: a) second instance of Word MDI window with MyDocument displayed only
Standard CommandBar (ToolBar missing)
b) previous Word MDI window on screen full commandBars
4) Click Close Document button on WFBruce windows form
result: a) second instance of Word closes correctly
b) previous Word MDI window on screen
5) Close Word launched in step 1.
So far so good --->
Second test, unexpected behavior ----
1) launch WFBruce.sln and build project and run program
2) Click Open Document button on WFBruce windows form
result: instance of Word MDI window with MyDocument displayed only Standard
CommandBar (ToolBar missing)
3) Launch Word by start->All Programs->Microsoft Office->Microsoft Office
Word 2003,
result: a) MDI windows opens with Document 1 window, however "We have a
problem Huston!", (Toolbar missing)
4) Click Close Document button on WFBruce windows form
result: a) all Word windows close!, "Huston, we have a serious prolbem1",
Both instances of Word closed. Very unexpected behavior.
Anyone know how to get around this?
Anyone else need to have something like this work? How did you accomplish
it?
Regards,
Bruce Hutfless