Exporting Excel data to Word (VBA)

SnowBlind

New Member
Joined
Jul 10, 2002
Messages
1
Hi!

I'm working on a macro that copies data from Excel and then pastes it to Word, after which it saves the document.

Here's the code:

Sub PasteToWord()

Range("A5:A6").Select
Dim WDApp As Word.Application

' Is something selected?

If Not TypeName(Selection) = "Range" Then
MsgBox "Select a Range!", vbExclamation, "No Range Selected"

Else

Set WDApp = CreateObject "Word.Application.9")
WDApp.Visible = True

Selection.copy

WDApp.Documents.Add
WDApp.Selection.Paste
WDApp.ActiveDocument.SaveAs FileName:="C:tempImported.doc"
WDApp.ActiveDocument.Close

Set WDDoc = Nothing
Set WDApp = Nothing
End If
End Sub



Now, I've managed to make it work, but I've got 2 questions:

1. How can I get the macro NOT to copy the cell outlines to the Word document?
2. How do define the empty space left between the words that are pasted to the document?
This message was edited by SnowBlind on 2002-07-11 05:31
 

Excel Facts

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December
I have been trying to do this for a while also, and have formatting problems. I tried "recording" a macro in Word to reformat, but didn't get that to work. I am interested in responses to this as well...
 
Upvote 0
Howdy Snowblind & NancyO

I use the following code with the pastespecial to leave the gridlines behind.

<pre>
Sub WordUp()
Dim WdObj As Object, fname As String
fname = Sheets(2).[a1].Value
Set WdObj = CreateObject("Word.Application")
WdObj.Visible = False
Selection.Copy 'Your Copy Range
WdObj.documents.Add
WdObj.Selection.PasteSpecial Link:=False, _
DataType:=wdPasteText, Placement:= _
wdInLine, DisplayAsIcon:=False
Application.CutCopyMode = False
If fname <> "" Then 'make sure fname is not blank
With WdObj
.ChangeFileOpenDirectory "c:temp" 'save Dir
.ActiveDocument.SaveAs Filename:=fname & ".doc"
End With
Else:
MsgBox ("File not saved, naming range was botched, guess again.")
End If
With WdObj
.ActiveDocument.Close
.Quit
End With
Set WdObj = Nothing
End Sub</pre>

I dim as an object, perhaps a little slower, but avoids the library reference.

Not sure I exactly follow question 2, but this should be formatted very much like your spreadsheet. Also, don't edit in Word, will return it to a gridline format.
 
Upvote 0
I had code as below:

Code:
Dim WdObj As Object, fname As String
fname = "Word"
Set WdObj = CreateObject("Word.Application")
WdObj.Visible = False
Range("A1:I30").Select
Selection.Copy 'Your Copy Range
WdObj.Documents.Add
WdObj.Selection.PasteSpecial Link:=False, _
    DataType:=wdPasteText, Placement:= _
    wdInLine, DisplayAsIcon:=False
Application.CutCopyMode = False
If fname <> "" Then 'make sure fname is not blank
With WdObj
   .ChangeFileOpenDirectory "c:\temp" 'save Dir
    .ActiveDocument.SaveAs Filename:=fname & ".doc"
End With
Else:
MsgBox ("File not saved, naming range was botched, guess again.")
End If
With WdObj
    .ActiveDocument.Close
    .Quit
End With
Set WdObj = Nothing

But it occur error at .ChangeFileOpenDirectory "c:\temp" 'save Dir
..May I know what is the problem?????Anyone can tell me?
 
Upvote 0
How can I do so that when I insert new excel data into this word document,it will be insert into the new page so it will not overlapping the existing page.
 
Upvote 0

Forum statistics

Threads
1,223,760
Messages
6,174,339
Members
452,555
Latest member
colc007

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top