Word VBA Codemodule providing VBA for a Command Button

R

RC - S

I have used VBA code suggested by the Microsoft website to programatically
add code to a Command Button being added to a document the VBA is creating,
but the code crashed Word every time just after it adds the code to
ThisDocument. Here is the code i am using:

Sub Test

Dim doc As Word.Document
Dim shp As Word.InlineShape
Set doc = Documents.Add

Set shp = doc.Content.InlineShapes.AddOLEControl("Forms.COmmandButton.1")

shp.OLEFormat.Object.Caption = "Save Signature"

Dim sCode As String

sCode = "Private Sub " & shp.OLEFormat.Object.Name & "_Click()" & vbCrLf & _
" Dim tempName As String" & vbCrLf & _
" tempName = Left(ActiveDocument.FullName, InStr(1,
ActiveDocument.FullName, ""."") - 1) " & vbCrLf & _
" ActiveDocument.SaveAs FileName:=tempName,
FileFormat:=wdFormatHTML, _ " & vbCrLf & _
" LockComments:=False, Password:="""",
AddToRecentFiles:=False, WritePassword:="""", _ " & vbCrLf & _
" ReadOnlyRecommended:=False, EmbedTrueTypeFonts:=False, _ " &
vbCrLf & _
" SaveNativePictureFormat:=False, SaveFormsData:=False,
SaveAsAOCELetter:=False" & vbCrLf & _
" msgbox ""Signature saved""" & vbCrLf & _
" ThisDocument.close" & vbCrLf & _
"End Sub"


doc.VBProject.VBComponents("ThisDocument").CodeModule.AddFromString sCode

End Sub

The point at which it fails is when it goes to run ANY code placed after the
doc.VBProject/VBComp.... line, no mater what it is.

If i stop the code without processing the End Sub the new document has the
COmmand Button and it is set with the code, and it runs fine.

Any help would be appreciated.

Thank you.
 
C

Cindy M.

Hi =?Utf-8?B?UkMgLSBT?=,

Hmmm. What if you add a doc.Save line?
I have used VBA code suggested by the Microsoft website to programatically
add code to a Command Button being added to a document the VBA is creating,
but the code crashed Word every time just after it adds the code to
ThisDocument. Here is the code i am using:

Sub Test

Dim doc As Word.Document
Dim shp As Word.InlineShape
Set doc = Documents.Add

Set shp = doc.Content.InlineShapes.AddOLEControl("Forms.COmmandButton.1")

shp.OLEFormat.Object.Caption = "Save Signature"

Dim sCode As String

sCode = "Private Sub " & shp.OLEFormat.Object.Name & "_Click()" & vbCrLf & _
" Dim tempName As String" & vbCrLf & _
" tempName = Left(ActiveDocument.FullName, InStr(1,
ActiveDocument.FullName, ""."") - 1) " & vbCrLf & _
" ActiveDocument.SaveAs FileName:=tempName,
FileFormat:=wdFormatHTML, _ " & vbCrLf & _
" LockComments:=False, Password:="""",
AddToRecentFiles:=False, WritePassword:="""", _ " & vbCrLf & _
" ReadOnlyRecommended:=False, EmbedTrueTypeFonts:=False, _ " &
vbCrLf & _
" SaveNativePictureFormat:=False, SaveFormsData:=False,
SaveAsAOCELetter:=False" & vbCrLf & _
" msgbox ""Signature saved""" & vbCrLf & _
" ThisDocument.close" & vbCrLf & _
"End Sub"


doc.VBProject.VBComponents("ThisDocument").CodeModule.AddFromString sCode

End Sub

The point at which it fails is when it goes to run ANY code placed after the
doc.VBProject/VBComp.... line, no mater what it is.

If i stop the code without processing the End Sub the new document has the
COmmand Button and it is set with the code, and it runs fine.

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 17 2005)
http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any follow question or reply
in the newsgroup and not by e-mail :)
 
R

RC - S

Hi Cidy
This did not make any difference.

If you clip the code to your own project - does it also fail?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top