Hi all,
Is there a sample for this:
I have a sheet with our number of products in stock.
When the number gets beneath a certain value, I need to order those products.
I would like to export to this to Word creating a list of the products and howmuch I need to order to ensure I have enough of them.
I'll post the code I have so far (the SetPrinted isn't working properly..) hopefully that shines some light..
' Automation of Microsoft Word from Excel
' NOTE: There must be a reference to the current Word
' library from TOOLS | REFERENCES within the VBE
' *************************************************************
'Proc12 uses CreateObject to get the Word 97 Automation object.
'Proc12 then proceeds to create a report in a Word document,
'pasting Excel worksheet data and an Excel chart into the document.
Sub ExcelRoutines_Proc12_CreateWordReport()
'Because this module relies on late binding, it is
'necessary to declare constants to be used with PowerPoint.
Const wdWindowStateMaximize As Integer = 1
Const wdNormalView As Integer = 1
Const wdAlignParagraphCenter As Integer = 1
Const wdAnimationShimmer As Integer = 1
Const wdPasteMetafilePicture As Integer = 3
Const wdInLine As Integer = 0
Const wdPageFitFullPage As Integer = 1
Const wdGoToAbsolute As Integer = 1
Const wdGoToLine As Integer = 3
Dim WordDoc As Object
Dim Filename As String
Filename = "Bestel-" & Format(Date, "dd-mm-yy") & ".doc"
Set WordApp = CreateObject("Word.Application")
With WordApp
.Visible = True
.WindowState = wdWindowStateMaximize
.Documents.Add
Set WordDoc = .ActiveDocument
End With
WordDoc.ActiveWindow.View = wdNormalView
With WordApp.Selection
.InsertAfter "Hansie's VBA Code"
.InsertParagraphAfter
.InsertAfter "Automatisch Word rapport vanuit Excel!"
.InsertParagraphAfter
.InsertAfter "Je kan hier natuurlijk van alles zetten " _
& ":"
.InsertParagraphAfter
.MoveRight
End With
With WordDoc.Paragraphs(1).Range
.ParagraphFormat.Alignment = wdAlignParagraphCenter
With .Font
.Name = "Arial"
.Size = 20
.Bold = True
.Animation = wdAnimationShimmer
End With
End With
With WordDoc.Paragraphs(2).Range
With .ParagraphFormat
.SpaceAfter = 12
.Alignment = wdAlignParagraphCenter
End With
With .Font
.Name = "Arial"
.Size = 14
End With
End With
WordDoc.Paragraphs(3).Range.ParagraphFormat.SpaceAfter = 30
Sheets("Bestel").Range("A1:B25").Copy
With WordApp.Selection
.Paste
.TypeParagraph
End With
With WordDoc
.SaveAs Filename
If MsgBox("Bestellijst gemaakt!" _
& vbCrLf & "Bestand is opgeslagen," _
& vbCrLf & vbCrLf & "Wilt u het meteen printen?", _
vbYesNo + vbDefaultButton2 + 32, "Bestellijst") = vbYes Then _
.PrintOut
.Close
End With
Sheets("Voorraad").Range("I3").Select
Set WordDoc = Nothing
WordApp.Quit
Set WordApp = Nothing
End Sub
Sub SetPrinted()
'print unmarked items
Dim wsV As Worksheet
Dim wsB As Worksheet
Set wsV = Worksheets("Voorraad")
Set wsB = Worksheets("Bestel")
Dim rStart As Long
Dim rEnd As Long
Dim r As Long
rStart = wsV.Cells(Rows.Count, 1).End(xlUp).Row + 1
rEnd = wsV.Cells(Rows.Count, 2).End(xlUp).Row
r = rEnd - rStart + 1
For r = 1 To r
'wsB.Range("A1").Value = wsV.Range("B" & rStart + r - 1)
wsB.PrintPreview
If wsB.Range("B" & rStart + r - 1) <> "" Then _
wsV.Range("A" & rStart + r - 1).Value = "x"
Next r
End Sub
Is there a sample for this:
I have a sheet with our number of products in stock.
When the number gets beneath a certain value, I need to order those products.
I would like to export to this to Word creating a list of the products and howmuch I need to order to ensure I have enough of them.
I'll post the code I have so far (the SetPrinted isn't working properly..) hopefully that shines some light..
' Automation of Microsoft Word from Excel
' NOTE: There must be a reference to the current Word
' library from TOOLS | REFERENCES within the VBE
' *************************************************************
'Proc12 uses CreateObject to get the Word 97 Automation object.
'Proc12 then proceeds to create a report in a Word document,
'pasting Excel worksheet data and an Excel chart into the document.
Sub ExcelRoutines_Proc12_CreateWordReport()
'Because this module relies on late binding, it is
'necessary to declare constants to be used with PowerPoint.
Const wdWindowStateMaximize As Integer = 1
Const wdNormalView As Integer = 1
Const wdAlignParagraphCenter As Integer = 1
Const wdAnimationShimmer As Integer = 1
Const wdPasteMetafilePicture As Integer = 3
Const wdInLine As Integer = 0
Const wdPageFitFullPage As Integer = 1
Const wdGoToAbsolute As Integer = 1
Const wdGoToLine As Integer = 3
Dim WordDoc As Object
Dim Filename As String
Filename = "Bestel-" & Format(Date, "dd-mm-yy") & ".doc"
Set WordApp = CreateObject("Word.Application")
With WordApp
.Visible = True
.WindowState = wdWindowStateMaximize
.Documents.Add
Set WordDoc = .ActiveDocument
End With
WordDoc.ActiveWindow.View = wdNormalView
With WordApp.Selection
.InsertAfter "Hansie's VBA Code"
.InsertParagraphAfter
.InsertAfter "Automatisch Word rapport vanuit Excel!"
.InsertParagraphAfter
.InsertAfter "Je kan hier natuurlijk van alles zetten " _
& ":"
.InsertParagraphAfter
.MoveRight
End With
With WordDoc.Paragraphs(1).Range
.ParagraphFormat.Alignment = wdAlignParagraphCenter
With .Font
.Name = "Arial"
.Size = 20
.Bold = True
.Animation = wdAnimationShimmer
End With
End With
With WordDoc.Paragraphs(2).Range
With .ParagraphFormat
.SpaceAfter = 12
.Alignment = wdAlignParagraphCenter
End With
With .Font
.Name = "Arial"
.Size = 14
End With
End With
WordDoc.Paragraphs(3).Range.ParagraphFormat.SpaceAfter = 30
Sheets("Bestel").Range("A1:B25").Copy
With WordApp.Selection
.Paste
.TypeParagraph
End With
With WordDoc
.SaveAs Filename
If MsgBox("Bestellijst gemaakt!" _
& vbCrLf & "Bestand is opgeslagen," _
& vbCrLf & vbCrLf & "Wilt u het meteen printen?", _
vbYesNo + vbDefaultButton2 + 32, "Bestellijst") = vbYes Then _
.PrintOut
.Close
End With
Sheets("Voorraad").Range("I3").Select
Set WordDoc = Nothing
WordApp.Quit
Set WordApp = Nothing
End Sub
Sub SetPrinted()
'print unmarked items
Dim wsV As Worksheet
Dim wsB As Worksheet
Set wsV = Worksheets("Voorraad")
Set wsB = Worksheets("Bestel")
Dim rStart As Long
Dim rEnd As Long
Dim r As Long
rStart = wsV.Cells(Rows.Count, 1).End(xlUp).Row + 1
rEnd = wsV.Cells(Rows.Count, 2).End(xlUp).Row
r = rEnd - rStart + 1
For r = 1 To r
'wsB.Range("A1").Value = wsV.Range("B" & rStart + r - 1)
wsB.PrintPreview
If wsB.Range("B" & rStart + r - 1) <> "" Then _
wsV.Range("A" & rStart + r - 1).Value = "x"
Next r
End Sub