Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 58
Default copying data in a sheet to a new Word document problem

I have been trying to make the below sub work. I keep getting errors of :
Compile error:

can't find project or library

I did go to tools and reference and load Microsoft word 11.0 object library
sheet 1 has 3 columns Regions, SalesNum, SalesAmt




Sub MakeMemos()
'Creates memos in word using Automation
Dim WordApp As Object
Set WordApp = CreateObject("Word.Application")
Dim Data As Range, message As String
Dim Records As Integer, i As Integer
Dim Region As String, SalesAmt As String, SalesNum As String

'Start Word and create an object (late binding)
Set WordApp = CreateObject("Word.Application")

'Information from worksheet
Set Data = Sheet("Sheet1").Range("A1")
message = Sheet("Sheet1").Range("Message")

'Cycle through all records in Sheet1
Records = Application.CountA(Sheets("Sheet1").Range("A:A"))
For i = 1 To Records
' Update status bar progress message
Application.SatusBar = "Processing Record " & i

' Assign current data to variables
Region = Data.Cells(i, 1).Value
SalesNum = Data.Cells(i, 2).Value
SalesAmt = Format(Data.Cells(i, 3).Value, "#,000")
'Determine the filename
SaveAsName = Application.DefaultFilePath & _
"\" & Region & ".doc"

'Send commands to Word
With WordApp
.Documents.Add
With .Selection
.Font.Size = 14
.Font.Bold = True
.ParagraphFormat.Alignment = 1
.TypeText Text:="MEMORANDUM"
.TypeParagraph
.TypeParagraph
.FontSize = 12
.ParagraphFormat.Alignment = 0
.Font.Bold = False
.TypeText Text:="Date:" & vbTab & _
.Format("mmmm d, yyyy")
.TypeParagraph
.TypeText Text:="To:" & vbTab & Region & _
"Manager"
.TypeParagraph
.TypeText Text:="From:" & vbTab & _
Application.UserName
.TypeParagraph
.TypeParagraph
.TypeText message
.TypeParagraph
.TypeParagraph
.TypeText Text:="Units Sold:" & vbTab & _
"SalesNum"
.TypeParagraph
.TypeText Text:="Amount:" & vbTab & _
Format(SalesAmt, "$#,##0")
End With
.ActiveDocument.SaveAs FileName:=SaveAsName
End With
Next i
'Kill the object
WordApp = Nothing

'Reset status bar
Application.StatusBar = " "
MsgBox Records & "memos were created and stored in " & _
Application.DefaultFilePath
End Sub



  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 789
Default copying data in a sheet to a new Word document problem

Hi
You might try being a bit more precise with the code

Dim WordApp as Word.Application
Dim Worddoc as Word.Document

Further on in code try

Set Worddoc = WordApp.document.add
With Worddoc
.Font.Size = 14
.Font.Bold = True
etc
.SaveAs FileName:=SaveAsName
end with

and release objects in right order
set Worddoc = Nothing
set WordApp = Nothing

connecting to Word seems to throw up errors unless you take care. It
can throw up errors even if you do take care (try seraching this
Newsgroup)
regards
Paul

On Mar 10, 3:51*am, gbpg wrote:
I have been trying to make the below sub work. I keep getting errors of :
Compile error:

can't find project or library

I did go to tools and reference and load Microsoft word 11.0 object library
sheet 1 has 3 columns Regions, SalesNum, SalesAmt

Sub MakeMemos()
'Creates memos in word using Automation
Dim WordApp As Object
Set WordApp = CreateObject("Word.Application")
Dim Data As Range, message As String
Dim Records As Integer, i As Integer
Dim Region As String, SalesAmt As String, SalesNum As String

'Start Word and create an object (late binding)
Set WordApp = CreateObject("Word.Application")

'Information from worksheet
Set Data = Sheet("Sheet1").Range("A1")
message = Sheet("Sheet1").Range("Message")

'Cycle through all records in Sheet1
Records = Application.CountA(Sheets("Sheet1").Range("A:A"))
For i = 1 To Records
' * Update status bar progress message
* * Application.SatusBar = "Processing Record " & i

' * Assign current data to variables
* * Region = Data.Cells(i, 1).Value
* * SalesNum = Data.Cells(i, 2).Value
* * SalesAmt = Format(Data.Cells(i, 3).Value, "#,000")
'Determine the filename
*SaveAsName = Application.DefaultFilePath & _
* * "\" & Region & ".doc"

'Send commands to Word
With WordApp
* * .Documents.Add
With .Selection
* * * * * .Font.Size = 14
* * * * * .Font.Bold = True
* * * * * .ParagraphFormat.Alignment = 1
* * * * * .TypeText Text:="MEMORANDUM"
* * * * * .TypeParagraph
* * * * * .TypeParagraph
* * * * * .FontSize = 12
* * * * * .ParagraphFormat.Alignment = 0
* * * * * .Font.Bold = False
* * * * * .TypeText Text:="Date:" & vbTab & _
* * * * * .Format("mmmm d, yyyy")
* * * * *.TypeParagraph
* * * * *.TypeText Text:="To:" & vbTab & Region & _
* * * * * "Manager"
* * * * *.TypeParagraph
* * * * * * * * *.TypeText Text:="From:" & vbTab & _
* * * * * * Application.UserName
* * * * *.TypeParagraph
* * * * * * * * *.TypeParagraph
* * * * *.TypeText message
* * * * *.TypeParagraph
* * * * *.TypeParagraph
* * * * *.TypeText Text:="Units Sold:" & vbTab & _
* * * * *"SalesNum"
* * * * *.TypeParagraph
* * * * *.TypeText Text:="Amount:" & vbTab & _
* * * * * *Format(SalesAmt, "$#,##0")
End With
* * .ActiveDocument.SaveAs FileName:=SaveAsName
End With
Next i
'Kill the object
WordApp = Nothing

'Reset status bar
Application.StatusBar = " "
MsgBox Records & "memos were created and stored in " & _
Application.DefaultFilePath
End Sub


Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Copying Cell data and pasting into word document? Simon Lloyd[_759_] Excel Programming 4 June 7th 06 10:41 AM
Copying an Excel chart into a Word document Carolyn Excel Discussion (Misc queries) 7 July 11th 05 07:29 PM
Is there an easy way to take data from a spread sheet and have it entered into a Word document? Marc New Users to Excel 2 April 8th 05 10:53 PM
Getting specific data from a word document into an excel sheet smintey Excel Worksheet Functions 3 December 7th 04 05:17 PM
Copying an Excel spreadsheet to a Word document KG Excel Discussion (Misc queries) 1 November 28th 04 07:38 PM


All times are GMT +1. The time now is 06:42 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"