PDA

View Full Version : Error 4605: This command is not available.



garynewport
05-16-2016, 07:13 AM
I have a Word template running some VBA and on one machine the file runs without issue. Yet on my other unit (exactly the same install of Windows and the same install of Office) I get the Error 4605: This command is not available notice.

If I go to debug then it always stops on this line...


.Columns(1).PreferredWidth = CentimetersToPoints(4.5)

As stated, this runs fine of some units and not on others.

This is a business network, by the way.

garynewport
05-16-2016, 08:32 AM
Interestingly, the document stops on this line and offers debug as an option.

If I select DEBUG and step through then the code works fine for this particular line and any subsequent CentimetersToPoints statements UNLESS they are preceded by


With ActiveDocument.Tables(n)

Where n is any integer above 0.

gmayor
05-16-2016, 09:58 PM
You have not posted sufficient of your code to evaluate it, but the following may help

Dim n As Long: n = 1
If n > ActiveDocument.Tables.Count Then
MsgBox "Table" & n & " is not present in the document"
End If
With ActiveDocument.Tables(n)
.Columns(1).PreferredWidth = CentimetersToPoints(4.5)
End With

garynewport
05-17-2016, 12:29 AM
Thanks for replying.

The table does not exist in the first instance; and you are correct, I should have given more code.



ActiveDocument.Paragraphs.Add
Set myRange = ActiveDocument.Content
myRange.Collapse Direction:=wdCollapseEnd

' Create and populate the first table on the form
ActiveDocument.Tables.Add Range:=myRange, numRows:=2, numColumns:=2

With ActiveDocument.Tables(1)

.Columns(1).PreferredWidth = CentimetersToPoints(4.5)
.Columns(1).Shading.BackgroundPatternColor = RGB(192, 192, 192)

.Columns(1).Cells(1).Range.ParagraphFormat.Alignment = wdAlignParagraphRight
.Columns(1).Cells(1).Range.Bold = True
.Columns(1).Cells(1).Range.Text = "Assignment Title:"

.Columns(1).Cells(2).Range.ParagraphFormat.Alignment = wdAlignParagraphRight
.Columns(1).Cells(2).Range.Bold = True
.Columns(1).Cells(2).Range.Text = "Assessor:"

.Columns(2).PreferredWidth = CentimetersToPoints(11.5)


So, as you can see the code creates the table and then sets the properties. The thing is, the code works on one computer but not on another. It is also not a case that it alternates (occasionally working on either, or occasional crashing on both). Equally, if I step through via debug then the code works fine.

I thought VBA was synchronously processed but wonder if there is an element of asynchronous activity here; the table is still being created when this line is executed and so, for that particular instance, the table does not appear to exist when it is trying to define the table size?

If so then is there a way of forcing VBA to wait for the conclusion of an instruction prior to moving on to the next?

Or am I far from the mark here?

gmayor
05-17-2016, 02:43 AM
Your code segment works here, and the code is processed sequentially, though I would prefer that you named the table, and you do need to add End With
Is there some reason why you think there may be a holdup?
There would be an error if the macro was run again (or a separate table was added) and there was no separation between the tables.


Dim myRange As Range
Dim oTable As Table
ActiveDocument.Range.InsertParagraphAfter
Set myRange = ActiveDocument.Range
myRange.Collapse direction:=wdCollapseEnd

' Create and populate the first table on the form
Set oTable = ActiveDocument.Tables.Add(Range:=myRange, numRows:=2, numColumns:=2)

With oTable
.Columns(1).PreferredWidth = CentimetersToPoints(4.5)
.Columns(1).Shading.BackgroundPatternColor = RGB(192, 192, 192)
.Columns(1).Cells(1).Range.ParagraphFormat.Alignment = wdAlignParagraphRight
.Columns(1).Cells(1).Range.Bold = True
.Columns(1).Cells(1).Range.Text = "Assignment Title:"
.Columns(1).Cells(2).Range.ParagraphFormat.Alignment = wdAlignParagraphRight
.Columns(1).Cells(2).Range.Bold = True
.Columns(1).Cells(2).Range.Text = "Assessor:"
.Columns(2).PreferredWidth = CentimetersToPoints(11.5)
End With

