R
RB Smissaert
Developed an Excel add-in in Excel 2003.
There is one Sub to open a Word document.
For this I would like to use early binding.
I have the following code to set a reference to the Word Object Library:
Sub ActivateWordLibrary()
Dim R
On Error Resume Next
'no need to carry on if the Word object library is already there
'------------------------------------------------------------------------
For Each R In ThisWorkbook.VBProject.References
If R.GUID = "{00020905-0000-0000-C000-000000000046}" Then
Exit Sub
End If
Next
'Will this work with any Word version?
'Word 2002 is Major:=8, Minor:=2
'Word 2003 is Major:=8, Minor:=3
'---------------------------------------
ThisWorkbook.VBProject.References.AddFromGuid _
GUID:="{00020905-0000-0000-C000-000000000046}", _
Major:=0, Minor:=0
On Error GoTo 0
End Sub
And the following code to open the Word document:
Sub OpenDocument()
Dim Wd As Word.Application
Dim Doc As Document
Dim WdDoc As String
Dim docFound As Boolean
Dim isect As Range
Dim LR As Long
Const ERR_APP_NOTRUNNING As Long = 429
LR = Cells(65536, 11).End(xlUp).Row
'make sure you are in the table
Set isect = _
Application.Intersect(ActiveCell, Range(Cells(2, 1), Cells(LR, 11)))
If isect Is Nothing Then
MsgBox "YOU ARE OUTSIDE THE TABLE", , " OPEN DOCUMENT"
Exit Sub
End If
WdDoc = _
strDocumentFolder & Trim(Cells(ActiveCell.Row, 10).Value) & ".doc"
On Error Resume Next
Set Wd = GetObject(, "Word.application")
If Err = ERR_APP_NOTRUNNING Then
'Word not running so start it and open the document
Set Wd = New Word.Application
Set Doc = Wd.Documents.Open(WdDoc)
Wd.Visible = True
Else
'Word running
If Wd.Documents.Count = 0 Then
'no document open at all so open the Doc
Set Doc = Wd.Documents.Open(WdDoc)
Else
For Each Doc In Wd.Documents
'see if the particular document is already open
If Doc.Path & "\" & Doc.Name = WdDoc Then
'it is open so no need to look any further
docFound = True
Exit For
End If
Next
'particular document not found, so open it
If docFound = False Then
Set Doc = Wd.Documents.Open(WdDoc)
End If
End If
End If
'usercontrol is read only for the
'Word application whereas in
'Excel is it is read/write
Doc.UserControl = True
AppActivate "Microsoft Word"
Doc.Activate
End Sub
This all works in With Excel 2003 and Word 2003.
My question is will it also work with Word 97, 2000 and 2002?
I understand that doing:
Major:=0, Minor:=0
should make it pick the available Word Object Library, but is there anything
else
that could go wrong here?
Thanks for any advice.
RBS
There is one Sub to open a Word document.
For this I would like to use early binding.
I have the following code to set a reference to the Word Object Library:
Sub ActivateWordLibrary()
Dim R
On Error Resume Next
'no need to carry on if the Word object library is already there
'------------------------------------------------------------------------
For Each R In ThisWorkbook.VBProject.References
If R.GUID = "{00020905-0000-0000-C000-000000000046}" Then
Exit Sub
End If
Next
'Will this work with any Word version?
'Word 2002 is Major:=8, Minor:=2
'Word 2003 is Major:=8, Minor:=3
'---------------------------------------
ThisWorkbook.VBProject.References.AddFromGuid _
GUID:="{00020905-0000-0000-C000-000000000046}", _
Major:=0, Minor:=0
On Error GoTo 0
End Sub
And the following code to open the Word document:
Sub OpenDocument()
Dim Wd As Word.Application
Dim Doc As Document
Dim WdDoc As String
Dim docFound As Boolean
Dim isect As Range
Dim LR As Long
Const ERR_APP_NOTRUNNING As Long = 429
LR = Cells(65536, 11).End(xlUp).Row
'make sure you are in the table
Set isect = _
Application.Intersect(ActiveCell, Range(Cells(2, 1), Cells(LR, 11)))
If isect Is Nothing Then
MsgBox "YOU ARE OUTSIDE THE TABLE", , " OPEN DOCUMENT"
Exit Sub
End If
WdDoc = _
strDocumentFolder & Trim(Cells(ActiveCell.Row, 10).Value) & ".doc"
On Error Resume Next
Set Wd = GetObject(, "Word.application")
If Err = ERR_APP_NOTRUNNING Then
'Word not running so start it and open the document
Set Wd = New Word.Application
Set Doc = Wd.Documents.Open(WdDoc)
Wd.Visible = True
Else
'Word running
If Wd.Documents.Count = 0 Then
'no document open at all so open the Doc
Set Doc = Wd.Documents.Open(WdDoc)
Else
For Each Doc In Wd.Documents
'see if the particular document is already open
If Doc.Path & "\" & Doc.Name = WdDoc Then
'it is open so no need to look any further
docFound = True
Exit For
End If
Next
'particular document not found, so open it
If docFound = False Then
Set Doc = Wd.Documents.Open(WdDoc)
End If
End If
End If
'usercontrol is read only for the
'Word application whereas in
'Excel is it is read/write
Doc.UserControl = True
AppActivate "Microsoft Word"
Doc.Activate
End Sub
This all works in With Excel 2003 and Word 2003.
My question is will it also work with Word 97, 2000 and 2002?
I understand that doing:
Major:=0, Minor:=0
should make it pick the available Word Object Library, but is there anything
else
that could go wrong here?
Thanks for any advice.
RBS