martin0852
10-16-2012, 07:24 PM
Hi guys,
I have the following macro which is to be used on a protected word table to email the table, when I added a macro to the taskbar (up by save, undo, redo) and attached the macro to that button it worked fine but I need something that will be within the document so that when I distribute the file the macros can be used.
Here is the code:
Private Sub CommandButton2_Click()
If ActiveDocument.ProtectionType = wdNoProtection Then
ActiveDocument.Protect Password:="0852", NoReset:=True, Type:= _
wdAllowOnlyFormFields, UseIRM:=False, EnforceStyleLock:=False
End If
If ActiveDocument.ProtectionType = wdNoProtection Then
ActiveDocument.Protect Password:="0852", NoReset:=True, Type:= _
wdAllowOnlyFormFields, UseIRM:=False, EnforceStyleLock:=False
End If
ActiveDocument.Unprotect Password:="0852"
Dim oAp As Outlook.Application
Dim oItem As Outlook.MailItem
Dim oInsp As Outlook.Inspector
Dim wdEditor
Dim strBody As String
Dim Z As Long
'~~> Get/Create an instance of Outlook
Set oAp = GetObject(, "Outlook.Application")
If Err <> 0 Then
Set oAp = CreateObject("Outlook.Application")
End If
'~~> Create new item
Set oItem = oAp.CreateItem(olMailItem)
'~~> Copy the table
ActiveDocument.Tables(1).Range.Copy
Set oInsp = oItem.GetInspector
Set wdEditor = oInsp.WordEditor
With oItem
.To = "martin.oleary@asml.com"
.CC = ""
.BCC = ""
.Subject = StrSubject & " Passdown " & Format(Now, "dd-mm-yy")
.HTMLBody = rng
.Attachments.Add ActiveDocument.FullName
End With
Z = wdEditor.Characters.Count
'~~> Paste the table in the email body
wdEditor.Characters(Z).PasteAndFormat (wdFormatOriginalFormatting)
'~~> Display the message. use .Send to actually Send the message
oItem.Display
'~~> Flushing the Toilet
Set oItem = Nothing
Set oAp = Nothing
Set oInsp = Nothing
Set wdEditor = Nothing
If ActiveDocument.ProtectionType = wdNoProtection Then
ActiveDocument.Protect Password:="0852", NoReset:=True, Type:= _
wdAllowOnlyFormFields, UseIRM:=False, EnforceStyleLock:=False
End If
End Sub
I have that code in the CommandButton_Click() sub of the "This Document" but when I click the CommandButton I get the error:
Run-time error '91':
Object Variable or With block Variable not set
And it highlights the
Z = wdEditor.Characters.Count
line of code.
Any help greatly appreciated.
TIA
M
I have the following macro which is to be used on a protected word table to email the table, when I added a macro to the taskbar (up by save, undo, redo) and attached the macro to that button it worked fine but I need something that will be within the document so that when I distribute the file the macros can be used.
Here is the code:
Private Sub CommandButton2_Click()
If ActiveDocument.ProtectionType = wdNoProtection Then
ActiveDocument.Protect Password:="0852", NoReset:=True, Type:= _
wdAllowOnlyFormFields, UseIRM:=False, EnforceStyleLock:=False
End If
If ActiveDocument.ProtectionType = wdNoProtection Then
ActiveDocument.Protect Password:="0852", NoReset:=True, Type:= _
wdAllowOnlyFormFields, UseIRM:=False, EnforceStyleLock:=False
End If
ActiveDocument.Unprotect Password:="0852"
Dim oAp As Outlook.Application
Dim oItem As Outlook.MailItem
Dim oInsp As Outlook.Inspector
Dim wdEditor
Dim strBody As String
Dim Z As Long
'~~> Get/Create an instance of Outlook
Set oAp = GetObject(, "Outlook.Application")
If Err <> 0 Then
Set oAp = CreateObject("Outlook.Application")
End If
'~~> Create new item
Set oItem = oAp.CreateItem(olMailItem)
'~~> Copy the table
ActiveDocument.Tables(1).Range.Copy
Set oInsp = oItem.GetInspector
Set wdEditor = oInsp.WordEditor
With oItem
.To = "martin.oleary@asml.com"
.CC = ""
.BCC = ""
.Subject = StrSubject & " Passdown " & Format(Now, "dd-mm-yy")
.HTMLBody = rng
.Attachments.Add ActiveDocument.FullName
End With
Z = wdEditor.Characters.Count
'~~> Paste the table in the email body
wdEditor.Characters(Z).PasteAndFormat (wdFormatOriginalFormatting)
'~~> Display the message. use .Send to actually Send the message
oItem.Display
'~~> Flushing the Toilet
Set oItem = Nothing
Set oAp = Nothing
Set oInsp = Nothing
Set wdEditor = Nothing
If ActiveDocument.ProtectionType = wdNoProtection Then
ActiveDocument.Protect Password:="0852", NoReset:=True, Type:= _
wdAllowOnlyFormFields, UseIRM:=False, EnforceStyleLock:=False
End If
End Sub
I have that code in the CommandButton_Click() sub of the "This Document" but when I click the CommandButton I get the error:
Run-time error '91':
Object Variable or With block Variable not set
And it highlights the
Z = wdEditor.Characters.Count
line of code.
Any help greatly appreciated.
TIA
M