epuglise
asked on
Create Button Macro-- Move to End of Document
I have a macro that creates a button, but I want it to put the button at the end of the document:
Sub AddCommandButton(ButtonNam e As String)
'************************* ********** ********** **
'*Note: For this to work, you must have a reference to Microsoft's VB for Apps Ext.
'* 1. In the VBA IDE, click on the 'Tools' menu item and select 'References...'
'* from the drop-down list.
'* 2. In the 'Available References:' box of the 'References' window, scroll down
'* to 'Microsoft Visual Basic for Applications Extensibility n.n'
'* 3. Click on the check box beside the name and then click OK near the top right
'* of the dialog box.
'************************* ********** ********** ***
Dim vbp As VBProject
Dim vbComp As VBComponent
Dim ILS As InlineShape
********'This doesn't seem to do it:
Selection.EndKey unit:=wdStory
Set ILS = ActiveDocument.InlineShape s.AddOLECo ntrol("For ms.Command Button.1")
Select Case ButtonName
Case "GenDocs"
ILS.OLEFormat.Object.Capti on = "Generate Documents"
ILS.OLEFormat.Object.Name = "GenerateDocs"
ILS.OLEFormat.Object.AutoS ize = True
Set vbp = ActiveDocument.VBProject
Set vbComp = vbp.VBComponents("ThisDocu ment")
vbComp.CodeModule.AddFromS tring "Sub GenerateDocs_Click()" & vbCrLf & _
" Call GenDocs " & vbCrLf & _
"End Sub"
Case "UpdateColor"
ILS.OLEFormat.Object.Capti on = "Update Cell Colors"
ILS.OLEFormat.Object.Name = "UpdateColor"
ILS.OLEFormat.Object.AutoS ize = True
Set vbp = ActiveDocument.VBProject
Set vbComp = vbp.VBComponents("ThisDocu ment")
vbComp.CodeModule.AddFromS tring "Sub UpdateColor_Click()" & vbCrLf & _
" UpdateCellColors" & vbCrLf & _
"End Sub"
Case "NoColor"
ILS.OLEFormat.Object.Capti on = "Remove Cell Colors"
ILS.OLEFormat.Object.Name = "NoColor"
ILS.OLEFormat.Object.AutoS ize = True
Set vbp = ActiveDocument.VBProject
Set vbComp = vbp.VBComponents("ThisDocu ment")
vbComp.CodeModule.AddFromS tring "Sub NoColor_Click()" & vbCrLf & _
" RemoveColor" & vbCrLf & _
"End Sub"
End Select
End Sub
Sub AddCommandButton(ButtonNam
'*************************
'*Note: For this to work, you must have a reference to Microsoft's VB for Apps Ext.
'* 1. In the VBA IDE, click on the 'Tools' menu item and select 'References...'
'* from the drop-down list.
'* 2. In the 'Available References:' box of the 'References' window, scroll down
'* to 'Microsoft Visual Basic for Applications Extensibility n.n'
'* 3. Click on the check box beside the name and then click OK near the top right
'* of the dialog box.
'*************************
Dim vbp As VBProject
Dim vbComp As VBComponent
Dim ILS As InlineShape
********'This doesn't seem to do it:
Selection.EndKey unit:=wdStory
Set ILS = ActiveDocument.InlineShape
Select Case ButtonName
Case "GenDocs"
ILS.OLEFormat.Object.Capti
ILS.OLEFormat.Object.Name = "GenerateDocs"
ILS.OLEFormat.Object.AutoS
Set vbp = ActiveDocument.VBProject
Set vbComp = vbp.VBComponents("ThisDocu
vbComp.CodeModule.AddFromS
" Call GenDocs " & vbCrLf & _
"End Sub"
Case "UpdateColor"
ILS.OLEFormat.Object.Capti
ILS.OLEFormat.Object.Name = "UpdateColor"
ILS.OLEFormat.Object.AutoS
Set vbp = ActiveDocument.VBProject
Set vbComp = vbp.VBComponents("ThisDocu
vbComp.CodeModule.AddFromS
" UpdateCellColors" & vbCrLf & _
"End Sub"
Case "NoColor"
ILS.OLEFormat.Object.Capti
ILS.OLEFormat.Object.Name = "NoColor"
ILS.OLEFormat.Object.AutoS
Set vbp = ActiveDocument.VBProject
Set vbComp = vbp.VBComponents("ThisDocu
vbComp.CodeModule.AddFromS
" RemoveColor" & vbCrLf & _
"End Sub"
End Select
End Sub
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
does it help if you include the extend attribute like
Selection.HomeKey Unit:=wdStory ' make sure you're at the start of the document
Selection.EndKey Unit:=wdStory, Extend:=wdMove ' then move to the end
Selection.HomeKey Unit:=wdStory ' make sure you're at the start of the document
Selection.EndKey Unit:=wdStory, Extend:=wdMove ' then move to the end
ASKER
Boy, it really doesn't like it if you're putting in more than one button. CrrrrrrrrAAAAAASH!
Selection.EndKey Unit:=wdStory
Maybe you just need to reboot epuglise and try again :)
Truly...
Maybe you just need to reboot epuglise and try again :)
Truly...
Hi JO,
not sure why but it doesn't seem to work
Selection.EndKey unit:=wdStory
if you break right after the above line and look at the doc the cursor will be at the end of the doc. but the following line still creates the button at the top of the doc
Set ILS = ActiveDocument.InlineShape s.AddOLECo ntrol("For ms.Command Button.1")
not sure why but it doesn't seem to work
Selection.EndKey unit:=wdStory
if you break right after the above line and look at the doc the cursor will be at the end of the doc. but the following line still creates the button at the top of the doc
Set ILS = ActiveDocument.InlineShape
expression.AddOLEControl(C lassType, Range)
i guess since the range parameter is being left blank, its defaulting to 0 [activedocument.range(0,0) ] Irrespective of where the cursor is.
it works if the range is mentioned
Set ILS = ActiveDocument.InlineShape s.AddOLECo ntrol("For ms.Command Button.1", ActiveDocument.Bookmarks(" \EndOfDoc" ).Range)
i guess since the range parameter is being left blank, its defaulting to 0 [activedocument.range(0,0)
it works if the range is mentioned
Set ILS = ActiveDocument.InlineShape
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Hey Rajesh -
BTW - how'd you like the write up in the EE newsletter? I hope it was okay. Well deserved... wish I could have said more.
BTW - how'd you like the write up in the EE newsletter? I hope it was okay. Well deserved... wish I could have said more.
yes, changing activedocument.inlineshape s to selection.inlineshapes does the trick :)
Set ILS = ActiveDocument.InlineShape s.AddOLECo ntrol("For ms.Command Button.1")
Set ILS = Selection.InlineShapes.Add OLEControl ("Forms.Co mmandButto n.1")
Set ILS = ActiveDocument.InlineShape
Set ILS = Selection.InlineShapes.Add
it was great, thanks :) (all my friends are tired of hearing me brag about it ;)
LOL - good for you Rajesh! You have every reason to be proud.
So Elizabeth - try this code:
Sub AddCommandButton(ButtonNam e As String)
'************************* ********** ********** **
'*Note: For this to work, you must have a reference to Microsoft's VB for Apps Ext.
'* 1. In the VBA IDE, click on the 'Tools' menu item and select 'References...'
'* from the drop-down list.
'* 2. In the 'Available References:' box of the 'References' window, scroll down
'* to 'Microsoft Visual Basic for Applications Extensibility n.n'
'* 3. Click on the check box beside the name and then click OK near the top right
'* of the dialog box.
'************************* ********** ********** ***
Dim vbp As VBProject
Dim vbComp As VBComponent
Dim ILS As InlineShape
Selection.EndKey unit:=wdStory
Set ILS = Selection.InlineShapes.Add OLEControl ("Forms.Co mmandButto n.1")
Select Case ButtonName
Case "GenDocs"
ILS.OLEFormat.Object.Capti on = "Generate Documents"
ILS.OLEFormat.Object.Name = "GenerateDocs"
ILS.OLEFormat.Object.AutoS ize = True
Set vbp = ActiveDocument.VBProject
Set vbComp = vbp.VBComponents("ThisDocu ment")
vbComp.CodeModule.AddFromS tring "Sub GenerateDocs_Click()" & vbCrLf & _
" Call GenDocs " & vbCrLf & _
"End Sub"
Case "UpdateColor"
ILS.OLEFormat.Object.Capti on = "Update Cell Colors"
ILS.OLEFormat.Object.Name = "UpdateColor"
ILS.OLEFormat.Object.AutoS ize = True
Set vbp = ActiveDocument.VBProject
Set vbComp = vbp.VBComponents("ThisDocu ment")
vbComp.CodeModule.AddFromS tring "Sub UpdateColor_Click()" & vbCrLf & _
" UpdateCellColors" & vbCrLf & _
"End Sub"
Case "NoColor"
ILS.OLEFormat.Object.Capti on = "Remove Cell Colors"
ILS.OLEFormat.Object.Name = "NoColor"
ILS.OLEFormat.Object.AutoS ize = True
Set vbp = ActiveDocument.VBProject
Set vbComp = vbp.VBComponents("ThisDocu ment")
vbComp.CodeModule.AddFromS tring "Sub NoColor_Click()" & vbCrLf & _
" RemoveColor" & vbCrLf & _
"End Sub"
End Select
End Sub
Sub AddCommandButton(ButtonNam
'*************************
'*Note: For this to work, you must have a reference to Microsoft's VB for Apps Ext.
'* 1. In the VBA IDE, click on the 'Tools' menu item and select 'References...'
'* from the drop-down list.
'* 2. In the 'Available References:' box of the 'References' window, scroll down
'* to 'Microsoft Visual Basic for Applications Extensibility n.n'
'* 3. Click on the check box beside the name and then click OK near the top right
'* of the dialog box.
'*************************
Dim vbp As VBProject
Dim vbComp As VBComponent
Dim ILS As InlineShape
Selection.EndKey unit:=wdStory
Set ILS = Selection.InlineShapes.Add
Select Case ButtonName
Case "GenDocs"
ILS.OLEFormat.Object.Capti
ILS.OLEFormat.Object.Name = "GenerateDocs"
ILS.OLEFormat.Object.AutoS
Set vbp = ActiveDocument.VBProject
Set vbComp = vbp.VBComponents("ThisDocu
vbComp.CodeModule.AddFromS
" Call GenDocs " & vbCrLf & _
"End Sub"
Case "UpdateColor"
ILS.OLEFormat.Object.Capti
ILS.OLEFormat.Object.Name = "UpdateColor"
ILS.OLEFormat.Object.AutoS
Set vbp = ActiveDocument.VBProject
Set vbComp = vbp.VBComponents("ThisDocu
vbComp.CodeModule.AddFromS
" UpdateCellColors" & vbCrLf & _
"End Sub"
Case "NoColor"
ILS.OLEFormat.Object.Capti
ILS.OLEFormat.Object.Name = "NoColor"
ILS.OLEFormat.Object.AutoS
Set vbp = ActiveDocument.VBProject
Set vbComp = vbp.VBComponents("ThisDocu
vbComp.CodeModule.AddFromS
" RemoveColor" & vbCrLf & _
"End Sub"
End Select
End Sub
ASKER
y'all-- this worked great! I can run the macro, let it execute and then execute it again for each of the three types of buttons; however, when I call this button-creating macro from another macro, e.g.,
Call AddCommandButton("GenDocs" )
Call AddCommandButton("UpdateCe llColor")
Call AddCommandButton("NoColor" )
Word crashes every time. How can I put a pause in there so whatever it is processing can complete before the next command button call is made? Or is there a better approach?
thx!
e
Call AddCommandButton("GenDocs"
Call AddCommandButton("UpdateCe
Call AddCommandButton("NoColor"
Word crashes every time. How can I put a pause in there so whatever it is processing can complete before the next command button call is made? Or is there a better approach?
thx!
e
ASKER
By the way, just a little tidbit. If you ever want to use a macro to remove the button, I found that the find-and-replace on "^g" (graphic) works (as long as you don't have a ton of graphics and/or you set a range for the find and replace). I don't know how to actually code to remove the button (I could never figure out how to "goto" the button object, and once you're in design mode, the macro halts)... but I thought this little find-and-replace technique was a nifty little workaround.
e
e
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Graham: I've been away w/ mono (!! glad computers don't catch our viruses, aren't you!)... and I missed this post in my emails. I am anxious to try it!! I'll let you know how it goes... I'm wrestling with templates now...
e
e
ASKER
Wow! I appreciate all the help. I gave points to R 'cause his solution works.
I gave Jo 200 'cause her solution works and it's the one I used (less typing, I think? Looks more obvious to someone maintaining the code what is happening??)
These two actually answered the question I had
And 200 to Graham 'cause he answered the "bonus" question of multiple buttons.
Thanks everyone for helping me with my multi-part problem!
e
I gave Jo 200 'cause her solution works and it's the one I used (less typing, I think? Looks more obvious to someone maintaining the code what is happening??)
These two actually answered the question I had
And 200 to Graham 'cause he answered the "bonus" question of multiple buttons.
Thanks everyone for helping me with my multi-part problem!
e
Thanks epuglise! Glad it's working for you.
Set ILS = ActiveDocument.Bookmarks("