Vous êtes sur la page 1sur 6

Chapter 16

Page 361
361 Early Binding
Sub WordEarlyBinding()
Dim wdApp As Word.Application
Dim wdDoc As Document
Set wdApp =New Word.Application
Set wdDoc =wdApp.Documents.Open(ThisWorkbook.Path &_
\Chapter 16 -Automating Word.doc )
wdApp.Visible =True
Set wdApp =Nothing
Set wdDoc =Nothing
End Sub
Page 362
Sub WordLateBinding()
Dim wdApp As Object,wdDoc As Object
Set wdApp =CreateObject(Word.Application )
Set wdDoc =wdApp.Documents.Open(ThisWorkbook.Path &\Chapter 16 -Automating
.Word.doc )
wdApp.Visible =True
Set wdApp =Nothing
Set wdDoc =Nothing
End Sub
Page 363
Sub UseGetObject()
Dim wdDoc As Object
Set wdDoc =GetObject(ThisWorkbook.Path &\Chapter 16 -Automating Word.doc )
wdDoc.Application.Visible =True
Set wdDoc =Nothing
End Sub
Page 364
Sub IsWordOpen()
Dim wdApp As Object
ActiveChart.ChartArea.Copy
On Error Resume Next
Set wdApp =GetObject(,Word.Application )
If wdApp Is Nothing Then
Set wdApp =GetObject(,Word.Application )
With wdApp
.Documents.Add
.Visible =True
End With
End If
On Error GoTo 0
With wdApp.Selection
.EndKey Unit:=wdStory
.TypeParagraph
.PasteSpecial link:=False,DataType:=wdPasteOLEObject,_
Placement:=wdInLine,DisplayAsIcon:=False
End With
Set wdApp =Nothing
End Sub

Page 365
Documents.Add Template:=Normal ,NewTemplate:=False,DocumentType:=0
"...
CreateObject ):
Sub NewDocument()
Dim wdApp As Word.Application
Set wdApp =GetObject(,Word.Application )
wdApp.Documents.Add
Set wdApp =Nothing
End Sub
Page 366
wdApp.Documents.Open _
Filename:=C:\Excel VBA 2003 by Jelen &Syrstad \Chapter 17 -Arrays.Doc ,_
ReadOnly:=True,AddtoRecentFiles:=False
"...
wdApp.Documents.Save
"...
wdApp.ActiveDocument.SaveAs C:\Excel VBA 2003 by Jelen &Syrstad \MemoTest.doc
"...
wdApp.Documents.Close SaveChanges:=wdDoNotSaveChanges
"...
wdApp.ActiveDocument.Close
"...
wdApp.Documents(Chapter 17 -Arrays.Doc ).Close
"...
wdApp.ActiveDocument.PrintOut
"...
wdApp.ActiveDocument.PrintOut Range:=wdPrintRangeOfPages,Pages:=2
Page 367
wdApp.Selection.HomeKey Unit:=wdStory,Extend:=wdMove
"...
.wdApp.Selection.EndKey Unit:=wdStory,Extend:=wdExtend
"...
Sub InsertText()
Dim wdApp As Word.Application
Dim wdDoc As Document
Dim wdSln As Selection
Set wdApp =GetObject(,Word.Application )
Set wdDoc =wdApp.ActiveDocument
Set wdSln =wdApp.Selection
wdDoc.Application.Options.Overtype =False
With wdSln
If .Type =wdSelectionIP Then
.TypeText (Inserting at insertion point.)
ElseIf .Type =wdSelectionNormal Then
If wdApp.Options.ReplaceSelection Then
.Collapse Direction:=wdCollapseStart
End If
.TypeText (Inserting before a text block.)
End If
End With
Set wdApp =Nothing
Set wdDoc =Nothing
End Sub

