Log in

View Full Version : [SLEEPER:] Create Command Button and format



dmeinder
04-16-2013, 01:30 PM
I've been working all day on this, and I can't get past the 2 errors:

Here is my code:


Private Sub Document_open()
'Add a command button to a new document
Dim doc As Word.Document
Dim shp As Word.InlineShape
Dim cmd As CommandButton
Set doc = ActiveDocument
Set shp = doc.Content.InlineShapes.AddOLEControl(ClassType:="Forms.CommandButton.1")
doc.InlineShapes(1).ConvertToShape
'On Error Resume Next
With CommandButton1
.AutoSize = False
.Caption = "Save & Close"
.Enabled = True
.Font = Calibri
.Font.Bold = True
.Font.Underline = False
.Font.Size = 10
.Height = 24
.Left = 500
.Locked = False
.TakeFocusOnClick = True
.Top = 25
.Width = 72
End With
End Sub

When the button is created I get a "Can't enter break mode at this time"....which only happens during step through.

The reason I converted it to a shape was so I could move it freely and access the properties that I need to modify.

Then when the code reaches the formatting i get the error 424 Object required.

Any suggestions are greatly appreciated!
Thanks!

fumei
04-16-2013, 07:09 PM
I can't even get past:

Dim cmd As CommandButton

Ummm, what version are you using.

Does this actually work for you:

Set shp = doc.Content.InlineShapes.AddOLEControl(ClassType:="Forms.CommandButton.1")

doc.InlineShapes(1).ConvertToShape

dmeinder
04-17-2013, 07:43 AM
The code is for MS word 2012. You can declare variable type in word as Commandbutton.....that element was left in while I was trying to assign an object as per the 424 error.

Yes the code works, it's converted to a shape so I can move it to the upper right corner of the doc.

The problem....as stated in OP is that on first run I get the 424 object required error. The button is created fine....

If I leave the button that was created in the document (now as "CommandButton1") and run the code again, a new button named CommandButton2 is created, and the original button is moved and renamed as originally intended.

So it's like the code isn't recognizing the newly created button on first pass....don't know if I need a timer, or function call, and am not sure how to declare the commandbutton1 as an object with the properties required to move it to the upper right corner.

If that doesn't make sense, can you please copy the code to a new MS word doc "ThisDocument" Object and step through once...(just hit continue when the "can't enter break mode comes up"), then step through again to see what I mean...

Thanks!

fumei
04-17-2013, 05:09 PM
Word 2012 is for Mac, yes? There may be issues with that. CommandButton is not a variable type available for me.

Dim cmd As CommandButton

errors out for me. It is NOT a valid instruction. BTW, are you using Option Explicit? Because if you are not, that explains why you do not get an error, but I do.

Now, if I create an ActiveX Control commanbutton, yes, it comes out a Commandbutton1. But to insert such via code you use InlineShape, as in your

Dim shp As Word.InlineShape

Set shp = doc.Content.InlineShapes.AddOLEControl(ClassType:="Forms.CommandButton.1")

THAT works for me.

BTW, you use cmd as CommandButton....but you NEVER actually use it!

If you are not using Option Explicit, please put it in and report back.

dmeinder
04-17-2013, 05:27 PM
Thanks for trying, but I already posted a response... Which I explain that I don't use that and it was left in from troubleshooting.... So I don't know why your hung up on that line.... Let it go!

I also mentioned in the first post what works and what doesn't...

The error 424 is obviously points to the problem... Anyway, I've found a solution.

BTW.. I made a mistake its 2010... But that doesn't matter.

fumei
04-17-2013, 05:32 PM
Sorry about that.

"Anyway, I've found a solution. "

OK, then be polite and post what the heck it is.

fumei
04-17-2013, 05:37 PM
Ah, I see what happened. You are NOT using Option Explicit. THAT is why you are not getting an error with

Dim cmd CommandButton.

If you were using Option Explicit (and you really really should), you WILL get an error on the CommandButton line...because it IS wrong.

dmeinder
04-17-2013, 06:02 PM
Hahaha... Yep you got it.

Actually it's not wrong... But just so you feel better... You got it

fumei
04-17-2013, 07:48 PM
So what is the solution, and did you actually try putting Option Explicit in?

fumei
04-17-2013, 08:51 PM
"Hahaha... Yep you got it.

Actually it's not wrong... But just so you feel better... You got it"

Actually...it IS wrong. If you put


Dim cmd CommandButton

in a module with Option Explicit (which REQUIRES you to use proper syntax and data types), it DOES error out. The code never progresses to any 424 error. The code stops.

If you do not use Option Explicit (which means you can put any garbage you want) it does not error out. VBA simply ignores it.


Dim cmd CommandButton is NOT a valid instruction. It is WRONG. It returns a "Compile Error: User-defined type not defined."

I certainly do not need any hahahah, or a "You got it" to feel better.

dmeinder
04-17-2013, 09:09 PM
.... Coming from someone who can't even quote properly on a forum... Haha

Maybe it works that way in your little dream land... But sure doesn't over here in the real world.

fumei
04-18-2013, 01:06 PM
Your last post was rude. You are new here, and perhaps starting off on the wrong foot. So maybe we can reboot this.