garynewport
05-17-2016, 03:59 AM
Hi and thanks for replying again. :)

The code segment works fine on my one workstation. It fails on my other workstation and, on debug, highlights the first CentimetersToPoints(4.5) statement immediately after With oTable.

I do have the End With statement; right at the end of that code segment?

I feel it might have been a time issue because if I use F8 during debug mode then the code executes fine. In fact if I tell the code to continue running it keeps reporting an error on the first line after a new table has been created but then continues to run without issue once I instruct it to continue.

The macro is not run again, it is a lengthy but simple sequential operation and all tables are separated. As I say, it works perfectly on my one workstation and stutteringly on my other one.

The whole subroutine code is here...



Private Sub cmd_next2_Click() ' Next button (2)
Dim i As Integer, k As Integer
Dim s_aims As String

k = 0

ReDim aims(lst_aims.ListCount - 1)
For i = 0 To lst_aims.ListCount - 1

If lst_aims.Selected(i) = True Then

aims(k) = lst_aims.List(i, 0)
k = k + 1

If s_aims = "" Then
s_aims = (i + 1) & ". " & lst_aims.List(i, 3)
Else
s_aims = s_aims & vbCrLf & (i + 1) & ". " & lst_aims.List(i, 3)
End If

End If

Next i

ActiveDocument.Paragraphs.Add
Set myRange = ActiveDocument.Content
myRange.Collapse Direction:=wdCollapseEnd

' Create and populate the first table on the form
ActiveDocument.Tables.Add Range:=myRange, numRows:=2, numColumns:=2

With ActiveDocument.Tables(1)

.Columns(1).PreferredWidth = CentimetersToPoints(4.5)
.Columns(1).Shading.BackgroundPatternColor = RGB(192, 192, 192)

.Columns(1).Cells(1).Range.ParagraphFormat.Alignment = wdAlignParagraphRight
.Columns(1).Cells(1).Range.Bold = True
.Columns(1).Cells(1).Range.Text = "Assignment Title:"

.Columns(1).Cells(2).Range.ParagraphFormat.Alignment = wdAlignParagraphRight
.Columns(1).Cells(2).Range.Bold = True
.Columns(1).Cells(2).Range.Text = "Assessor:"

.Columns(2).PreferredWidth = CentimetersToPoints(11.5)
.Columns(2).Cells(1).Range.Text = txt_number.value & ": " & txt_title.value
.Columns(2).Cells(2).Range.Text = cmb_assessor.value

.Borders.InsideLineStyle = wdLineStyleSingle
.Borders.OutsideLineStyle = wdLineStyleSingle

End With

' Create and populate the second table on the form - minus specific data (labels only)
ActiveDocument.Paragraphs.Add
Set myRange = ActiveDocument.Content
myRange.Collapse Direction:=wdCollapseEnd