Page 368
Range(StartPosition,EndPosition)
Sub RangeText()
Dim wdApp As Word.Application
Dim wdDoc As Document
Dim wdRng As Word.Range
Set wdApp =GetObject(,Word.Application )
Set wdDoc =wdApp.ActiveDocument
Set wdRng =wdDoc.Range(0,22)
wdRng.Select
Set wdApp =Nothing
Set wdDoc =Nothing
Set wdRng =Nothing
End Sub
Page 369
Sub SelectSentence()
Dim wdApp As Word.Application
Dim wdRng As Word.Range
Set wdApp =GetObject(,Word.Application )
With wdApp.ActiveDocument
If .Paragraphs.Count >=3 Then
Set wdRng =.Paragraphs(3).Range
wdRng.Copy
End If
End With
This line pastes the copied text into a text box
Worksheets(Sheet2 ).PasteSpecial
These two lines paste the copied text in cell A1
Note that the range must be selected and then we can paste the text
Worksheets(Sheet2 ).Range(A1 ).Activate
ActiveSheet.Paste
Set wdApp =Nothing
Set wdRng =Nothing
End Sub
"...
Page 369-370
Sub ChangeFormat()
Dim wdApp As Word.Application
Dim wdRng As Word.Range
Dim count As Integer
Set wdApp =GetObject(,Word.Application )
With wdApp.ActiveDocument
For count =1 To .Paragraphs.count
Set wdRng =.Paragraphs(count).Range
With wdRng
.Words(1).Font.Bold =True
.Collapse
End With
Next count
End With
Set wdApp =Nothing

Page 370
Sub ChangeStyle()
Dim wdApp As Word.Application
Dim wdRng As Word.Range
Dim count As Integer
Set wdApp =GetObject(,Word.Application )
With wdApp.ActiveDocument
For count =1 To .Paragraphs.count
Set wdRng =.Paragraphs(count).Range
With wdRng
If .Style =NO Then
.Style =HA
.Collapse
End If
End With
Next count
End With
Set wdApp =Nothing
Set wdRng =Nothing
End Sub
Page 372
Sub UseBookmarks()
Dim myArray()
Dim wdBkmk As String
Dim wdApp As Word.Application
Dim wdRng As Word.Range
myArray =Array(To ,CC ,From ,Subject )
Set wdApp =GetObject(,Word.Application )
Set wdRng =wdApp.ActiveDocument.Bookmarks(myArray(0)).Range
wdRng.InsertBefore (Bill Jelen )
Set wdRng =wdApp.ActiveDocument.Bookmarks(myArray(1)).Range
wdRng.InsertBefore (Tracy Syrstad )
Set wdRng =wdApp.ActiveDocument.Bookmarks(myArray(2)).Range
wdRng.InsertBefore (MrExcel )
Set wdRng =wdApp.ActiveDocument.Bookmarks(myArray(3)).Range
wdRng.InsertBefore (Fruit Sales )
Set wdApp =Nothing
Set wdRng =Nothing
End Sub
"...
Page 372-373
Sub CreateMemo()
Dim myArray()
Dim wdBkmk As String
Dim wdApp As Word.Application
Dim wdRng As Word.Range
myArray =Array(To ,CC ,From ,Subject ,Chart )
Set wdApp =GetObject(,Word.Application )
Set wdRng =wdApp.ActiveDocument.Bookmarks(myArray(0)).Range
wdRng.InsertBefore (Bill Jelen )
Set wdRng =wdApp.ActiveDocument.Bookmarks(myArray(1)).Range
wdRng.InsertBefore (Tracy Syrstad )
Set wdRng =wdApp.ActiveDocument.Bookmarks(myArray(2)).Range
wdRng.InsertBefore (MrExcel )

Set wdRng =wdApp.ActiveDocument.Bookmarks(myArray(3)).Range


