Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

MS Word Macro error

Status
Not open for further replies.

347pg

Technical User
Jan 22, 2009
31
0
0
US
I have an Access db that outputs a table (from a query) to MS Word in RFT format. I've made 3 macros to apply to the table. The first one saves it as a macro enable compatibility doc (docm). The next macro changes the font in the whole table to Times NR 10. These two work great. The third macro tries to adjust the column widths but I get the following error:

Run-time error '4605'
The SelectColumn method or property is not available because some or all of the object does not refer to a table.

I am using the Layout tab to select the table, then the properties, then column widths to record the macro but it's not working.

Here is the macro as recorded:

Sub MTL_MacroColumns()
'
' MTL_MacroColumns Macro
' Changes column widths
'
Selection.Tables(1).Select
Selection.Columns.PreferredWidthType = wdPreferredWidthAuto
Selection.Columns.PreferredWidth = 0
Selection.Move Unit:=wdColumn, Count:=1
Selection.SelectColumn
Selection.Columns.PreferredWidthType = wdPreferredWidthPoints
Selection.Columns.PreferredWidth = InchesToPoints(1)
Selection.Move Unit:=wdColumn, Count:=1
Selection.SelectColumn
Selection.Columns.PreferredWidthType = wdPreferredWidthPoints
Selection.Columns.PreferredWidth = InchesToPoints(3.32)
Selection.Move Unit:=wdColumn, Count:=1
Selection.SelectColumn
Selection.Columns.PreferredWidthType = wdPreferredWidthPoints
Selection.Columns.PreferredWidth = InchesToPoints(0.69)
Selection.Move Unit:=wdColumn, Count:=1
Selection.SelectColumn
Selection.Columns.PreferredWidthType = wdPreferredWidthPoints
Selection.Columns.PreferredWidth = InchesToPoints(0.9)
Selection.Move Unit:=wdColumn, Count:=1
Selection.SelectColumn
Selection.Columns.PreferredWidthType = wdPreferredWidthPoints
Selection.Columns.PreferredWidth = InchesToPoints(0.9)
Selection.Move Unit:=wdColumn, Count:=1
Selection.SelectColumn
Selection.Columns.PreferredWidthType = wdPreferredWidthPoints
Selection.Columns.PreferredWidth = InchesToPoints(0.69)
Selection.Move Unit:=wdColumn, Count:=1
Selection.SelectColumn
End Sub


Anybody have any ideas?
 
Your macro has evidently been developed via Word's macro recorder. Amongst other things, that generates very inefficient code and there's nothing in it to tell Access that you're working with a Word document.

Try something based on:
Code:
Sub MTL_MacroColumns()
'
' MTL_MacroColumns Macro
' Changes column widths
'
Dim Doc As Word.Document
Set Doc = Word.ActiveDocument
With Doc.Tables(1)
    .Columns.PreferredWidthType = wdPreferredWidthPoints
    .Columns(1).PreferredWidth = InchesToPoints(1)
    .Columns(2).PreferredWidth = InchesToPoints(3.32)
    .Columns(3).PreferredWidth = InchesToPoints(0.69)
    .Columns(4).PreferredWidth = InchesToPoints(0.9)
    .Columns(5).PreferredWidth = InchesToPoints(0.9)
    .Columns(6).PreferredWidth = InchesToPoints(0.69)
End Sub
Note: I haven't incorporated all the required Word automation code - just enough to demonstrate how to refer to the table correctly.

Cheers
Paul Edstein
[MS MVP - Word]
 
Is it possible to have access run a Word macro after it has open the rtf file? I think the coding would be a little cleaner if I could do this as opposed to having Access try to modify the open word doc. I need to:
apply a macro to save the rtf as a .docm file
apply a macro to change the font of the table
apply a macro to change column widths
apply a macro to modify borders and shading
apply a macro to insert a row above the table, merge the cells and apply text heading

I think this would be too hard to do in Access and wanted Access to run a Word macro on the open document
 
apply a macro to save the rtf as a .docm file
Why docm? Unless you're adding a vba module to the document via your Access code, I can't see the need for this - docx would do.
I think this would be too hard to do in Access and wanted Access to run a Word macro on the open document
Since you're already creating the document from Access and doing some manipulation (eg changing fonts), that suggests you already have the code that instantiates the Word session. All you need to do, then, is to continue modifying the document within that session - and that is what the code I posted is predicated upon.

Cheers
Paul Edstein
[MS MVP - Word]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top