ActiveDocument.Tables.Add Range:=myRange, numRows:=3, numColumns:=2
With ActiveDocument.Tables(2)
.Columns(1).PreferredWidth = CentimetersToPoints(4.5)
.Columns(1).Shading.BackgroundPatternColor = RGB(192, 192, 192)
.Columns(1).Cells(1).Range.ParagraphFormat.Alignment = wdAlignParagraphRight
.Columns(1).Cells(1).Range.Bold = True
.Columns(1).Cells(1).Range.Text = "Date Issued:"
.Columns(1).Cells(2).Range.ParagraphFormat.Alignment = wdAlignParagraphRight
.Columns(1).Cells(2).Range.Bold = True
.Columns(1).Cells(2).Range.Text = "Hand In Date:"
.Columns(1).Cells(3).Range.ParagraphFormat.Alignment = wdAlignParagraphRight
.Columns(1).Cells(3).Range.Bold = True
.Columns(1).Cells(3).Range.Text = "Duration (Approx.):"
.Columns(2).PreferredWidth = CentimetersToPoints(11.5)
.Columns(2).Cells(2).Split 1, 3
.Rows(2).Cells(3).Range.ParagraphFormat.Alignment = wdAlignParagraphRight
.Rows(2).Cells(3).Shading.BackgroundPatternColor = RGB(192, 192, 192)
.Rows(2).Cells(3).Range.Bold = True
.Rows(2).Cells(3).Range.Text = "Assessment Date:"
.Borders.InsideLineStyle = wdLineStyleSingle
.Borders.OutsideLineStyle = wdLineStyleSingle
End With
' Create and populate the third table on the form
ActiveDocument.Paragraphs.Add
Set myRange = ActiveDocument.Content
myRange.Collapse Direction:=wdCollapseEnd
ActiveDocument.Tables.Add Range:=myRange, numRows:=3, numColumns:=2
With ActiveDocument.Tables(3)
.Columns(1).PreferredWidth = CentimetersToPoints(4.5)
.Columns(1).Shading.BackgroundPatternColor = RGB(192, 192, 192)
.Columns(1).Cells(1).Range.ParagraphFormat.Alignment = wdAlignParagraphRight
.Columns(1).Cells(1).Range.Bold = True
.Columns(1).Cells(1).Range.Text = "Qualification Covered:"
.Columns(1).Cells(2).Range.ParagraphFormat.Alignment = wdAlignParagraphRight
.Columns(1).Cells(2).Range.Bold = True
.Columns(1).Cells(2).Range.Text = "Units Covered:"
.Columns(1).Cells(3).Range.ParagraphFormat.Alignment = wdAlignParagraphRight
.Columns(1).Cells(3).Range.Bold = True
.Columns(1).Cells(3).Range.Text = "Learning Aims Covered:"
.Columns(2).PreferredWidth = CentimetersToPoints(11.5)
.Columns(2).Cells(1).Range.Text = myCourse.title
.Columns(2).Cells(2).Range.Text = lst_unit.List(lst_unit.ListIndex, 1)
.Columns(2).Cells(3).Range.Text = s_aims
.Borders.InsideLineStyle = wdLineStyleSingle
.Borders.OutsideLineStyle = wdLineStyleSingle
End With

Me.tabs_info.Pages(2).Visible = True
Me.tabs_info.Pages(1).Visible = False

If edit_data = True Then
set_dates issue_date, handin_date, assessment_date
Else
set_dates Date, DateAdd("d", 14, Date), DateAdd("d", 14, d_handin.value)
End If
End Sub




Oooo, can you name the tables? Never found that one; that'd be nicer (and easier) to work with. :)

garynewport
05-23-2016, 12:32 AM
Just wondering if anyone has any advice on this one?

gmayor
05-23-2016, 04:09 AM
Yes you can (and should) name the tables. The following is your macro, with the data that cannot be established from the listing commented out. It creates three formatted (and named) tables


Option Explicit

Private Sub cmd_next2_Click() ' Next button (2)
Dim i As Integer, k As Integer
Dim s_aims As String
Dim oDoc As Document
Dim oRng As Range
Dim oTable1 As Table
Dim oTable2 As Table
Dim oTable3 As Table

Set oDoc = ActiveDocument

'k = 0

'ReDim aims(lst_aims.ListCount - 1)
'For i = 0 To lst_aims.ListCount - 1

' If lst_aims.Selected(i) = True Then

' aims(k) = lst_aims.List(i, 0)
' k = k + 1

' If s_aims = "" Then
' s_aims = (i + 1) & ". " & lst_aims.List(i, 3)
' Else
' s_aims = s_aims & vbCrLf & (i + 1) & ". " & lst_aims.List(i, 3)
' End If

' End If

'Next i

oDoc.Range.InsertParagraphAfter
Set oRng = oDoc.Range
oRng.Collapse 0

Set oTable1 = oDoc.Tables.Add(Range:=oRng, numRows:=2, numColumns:=2)

With oTable1
.Columns(1).PreferredWidth = CentimetersToPoints(4.5)
.Columns(1).Shading.BackgroundPatternColor = RGB(192, 192, 192)
.Columns(1).Cells(1).Range.ParagraphFormat.Alignment = wdAlignParagraphRight
.Columns(1).Cells(1).Range.Bold = True
.Columns(1).Cells(1).Range.Text = "Assignment Title:"

.Columns(1).Cells(2).Range.ParagraphFormat.Alignment = wdAlignParagraphRight
.Columns(1).Cells(2).Range.Bold = True
.Columns(1).Cells(2).Range.Text = "Assessor:"

