Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Saving WordDoc from Excel

Status
Not open for further replies.

pookie62

Technical User
Oct 22, 2004
139
GB
Hi all,
I found this code, but I can't save the WordDoc properly as I want.
Anyone knows how to do this ?
Code:
' 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" & (Date) & ".doc"
    Set WordApp = CreateObject("Word.Application")
    With WordApp
        .Visible = True
        .WindowState = wdWindowStateMaximize
        .Documents.Add
        Set WordDoc = .ActiveDocument
    End With
'next bold part isn't working, it's saving the Excel file instead of the WordDoc
    [b]With WordDoc
        SaveAs Filename
    End With[/b]
    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
    Range("A1:B250").Copy
    With WordApp.Selection
        .Paste
        .TypeParagraph
    End With
    
    
End Sub
 
Hi pookie62
You need a stop in this:
[tt] With WordDoc
[red].[/red]SaveAs Filename
End With[/tt]

Also:
[tt]Filename = "Bestel" & (Date) & ".doc"[/tt]
Is likely to give you an invalid file name. You could try:
[tt]Filename = "Bestel" & Format(Date, "yymmdd") & ".doc"[/tt]

By the way, this is an Access forum, you are likely to get better answers in:
VBA Visual Basic for Applications (Microsoft) Forum
forum707 :)


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top