Log in

View Full Version : Word and excel: Late binding



hans2607
08-18-2013, 02:21 AM
Hi
I have a problem concerning word/excel that I am hoping someone can help me with.
I have some data in an excelsheet that I want to put in a word document. I want to put the data in a table I the word document. I have written a code I excel that creates the document and puts the data in the table I the document.
My problem is this: My code crashes when I automate word via late binding instead of early binding. And I need use late binding. It seems that I can’t access the built in constants such as wdline, wdstory etc. I have posted part of the code below:


Sub test()
Dim myWord As Object

Set myWord = CreateObject("Word.application")

With myWord
.Documents.Add
.Visible = True
.ActiveDocument.Tables.Add Range:=.Selection.Range, numrows:=1, numcolumns:=2, _
DefaultTableBehavior:=wdWord9TableBehavior, AutoFitBehavior:=wdAutoFitContent
With .Selection
.Tables(1).UpdateAutoFormat
.Tables(1).Columns(1).SetWidth ColumnWidth:=50, rulerstyle:=wdAdjustNone
.Tables(1).Columns(2).SetWidth ColumnWidth:=250, rulerstyle:=wdAdjustNone
.Tables(1).Rows.SetHeight RowHeight:=30, HeightRule:=wdRowHeightExactly
.Tables(1).Range.Cells.VerticalAlignment = wdCellAlignVerticalCenter
End With
.Selection.typetext Text:="Hello"
.MoveRight unit:=wdCell 'this is where it crashes 'Runtime error 438. Object doesn't suport this property or method
End With


So my question is:

Is there a way to move the next cell in the table without the use of the constant: wdCell. I would like create new rows in the table as i move right?

Any help would be greatly appreciated

mark007
08-18-2013, 05:18 PM
Yes, just replace the constants with the numbers they represent.

Open word and type ?constantname in the immediate window and it will tell you what the underlying value is. For example ?wdCell would give 12. You can then either:

a) replace all wdCell with 12
b) define the constant wdCell as being 12 at the top of your module

Hope that helps.

:-)

fumei
08-19-2013, 04:24 PM
And I need use late binding. It seems that I can’t access the built in constants such as wdline, wdstory etc.

By definition late binding means you can not use the application built-in constants.

Why do you need to use late binding. In any case, if you do, you MUST use the numeric values.

mark007
08-21-2013, 01:56 AM
I find usual reason to need late binding is because it needs to work with all versions of Office.

Whilst you can use early binding and develop in lowest common denominator it is all too easy to save a version in a higher version that will then fail when loaded in an older one.