.Columns(2).PreferredWidth = CentimetersToPoints(11.5)
'.Columns(2).Cells(1).Range.Text = txt_number.Value & ": " & txt_title.Value
'.Columns(2).Cells(2).Range.Text = cmb_assessor.Value

.Borders.InsideLineStyle = wdLineStyleSingle
.Borders.OutsideLineStyle = wdLineStyleSingle
End With

oDoc.Range.InsertParagraphAfter
Set oRng = oDoc.Range
oRng.Collapse 0

' Create and populate the second table on the form
Set oTable2 = oDoc.Tables.Add(Range:=oRng, numRows:=3, numColumns:=2)
With oTable2
.Columns(1).PreferredWidth = CentimetersToPoints(4.5)
.Columns(1).Shading.BackgroundPatternColor = RGB(192, 192, 192)
.Columns(1).Cells(1).Range.ParagraphFormat.Alignment = wdAlignParagraphRight
.Columns(1).Cells(1).Range.Bold = True
.Columns(1).Cells(1).Range.Text = "Date Issued:"
.Columns(1).Cells(2).Range.ParagraphFormat.Alignment = wdAlignParagraphRight
.Columns(1).Cells(2).Range.Bold = True
.Columns(1).Cells(2).Range.Text = "Hand-In Date:"
.Columns(1).Cells(3).Range.ParagraphFormat.Alignment = wdAlignParagraphRight
.Columns(1).Cells(3).Range.Bold = True
.Columns(1).Cells(3).Range.Text = "Duration (Approx.):"
.Columns(2).PreferredWidth = CentimetersToPoints(11.5)
.Columns(2).Cells(2).Split 1, 3
.Rows(2).Cells(3).Range.ParagraphFormat.Alignment = wdAlignParagraphRight
.Rows(2).Cells(3).Shading.BackgroundPatternColor = RGB(192, 192, 192)
.Rows(2).Cells(3).Range.Bold = True
.Rows(2).Cells(3).Range.Text = "Assessment Date:"
.Borders.InsideLineStyle = wdLineStyleSingle
.Borders.OutsideLineStyle = wdLineStyleSingle
End With
' Create and populate the third table on the form
oDoc.Range.InsertParagraphAfter
Set oRng = oDoc.Range
oRng.Collapse 0

' Create and populate the third table on the form
Set oTable3 = oDoc.Tables.Add(Range:=oRng, numRows:=3, numColumns:=2)
With oTable3
.Columns(1).PreferredWidth = CentimetersToPoints(4.5)
.Columns(1).Shading.BackgroundPatternColor = RGB(192, 192, 192)
.Columns(1).Cells(1).Range.ParagraphFormat.Alignment = wdAlignParagraphRight
.Columns(1).Cells(1).Range.Bold = True
.Columns(1).Cells(1).Range.Text = "Qualification Covered:"
.Columns(1).Cells(2).Range.ParagraphFormat.Alignment = wdAlignParagraphRight
.Columns(1).Cells(2).Range.Bold = True
.Columns(1).Cells(2).Range.Text = "Units Covered:"
.Columns(1).Cells(3).Range.ParagraphFormat.Alignment = wdAlignParagraphRight
.Columns(1).Cells(3).Range.Bold = True
.Columns(1).Cells(3).Range.Text = "Learning Aims Covered:"
.Columns(2).PreferredWidth = CentimetersToPoints(11.5)
'.Columns(2).Cells(1).Range.Text = myCourse.Title
'.Columns(2).Cells(2).Range.Text = lst_unit.List(lst_unit.ListIndex, 1)
'.Columns(2).Cells(3).Range.Text = s_aims
.Borders.InsideLineStyle = wdLineStyleSingle
.Borders.OutsideLineStyle = wdLineStyleSingle
End With

'Me.tabs_info.Pages(2).Visible = True
'Me.tabs_info.Pages(1).Visible = False

'If edit_data = True Then
' set_dates issue_date, handin_date, assessment_date
'Else
' set_dates Date, DateAdd("d", 14, Date), DateAdd("d", 14, d_handin.Value)
'End If
End Sub

garynewport
05-23-2016, 06:44 AM
Since I am referring to differing tables within differing routines, would it make sense to define all of my tables as globals?

I hate to ask this, since I avoid globals unless essential but it would appear that this might be a good moment to use them.