-3

When I try to run either of the following macros

Sub Label()
'
' Label Macro
'
'
    ActiveDocument.MailMerge.MainDocumentType = wdMailingLabels
    Selection.Tables(1).Style = "Table Grid"
    ActiveDocument.Save
End Sub
Sub labels()
'
' labels Macro
' creation of labels
'
    ActiveDocument.MailMerge.MainDocumentType = wdMailingLabels
    Selection.Tables(1).Style = "Table Grid"
End Sub

I get the following error

enter image description here

at the line saying

Selection.Tables(1).Style = "Table Grid"

What am I doing wrong?

YowE3K
  • 23,687
  • 7
  • 25
  • 40
dato datuashvili
  • 17,789
  • 59
  • 193
  • 310
  • 2
    Without seeing the code that is causing the error, we can't really do anything other than to so that you are referencing a member of a collection and that member doesn't exist. – YowE3K Oct 22 '17 at 20:40
  • @YowE3K i am running VBA and getting this error, what kind of code do you need? – dato datuashvili Oct 22 '17 at 20:42
  • 1
    Post the VBA ("macro") code, and tell us which line generates the error – YowE3K Oct 22 '17 at 20:47
  • 1
    Does your document include a table? – YowE3K Oct 22 '17 at 20:50
  • Selection.Tables(1).Style = "Table Grid" that is which causes error, i tried to draw label(avery usa letter) – dato datuashvili Oct 22 '17 at 20:51
  • And is the table within the `Selection`? – YowE3K Oct 22 '17 at 20:51
  • actual macros in my microsoft word document does not work at all, i just recorded how to create labels from mailing using macros, but it odes not work – dato datuashvili Oct 22 '17 at 20:54
  • You would do better to actively select the table e.g. With ActiveDocument.Tables(1) ......do stuff End With. Also, it looks like you have recorded two macros so you will only need one. Either Label or Labels. – QHarr Oct 22 '17 at 20:55
  • i deleted both of them and try again , but still no result – dato datuashvili Oct 22 '17 at 20:56
  • for instance label does not work to draw shape as well – dato datuashvili Oct 22 '17 at 20:58
  • `Label` isn't **trying** to draw a shape, so I'm not surprised that it doesn't do so! – YowE3K Oct 22 '17 at 20:59
  • no you did not undertand correctly, if i will do another macro for drawing shape, it does not work also , so in short macro does not work and do anything – dato datuashvili Oct 22 '17 at 21:01
  • Your posted macro works for me. Are you sure that you have a table within `Selection`? – YowE3K Oct 22 '17 at 21:02
  • If you have a table in your document, click on it. Then run your macro. (The one you posted, not something else that we haven't seen.) Does the macro still give an error, or does it work? – YowE3K Oct 22 '17 at 21:05
  • i dont have table, what i have did : i created simple macro and call it label, after that i go to mailing tab and choose labels, and formatted using table after that i stop recording – dato datuashvili Oct 22 '17 at 21:07

1 Answers1

2

You are referring to Selection.Tables, which means that you can refer only to the object that is currently selected. Your macro does not select anything however, so in the end it is referring to object that does not exist.

Try this topic to amend your code to correctly refer the table.

WAR
  • 149
  • 1
  • 1
  • 8
  • no no it does not help me, nobody understand me problem – dato datuashvili Oct 22 '17 at 21:22
  • 2
    You were asking what you did wrong so I explained the error to you. If that doesn't help, please provide more background to what you are trying to do overall (not only VBA). – WAR Oct 22 '17 at 21:29
  • imagine such kind of scenario, you are recording how to draw shape and using macro you are trying to redraw shape again, but it does not do is , so what is problem? – dato datuashvili Oct 22 '17 at 21:50
  • 1
    that is not what your original question is about. your question is about using VBA to apply the "Table Grid" style to a table. – jsotola Oct 23 '17 at 04:40
  • maybe your best course of action is to close this question and ask another one. – jsotola Oct 23 '17 at 04:44
  • Your code does not look like it contains functions used to draw tables. Also, macro recorder is not an efficient tool to learn what code to use. As you can see on this example, it does not return the actual syntax you would need to use. Understanding object drawing functions and applying them in trial & error could be helpful. – WAR Oct 23 '17 at 17:03