Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / January 2005

Tip: Looking for answers? Try searching our database.

What is wrong with this code?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
TT - 28 Jan 2005 15:29 GMT
Hello,

I?m trying to paste two cells (text) from Excel to Word. The code pasted
below does not completely do the trick - please could someone tell me what
is the problem, what must be added?

Problems:
1. If I leave out the ".Collapse wdCollapseEnd", the range A1:D1 gets
replaced by the range A2:A6. Then the range A2:A6 is formatted like I want
it to be (bold).
2. If I dont leave it out, the formatting is not correct, but both ranges
are being copied otherwise correctly. Then the range A2:A6 is not being
formatted at all, but copies the formatting from range A1:D1 (not bold).

Sub TW()

Dim AppWD As Word.Application
Dim DocWD As Word.Document
Dim RangeWD As Word.Range

Set AppWD = CreateObject("Word.Application.10")
AppWD.Visible = True

Set DocWD = AppWD.Documents.Add
With DocWD
Set RangeWD = .Range
Sheets("T").Select
Range("A1:D1").Select
Selection.Copy
With RangeWD
.Font.Name = "Arial"
.Font.Size = 12
.Font.Bold = False
.PasteAndFormat Type:=wdFormatPlainText
.Collapse wdCollapseEnd
Sheets("T").Select
Range("A2:A6").Select
Selection.Copy
With RangeWD
.Font.Name = "Arial"
.Font.Size = 12
.Font.Bold = True
.PasteAndFormat Type:=wdFormatPlainText
.Collapse wdCollapseEnd
End With
End With

End With

End Sub
Helmut Weber - 28 Jan 2005 22:28 GMT
Hi,
be precise, because
>I´m trying to paste two cells (text) from Excel to Word.
you are trying to paste 2 Excel-Ranges, not cells.

Furthermore, if you collapse the range,
it stays collapsed, and you apply your formatting
to a range of length 0.

How about this one, with improvements possible, I think:
Sub TW()

Dim AppWD As Word.Application
Dim DocWD As Word.Document
Dim RangeWD As Word.Range

Set AppWD = CreateObject("Word.Application.10")
AppWD.Visible = True

Set DocWD = AppWD.Documents.Add
With DocWD
  Set RangeWD = .Range
  Sheets("T").Select
  Range("A1:D1").Select
  Selection.Copy
  With RangeWD
     .Font.Name = "Arial"
     .Font.Size = 12
     .Font.Bold = False
     .PasteAndFormat Type:=wdFormatPlainText
     .InsertAfter vbCr
     .Start = .End + 1
     .Collapse wdCollapseEnd
     MsgBox Len(RangeWD.Text) ' for testing
     Sheets("T").Select
     Range("A2:A6").Select
     Selection.Copy
     .End = DocWD.Content.End ' extent the range
     .Font.Bold = True
     .PasteAndFormat Type:=wdFormatPlainText
  End With
End With

End Sub

Greetings from Bavaria, Germany

Helmut Weber, MVP
"red.sys" & chr(64) & "t-online.de"
Word XP, Win 98
http://word.mvps.org/
TT - 29 Jan 2005 18:35 GMT
Thanks a lot Helmut! Seems to work fine.

This is all new stuff to me. Thanks for helping me out here.

TT

> Hi,
> be precise, because
[quoted text clipped - 47 lines]
> Word XP, Win 98
> http://word.mvps.org/
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.