PC Review


Reply
Thread Tools Rate Thread

copying data in a sheet to a new Word document problem

 
 
gbpg
Guest
Posts: n/a
 
      10th Mar 2008
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 With Quote
 
 
 
 
paul.robinson@it-tallaght.ie
Guest
Posts: n/a
 
      10th Mar 2008
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 <g...@discussions.microsoft.com> 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 With Quote
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem copying from a hidden worksheet (data is copied, but alsoadditional sheet info) giancarp@googlemail.com Microsoft Excel Programming 8 31st Jan 2008 06:26 PM
Copying Cell data and pasting into word document? Simon Lloyd Microsoft Excel Programming 4 7th Jun 2006 10:41 AM
Is there an easy way to take data from a spread sheet and have it entered into a Word document? Marc Microsoft Excel New Users 2 8th Apr 2005 10:53 PM
Getting specific data from a word document into an excel sheet =?Utf-8?B?c21pbnRleQ==?= Microsoft Excel Worksheet Functions 3 7th Dec 2004 05:17 PM
Copying Word Document, Including Header/Footer, Into Another Word Document Tom Sears Microsoft Word Document Management 1 19th Aug 2003 10:59 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 11:52 AM.