简体   繁体   中英

VBA macro to embed OLEobject into an Outlook Appointment series

We use Outlook calendar to keep track of room bookings at our local community centre. For each calendar appointment/room booking we insert an object from file (an excel spreadsheet). The spreadsheet and file path is the same for each booking. Screenshot

在此处输入图片说明

I'd like to simplify embedding this spreadsheet with a macro. Unfortunately Outlook doesn't allow me to record one, so I'm trying to get to grips with scripting it with VBA. I tried recorded the macro in Word to see if it would copy over to Outlook (it doesn't - object variable not set). The code generated by Word was

Selection.InlineShapes.AddOLEObject ClassType:= _
    "Excel.SheetMacroEnabled.12", FileName:= _
    "K:\OutlookCalendar.xlsm" _
    , LinkToFile:=False, DisplayAsIcon:=False

I think I need to specify DIMs, but I'm getting out of my depth and I've been unable to find a Google solution so far. Could someone point me in the right direction please?

First set a reference to the "Microsoft Word x.0 Object Library" so Outlook can recognize the objects used by Word - Click Tools/References

Now In order to use your recorded macro in Outlook, you need to set a variable to the Selection object Item.

Example

Option Explicit
Public Sub UseWord()
  Dim Inspector  As Outlook.Inspector
  Dim wdDoc As Word.Document
  Dim Selection As Word.Selection

  Set Inspector = Application.ActiveInspector()
  Set wdDoc = Inspector.WordEditor
  Set Selection = wdDoc.Application.Selection

  '-> And insert your recorded macro here

End Sub

IDE Tools / References

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM