• Error during pasting to Word doc (Office 2003)

    Home » Forums » AskWoody support » Productivity software by function » MS Excel and spreadsheet help » Error during pasting to Word doc (Office 2003)

    Author
    Topic
    #426979

    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

    Viewing 0 reply threads
    Author
    Replies
    • #988131

      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)

      • #988136

        Thanks Han,

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

        Regards
        Ridz

        • #988140

          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

          Set appWord = CreateObject("Word.Application")

          to

          Set appWord = GetObject(, "Word.Application")

          and comment out the line

          appWord.Quit

          by inserting an apostrophe ' at the beginning of the line.

          • #988147

            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

            Set appWord = GetObject(, "Word.Application")

            Regards
            Ridz

            • #988149

              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?

            • #988150

              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

              .ActiveDocument.SaveAs Filename:=newWord

              . Any quick fix apart from manually going through 1218 files and change the filenames?

              Thank you again Hans

            • #988152

              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

            • #988155

              You are a life saver…

              Thank you so much

    Viewing 0 reply threads
    Reply To: Error during pasting to Word doc (Office 2003)

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: