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.
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.