wdRng.InsertBefore (Fruit &Vegetable Sales )
Set wdRng =wdApp.ActiveDocument.Bookmarks(myArray(4)).Range
ActiveSheet.ChartObjects(Chart 1 ).Copy
wdRng.Select
wdRng.Application.Selection.PasteAndFormat Type:=2
wdApp.Activate
Set wdApp =Nothing
Set wdRng =Nothing
End Sub
Page 324-376
Sub RunReportForEachCustomer()
Dim IRange As Range
Dim ORange As Range
Dim CRange As Range
Dim WBN As Workbook
Dim WSN As Worksheet
Dim WSO As Worksheet
Dim wdApp As Word.Application
Dim wdDoc As Word.Document
Dim wdRng As Word.Range
Application.ScreenUpdating =False
Set WSO =ActiveSheet
Find the size of today s dataset
FinalRow =Cells(65536,1).End(xlUp).Row
NextCol =Cells(1,255).End(xlToLeft).Column +2
First --get a unique list of customers in J
Set up output range..Copy heading from D1 there
Range(D1 ).Copy Destination:=Cells(1,NextCol)
Set ORange =Cells(1,NextCol)
Define the Input Range
Set IRange =Range(A1 ).Resize(FinalRow,NextCol -2)
Do the Advanced Filter to get unique list of customers
IRange.AdvancedFilter Action:=xlFilterCopy,CriteriaRange:=,_
CopyToRange:=ORange,Unique:=True
FinalCust =Cells(65536,NextCol).End(xlUp).Row
Loop through each customer
For Each cell In Cells(2,NextCol).Resize(FinalCust -1,1)
ThisCust =cell.Value
Set up the Criteria Range with one customer
Cells(1,NextCol +2).Value =Range(D1 ).Value
Cells(2,NextCol +2).Value =ThisCust
Set CRange =Cells(1,NextCol +2).Resize(2,1)
Set up output range..We want Date,Quantity,Product,Revenue
These columns are in C,,E,B,and F
Cells(1,NextCol +4).Resize(1,4).Value =_
Array(Cells(1,3),Cells(1,5),_
Cells(1,2),Cells(1,6))
Set ORange =Cells(1,NextCol +4).Resize(1,4)
Do the Advanced Filter to get unique list of customers &&product
IRange.AdvancedFilter Action:=xlFilterCopy,CriteriaRange:=CRange,
.CopyToRange:=ORange
Add Total information
totalrow =WSO.Cells(65536,ORange.Columns(1).Column).End(xlUp).Row +1
WSO.Cells(totalrow,ORange.Columns(1).Column).Value =Total
WSO.Cells(totalrow,ORange.Columns(2).Column).FormulaR1C1 ==SUM(R2C:R [-

.1 ]C)
WSO.Cells(totalrow,ORange.Columns(4).Column).FormulaR1C1 ==SUM(R2C:R [.1 ]C)
Create a new document to hold the output
On Error Resume Next
Set wdDoc =New Word.Document
Set wdApp =GetObject(,Word.Application )
If wdApp Is Nothing Then Set wdApp =GetObject(,Word.Application )
Set wdDoc =wdApp.Documents.Add(Template:=C:\Reports \MrExcel
.SalesTemplate.dot )
wdDoc.Activate
Set up a title on the document
wdDoc.Bookmarks(Client ).Range.InsertBefore (ThisCust)
Copy data from WSO to wdDoc
WSO.Cells(1,NextCol +4).CurrentRegion.Copy
Set wdRng =wdApp.ActiveDocument.Bookmarks(Table ).Range
wdRng.Select
Format the table
With wdDoc.Application.Selection
.Paste
With wdDoc.Application.ActiveDocument.Tables(1)
.Rows.Alignment =wdAlignRowCenter
With .Rows(1)
.HeadingFormat =True
.Select
wdApp.Selection.Font.Bold =True
End With
End With
.HomeKey Unit:=wdStory,Extend:=Move
End With
Save the document with a unique title and then close it
wdDoc.SaveAs C:\Reports \ &&ThisCust &.doc
wdDoc.Close savechanges:=False
WSO.Select
Set wdApp =Nothing
Set wdDoc =Nothing
Set wdRng =Nothing
clear the output range,,etc.
Cells(1,NextCol +2).Resize(1,10).EntireColumn.Clear
Next cell
Application.ScreenUpdating =True
Cells(1,NextCol).EntireColumn.Clear
MsgBox FinalCust -1 & Reports have been created!!
End Sub

Set wdRng =Nothing


End Sub

Vous aimerez peut-être aussi