Results 1 to 9 of 9
  1. #1
    Star Lounger
    Join Date
    Feb 2005
    Posts
    61
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Error during pasting to Word doc (Office 2003)

    Hello All,

    I need help. The attached codes read Excel files from a directory, resize the charts, (supposed to) paste into a new Word document and save processed Excel and Word files accordingly. The codes worked well until last night, and it gave me "Run time error 4198: Command failed" at appWord.Selection.Paste. Could somebody help me please? Thank you in advance.

    Regards
    Ridz

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Error during pasting to Word doc (Office 2003)

    Can you inspect what's on the clipboard when the error occurs? (For example by looking at the Clipboard task pane, or by trying to paste into another application)

  3. #3
    Star Lounger
    Join Date
    Feb 2005
    Posts
    61
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Error during pasting to Word doc (Office 2003)

    Thanks Han,

    I did that, but placing break after Copy Picture. The chart (graphic) is in the clipboard and pastable anywhere.

    Regards
    Ridz

  4. #4
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Error during pasting to Word doc (Office 2003)

    I notice that you make the Word application visible in your code. Do you see anything out of the ordinary if you switch to Word while code execution is paused?

    You could try starting Word before running the code and using GetObject instead of CreateObject. Change the line
    <code>
    Set appWord = CreateObject("Word.Application")
    </code>
    to
    <code>
    Set appWord = GetObject(, "Word.Application")
    </code>
    and comment out the line
    <code>
    appWord.Quit
    </code>
    by inserting an apostrophe <code>'</code> at the beginning of the line.

  5. #5
    Star Lounger
    Join Date
    Feb 2005
    Posts
    61
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Error during pasting to Word doc (Office 2003)

    I switched to Word and could not see anything out of ordinary, except the new doc is created.

    I tried your suggestion to change the codes - now it gives me run time error 429: ActiveX component can't create object at <pre>Set appWord = GetObject(, "Word.Application")</pre>



    Regards
    Ridz

  6. #6
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Error during pasting to Word doc (Office 2003)

    GetObject assumes that Word is already active, so you should start Word and leave it open, then switch to Excel and run the macro. Do you still get error 429 then?

  7. #7
    Star Lounger
    Join Date
    Feb 2005
    Posts
    61
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Error during pasting to Word doc (Office 2003)

    Thank you thank you....

    At least it is pasted on Word now. But I have a new problem...the client has named the file with characters such as ".", "&", and I believe it is causing error 5152: Invalid file name at <pre>.ActiveDocument.SaveAs Filename:=newWord</pre>. Any quick fix apart from manually going through 1218 files and change the filenames?

    Thank you again Hans

  8. #8
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Error during pasting to Word doc (Office 2003)

    You could remove all non-alphanumeric characters from the filename in the code. This could cause a problem if it results in non-unique names.

    Here is a function to strip all non-alphanumeric characters from a string:

    Function CleanName(strName As String) As String
    Dim i As Integer
    Dim c As String
    For i = 1 To Len(strName)
    c = Mid(strName, i, 1)
    Select Case UCaseŠ
    Case "0" To "9", "A" To "Z"
    CleanName = CleanName & c
    End Select
    Next i
    End Function

    You could use it like this in your code:
    ...
    Dim newWord As String
    newWord = Left(fileNameWord, Len(fileNameWord) - 4)
    newWord = Clean(GetFileName(newWord, "")) & ".doc"
    newWord = strTargetWordPath & newWord
    ...

  9. #9
    Star Lounger
    Join Date
    Feb 2005
    Posts
    61
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Error during pasting to Word doc (Office 2003)

    You are a life saver...


    Thank you so much

Posting Permissions

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