Quote:
Originally Posted by macropod
Hi tinfanide,
It's a bit more involved than simply unchecking the "Microsoft Word Object Library 14.0" reference. See: http://support.microsoft.com/default...b;EN-US;245115
Amongst other things, you'd change all the Word-specific definitions to 'Object':
Dim wrdApp As Object, wrdDoc As Object, wrdTbl As Object, wrdTpl As Object
and you'd change:
Dim rng as Word.Range
to:
Dim rng as Range
or, better still:
Dim wdRng As Range
You might also need to change some Word-specific parameters to the values. The code will throw an error on any affected lines. Simply replace those parameters' names with their numeric values.
|
Yes, but the problem is
Code:
Dim wrdTbl As Table
Dim wrdRng As Range
those only work in Word VBA. But if I want to automate Word in Excel,
please see the comments below.
When I need to not set reference to any object library so that users can use my macro any version of the Office applications
(in my case, I write the macro in 2010, but want my users to run in 2007 and I don't want to start everything in Word Object Library 12.0)
Code:
Sub setTableBorders()
Dim wrdApp As Object
Set wrdApp = CreateObject("Word.Application")
Dim wrdDoc As Object
Set wrdDoc = wrdApp.documents.Add
wrdApp.Visible = True
' Dim wrdTbl As Table doesn't work in Excel VBA
Dim wrdTbl As Object
Set wrdTbl = wrdDoc.Tables.Add(Range:=wrdDoc.Range, _
NumRows:=4, _
NumColumns:=1)
With wrdTbl
For r = 1 To 4
.Cell(r, 1).Range.Text = ActiveSheet.Cells(r, 1).Value
Next r
' Starting from this line, it does not work
' Application-defined or Object-defined error
.Borders(wdBorderTop).LineStyle = wdLineStyleSingle
.Borders(wdBorderLeft).LineStyle = wdLineStyleSingle
.Borders(wdBorderBottom).LineStyle = wdLineStyleSingle
.Borders(wdBorderRight).LineStyle = wdLineStyleSingle
.Borders(wdBorderHorizontal).LineStyle = wdLineStyleSingle
.Borders(wdBorderVertical).LineStyle = wdLineStyleSingle
End With
End Sub
Many thanks for your illustration. Just feel sad that I can hardly find the late binding model (Word automation in Excel object library model) online and that's why I don't know how to style borders in Excel for word (no problem when I set Word borders in Word VBA)
Book1.xlsm
Please see the attachment.