Consulting

Results 1 to 4 of 4

Thread: Word and excel: Late binding

  1. #1
    VBAX Newbie
    Joined
    Aug 2013
    Posts
    1
    Location

    Word and excel: Late binding

    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

  2. #2
    BoardCoder
    Licensed Coder
    VBAX Expert mark007's Avatar
    Joined
    May 2004
    Location
    Leeds, UK
    Posts
    622
    Location
    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.

    :-)
    "Computers are useless. They can only give you answers." - Pablo Picasso
    Mark Rowlinson FIA | The Code Net

  3. #3
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    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.

  4. #4
    BoardCoder
    Licensed Coder VBAX Expert mark007's Avatar
    Joined
    May 2004
    Location
    Leeds, UK
    Posts
    622
    Location
    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.
    "Computers are useless. They can only give you answers." - Pablo Picasso
    Mark Rowlinson FIA | The Code Net

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •