Jean-Guy
Mon Oct 02 12:00:55 CDT 2006
News.Microsoft.com was telling us:
News.Microsoft.com nous racontait que :
> Thanks that got me past the point of the "ActiveX component" error,
> but now it is giving me "Type Mismatch" errors on the placing of data
> into the word document from VB, even on my machine. If I switch it
> back to the early binding it works fine. Here is the code.
>
> Dim MyWord As Word.Application
> Dim Path As String
> Dim FileName As String
> Dim Extension As String
> Dim sWordDocument As String
> Dim WordDoc As Word.Document
>
> Path = "c:\"
> FileName = "BuildingChangeForm"
> Extension = ".Doc"
> sWordDocument = Path + FileName + Extension
> Set MyWord = New Word.Application
> Set WordDoc = MyWord.Documents.Open(sWordDocument)
> With MyWord
> .Visible = True
> .Options.Overtype = True
> .ActiveDocument.bookmarks("BuildingNumber").Select
> .Selection.TypeText USIComboRead(txtBuildingID)
> .ActiveDocument.bookmarks("BuildingName").Select
> .Selection.TypeText USIComboRead(cmbBuilding)
> .ActiveDocument.bookmarks("BuildingAddress").Select
> .Selection.TypeText txtBuildingAddress
> ERRORS ON THIS LINE. Type mismatch. I do have a bookmark at that
> spot and if I comment out that line then it happens a few lines later
> and if I comment out that line, it happens a few lines later etc.
I am not sure if this will solve your problem or not, but you should avoid
the Selection object as much as possible, especially when automating
documents.
Replace each pair of line by a single one like this:
.ActiveDocument.bookmarks("BuildingNumber").Select
.Selection.TypeText USIComboRead(txtBuildingID)
to be replaced by:
.ActiveDocument.bookmarks("BuildingNumber").Range.Text =
USIComboRead(txtBuildingID)
Also, Jean_Yves suggested that you use:
Dim wdlApp As Object ' Declare variable to hold the reference.
Set wdApp = CreateObject("Word.application")
but you are using:
Dim MyWord As Word.Application
Set MyWord = New Word.Application
Any particular reason?
For a more on late binding, see:
http://word.mvps.org/faqs/interdev/earlyvslatebinding.htm
Moreover, do not use ActiveDocument, especially when there might be more
than one document open. You already have a Document object, use it.
Declare it like this:
Dim WordDoc As Object
and use it like this:
Set MyWord = CreateObject("Word.application")
With MyWord
Set WordDoc = .Documents.Open(sWordDocument)
.Visible = True
'.Options.Overtype = True
'Is this necessary? I do not think so
'If it is, do not forget to reset the user setting
'I hate it when some code/application changes
'my settings without telling me
'Something like this
Dim boolUserOverType As Boolean
boolUserOverType = Options.Overtype
.Options.Overtype = True
With WordDoc
.Bookmarks("BuildingNumber").Range.Text =
USIComboRead(txtBuildingID)
.Bookmarks("BuildingName").Range.Text = USIComboRead(cmbBuilding)
'Etc.
End With
'If necessary:
.Options.Overtype = boolUserOverType
End With
--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
jmarcilREMOVE@CAPSsympatico.caTHISTOO
Word MVP site:
http://www.word.mvps.org