我有一个MS Word模板,用于打印纸张,我在工作会议中记笔记。我将主题,与会者列表,时间和地点逐个复制并粘贴到我的Word文档中。最终结果是一个页面顶部有一个标题,其中包含有关会议的所有基本信息。
我想自动执行此程序。很自然地,我有很多问题需要解决:
如果可以的话,请给我一些代码片段。示例帮助方式而不是链接。
答案 0 :(得分:1)
我认为最好的起点是你打开相关的AppointmentItem。这里有一些严重未经测试的半伪代码可以帮助您入门。首先设置对Word对象库的引用(工具 - 引用)。
Sub MakeMeetingTemplate()
Dim wdApp As Word.Application
Dim wdDoc As Word.Document
Dim wdRng As Word.Range
Dim Appt As AppointmentItem
If TypeName(ActiveInspector.CurrentItem) = "AppointmentItem" Then
Set Appt = ActiveInspector.CurrentItem
Set wdApp = New Word.Application
wsApp.Visible = True
Set wdDoc = wdApp.Documents.Add("C:\MyTemplate.doc")
FillBookMark wdDoc.Bookmarks("MeetingName"), Appt.Subject
FillBookMark wdDoc.Bookmarks("Attendees"), GetAttendees(Appt)
FillBookMark wdDoc.Bookmarks("When"), Appt.Start
FillBookMark wdDoc.Bookmarks("Location"), Appt.Location
End If
End Sub
Sub FillBookMark(ByRef bMark As Word.Bookmark, sText As String)
Dim wdRng As Word.Range
Set wdRng = bMark.Range
wdRng.Text = sText
End Sub
Function GetAttendees(Appt As AppointmentItem) As String
Dim Rcpt As Recipient
Dim sReturn As String
For Each Rcpt In Appt.Recipients
sReturn = sReturn & Rcpt.Name & " "
Next Rcpt
GetAttendees = sReturn
End Function
这是它的作用:确保活动项是AppointmentItem。打开Word模板。使用AppointmentItem中的数据在Word文档中填写预定义的书签。完成后,您将拥有一个Word文档,其中包含可以打印,编辑或其他任何内容的预填充信息。有关Word中书签的详细信息,请参阅
http://www.dailydoseofexcel.com/archives/2004/08/13/automating-word/
答案 1 :(得分:0)
供参考,这是我使用的最终代码:
Sub PrintMeetingheader()
'Declaration
Dim myItems, myItem As Object
Dim myOrt As String
Dim myOlApp As New Outlook.Application
Dim myOlExp As Outlook.Explorer
Dim myOlSel As Outlook.Selection
Dim sDate As String
Dim sTopic As String
Dim sLocation As String
Dim sAttendees As String
Dim wdApp As Object
Dim wdDoc As Object
Dim wdRng As Object
'work on selected items
Set myOlExp = myOlApp.ActiveExplorer
Set myOlSel = myOlExp.Selection
'for all items do...
For Each myItem In myOlSel
'Check if its the right type
If TypeName(myItem) = "AppointmentItem" Then
'Do all sorts of stuff with the item
sDate = Format(myItem.Start, "mm/dd")
sDate = sDate + " ("
sDate = sDate + Format(myItem.Start, "Medium Time")
sDate = sDate + "-"
sDate = sDate + Format(myItem.End, "Medium Time")
sDate = sDate + ")"
sTopic = myItem.Subject
sLocation = myItem.Location
sAttendees = myItem.RequiredAttendees
Set wdApp = CreateObject("Word.Application")
Set wdDoc = wdApp.Documents.Open(FileName:="C:\Meeting Notes Temp.doc", ReadOnly:=True)
FillBookmark wdDoc, sDate, "mDate"
FillBookmark wdDoc, sTopic, "mTopic"
FillBookmark wdDoc, sLocation, "mLocation"
FillBookmark wdDoc, sAttendees, "mAttendees"
wdDoc.PrintOut True
MsgBox ("The document was sent to the default printer. Press OK to close it.")
wdApp.Quit wdDoNotSaveChanges
End If
Next
'free variables
Set myItems = Nothing
Set myItem = Nothing
Set myOlApp = Nothing
Set myOlExp = Nothing
Set myOlSel = Nothing
End Sub