1. My "dream land" includes being a Microsoft Word MVP, over 50,000 posts on Word and Word VBA, including over 6,000 in this forum.

2. Forum etiquette asks for when someone comes up with a solution on their own, to post that solution for others. I requested that you do this, and you have ignored this.

3. Forum etiquette asks that when you are asked direct questions, that you respond. I asked diect questions about your use of option explicit, and you have ignored this.

I will reiterate. I put YOUR code in a ThisDocument module in 2010, with Option Explicit.

It errors out on the Dim cmd as CommandButton, with a "Compile Error: user-defined type not defined"

If I try it without Option Explicit, it does indeed go on to the 424 error as you described.

You are stating - with no evidence that you actually tested it - that I am in dream land. Well, then state that you actually tried it with Option Explicit.

".... Coming from someone who can't even quote properly on a forum... Haha"

Coming from someone who did not know what version (2012 ?), that is rude and uncalled for. Please try to reboot this and post your solution so you can possibly help others.

dmeinder
04-18-2013, 02:09 PM
Your last post was rude. You are new here, and perhaps starting off on the wrong foot. So maybe we can reboot this.

1. My "dream land" includes being a Microsoft Word MVP, over 50,000 posts on Word and Word VBA, including over 6,000 in this forum.

MS MVP...well that explains everything...

BTW, I found every one of your posts rude and obnoxious, so you're preachin' to the choir.

Did you notice what I did there....that's called a quote....I would have thought you would have learned that in your 50K+ posts, maybe that more accurately reflects your "dream land"...

fumei
04-18-2013, 03:03 PM
I only mentioned my credentials in order to state that I do know a bit about Word and VBA. You are stating that I am wrong without EVER stating you actually tested what I wrote.

So be it.

As this person refuses to share their solution when asked for it (a rudeness in itself), and they refuse to even mention anything to questions asked, I have formally asked the forum admin to review this thread (including my own postings) with the possibility of having them removed from the forum.

dmeinder
04-18-2013, 03:15 PM
If I posted your responses from the beginning....I'd sure as heck want them removed too, as you continued to avoid the problem, and I clearly explained it....twice!

You however, continued dwelling on a non issue.....when I clearly explained....twice.

(you see what I did there.....I explained twice again)

I'd be more than happy to post the solution.....it's really quite simple, and has nothing to do with variable declarations.....but I see no point to helping you when all you've done is avoid the real concern....and continue to belittle and patronize.

fumei
04-18-2013, 03:27 PM
You never stated if you used Option Explicit, which DOES have something to do with the problem. It is not a non-issue.

You have still never posted what you came up with as a solution. Do you not want to possibly help other people who may have the same problem? Apparently not. If that is the case, perhaps you should go to another forum.

In any case, I will let the admin decide. Oh, and BTW, it is not the responses I am asking to be reviewed for possible removal.

dmeinder
04-18-2013, 03:31 PM
jeeze let it go.....it has NOTHING to do with the problem...NOTHING NOTHING!

I have it working WITHOUT option explicit, and also it has NOTHING to do with a variable declaration i DON'T use.

fumei
04-18-2013, 03:35 PM
Then post (as repeatedly requested) what DID work.

dmeinder
04-19-2013, 07:19 AM
Sure....

Here is my code. It works great!


Private Sub Document_open()
Dim DocTest As String
'edit
On Error Resume Next
DocTest = ActiveDocument.Tables(1).Cell(1, 1).Range.Text
DocTest = Left(DocTest, Len(DocTest) - 2)
If Not DocTest = "Subject" Then
Dim doc As Word.Document
Set doc = ActiveDocument
With doc.Content.InlineShapes.AddOLEControl(ClassType:="Forms.CommandButton.1").ConvertToShape.OLEFormat.Object
.AutoSize = False
.Caption = "Save & Close"
.Enabled = True
.Font.Underline = False
.Font.Size = 11
.Height = 24
.Left = 500
.Locked = False
.TakeFocusOnClick = True
.Top = 25
.Width = 72
End With
On Error Resume Next
For Each o In ActiveDocument.Shapes
If Not o.OLEFormat.Object.Name = "CommandButton1" Then
o.Delete
End If
Next
End If
If DocTest = "Subject" Then
On Error Resume Next
For Each o In ActiveDocument.Shapes
If o.OLEFormat.Object.Name = "CommandButton1" Then
o.Delete
End If
Next
End If
End Sub

fumei
04-19-2013, 12:40 PM
Admin edit

I have formally requested you be removed from this forum.

dmeinder
04-19-2013, 12:41 PM
that's my code that works....as requested.

Did you try it yet?
Or maybe now that you know it has nothing to do with option explicit and variables declared as "CommandButton" as previously mentioned time and time again that it urks you?

Anyway, the code words great....and my point is proven, and I believe that if I'm removed from the forum it goes against everything I've been trying to say from the beginning. Further yet, that the code works great as intended, removal request would only be in spite of being patronized back....if you can dish it.....you gotta be able to take it.

lucas
04-20-2013, 12:49 PM
Admin edit

I have formally requested you be removed from this forum.

Done


if you can dish it.....you gotta be able to take it.

Do your dishing elsewhere.