Results 1 to 3 of 3

Thread: Copy Excel to Word

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2001
    Location
    Washington DC
    Posts
    330

    Unhappy Copy Excel to Word

    Please look over the code, something isnt right even though I have compared this to a bunch of posts already. I made comments throughout regarding what is happening where. thank you.

    VB Code:
    1. Sub CreateWord()
    2.  NewBook = ActiveWorkbook.Name 'Obtains the name of the spreadsheet
    3.  
    4. 'This code requires a referece to the Word object model
    5.  Dim Appword As New Word.Application
    6.  Dim wdDoc As Word.Document ' What is this for?
    7.          
    8.  Set Appword = CreateObject("Word.Application") 'There is a new workbook created called object?
    9.  
    10.    Appword.Documents.Add
    11.    
    12.     Windows(NewBook).Activate 'Makes sure I stay in the right place for copying from Excel
    13.     Sheets("CHART").Select
    14.  
    15. For i = 1 To 6
    16.     Windows(NewBook).Activate 'Makes sure I stay in the right place for copying from Excel
    17.     Sheets("CHART").Select
    18.     CName = "Chart " & i 'Selects the  chart to be copied
    19.     Sheets("Chart").ChartObjects(CName).Activate
    20.     Sheets("Chart").ChartObjects(CName).Copy
    21.    
    22.     MsgBox ("We are on Chart: " & CName)
    23.        
    24.     MsgBox (Appword.Name) 'This only says Microsoft Word
    25.     Appword.Selection.PasteAndFormat wdChartPicture 'Errors and states "command is not available"
    26.    
    27. Next i
    28.  
    29. Appword.Visible = True
    30.  
    31. 'Save New File as : Need to Change File Location information.  This is where the newly created file is saved to.
    32. '
    33.  
    34.        ChangeFileOpenDirectory _
    35.         Drive & Path
    36.         Appword.ActiveDocument.SaveAs FileName:=FileName & ext, FileFormat:= _
    37.         wdFormatDocument, LockComments:=False, Password:="", AddToRecentFiles:= _
    38.         True, WritePassword:="", ReadOnlyRecommended:=False, EmbedTrueTypeFonts:= _
    39.         False, SaveNativePictureFormat:=False, SaveFormsData:=False, _
    40.         SaveAsAOCELetter:=False
    41.        
    42.  
    43.    Appword.Documents.Save
    44.    Appword.Documents.Close
    45. '~~~~~~~~~~~~~~~~~~~~~~~~
    46.     Windows(NewBook).Activate
    47.     Sheets("CHART").Select
    48.  
    49. Set Appword = Nothing
    50. End Sub
    Swoozie
    Somedays you just should not get out of bed.

  2. #2
    Don't Panic! Ecniv's Avatar
    Join Date
    Nov 2000
    Location
    Amsterdam...
    Posts
    5,343

    Re: Copy Excel to Word

    Quote Originally Posted by swoozie
    Please look over the code, something isnt right even though I have compared this to a bunch of posts already. I made comments throughout regarding what is happening where. thank you.

    VB Code:
    1. Sub CreateWord()
    2.  NewBook = ActiveWorkbook.Name 'Obtains the name of the spreadsheet
    3.  
    4. 'This code requires a referece to the Word object model
    5.  Dim Appword As New Word.Application
    6.  Dim wdDoc As Word.Document ' What is this for?
    7. '---- when you edit a word doc
    8. '---- the above is that document
    9.  
    10. '---- I don't think you need this as you have created a word app
    11. '---- (new) already above        
    12.  Set Appword = CreateObject("Word.Application")
    13. 'There is a new workbook created called object?
    14.  
    15. '---- adds a new doc
    16.    Appword.Documents.Add
    17.    
    18.  
    19.     Windows(NewBook).Activate 'Makes sure I stay in the right place for copying from Excel
    20.     Sheets("CHART").Select
    21.  
    22. For i = 1 To 6
    23.     Windows(NewBook).Activate 'Makes sure I stay in the right place for copying from Excel
    24.     Sheets("CHART").Select
    25.     CName = "Chart " & i 'Selects the  chart to be copied
    26.     Sheets("Chart").ChartObjects(CName).Activate
    27.     Sheets("Chart").ChartObjects(CName).Copy
    28.    
    29.     MsgBox ("We are on Chart: " & CName)
    30.        
    31. 'This only says Microsoft Word
    32. '---- because you haven't saved the word doc
    33.     MsgBox (Appword.Name)
    34.     Appword.Selection.PasteAndFormat wdChartPicture
    35. 'Errors and states "command is not available"
    36.    
    37. Next i
    38.  
    39. Appword.Visible = True
    40.  
    41. 'Save New File as : Need to Change File Location information.  This is where the newly created file is saved to.
    42. '
    43.  
    44.        ChangeFileOpenDirectory _
    45.         Drive & Path
    46.         Appword.ActiveDocument.SaveAs FileName:=FileName & ext, FileFormat:= _
    47.         wdFormatDocument, LockComments:=False, Password:="", AddToRecentFiles:= _
    48.         True, WritePassword:="", ReadOnlyRecommended:=False, EmbedTrueTypeFonts:= _
    49.         False, SaveNativePictureFormat:=False, SaveFormsData:=False, _
    50.         SaveAsAOCELetter:=False
    51.        
    52.  
    53.    Appword.Documents.Save
    54.    Appword.Documents.Close
    55. '~~~~~~~~~~~~~~~~~~~~~~~~
    56.     Windows(NewBook).Activate
    57.     Sheets("CHART").Select
    58.  
    59. Set Appword = Nothing
    60. End Sub
    Are you sure that the chart (dot) copy is putting the chart into the clipboard?
    .PasteAndFormat ? is that the right method?
    Did you record all your movements to have an idea of what excel and word want?

    BOFH Now, BOFH Past, Information on duplicates

    Feeling like a fly on the inside of a closed window (Thunk!)
    If I post a lot, it is because I am bored at work! ;D Or stuck...
    * Anything I post can be only my opinion. Advice etc is up to you to persue...

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2001
    Location
    Washington DC
    Posts
    330

    Re: Copy Excel to Word

    1) Yes I recorded a macro first to see what the macro showed. Which actually was virtually nothing.

    2) From the macro I found the .pasteformat method. Since I want the chart to be a pix and not an embedded object

    3) Yes it is copying to the clip board.

    On more than one occasion it did manage to copy chart 1 to a word document however it locked up and I had to kill the macro and find the file. I have 6 charts to paste into word. It is a 3 page document. The top is a summary of the charts, then 2 charts displayed below.

    Any suggestions?
    Swoozie
    Somedays you just should not get out of bed.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width