+ Reply to Thread
Results 1 to 5 of 5

Requesting debugging help

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    04-02-2012
    Location
    Ohio
    MS-Off Ver
    Excel 2010
    Posts
    452

    Requesting debugging help

    I have this macro,

    Sub exporttojournal()
     
     
      Dim wrdApp As Word.Application
        Dim wrdDoc As Word.Document
       
        Dim Rng As Word.Range
        Dim i As Integer
       Dim pathName As String
       
         If MsgBox("Are you sure you want to export data as " & Range("AD9").Text & " ?", vbYesNo + vbQuestion) = vbNo Then Exit Sub
       
        Set wrdApp = CreateObject("Word.Application")
        wrdApp.Visible = True
        pathName = Sheets("ADMIN SHEET").Range("A2").Value
        Set wrdDoc = wrdApp.Documents.Open(pathName)
        
        With Sheets("Calculator")
            Set rngWord = .Range("U1:X1", .Range("U1:X1").End(xlDown))
        End With
        
        rngWord.Copy
        
        Set Rng = wrdDoc.Range
        Rng.Collapse 0  'wdCollapseEnd
        Rng.PasteExcelTable False, True, False
        With wrdDoc
        .Content.InsertAfter "E1RM-"
        With Sheets("Calculator")
            Set rngWord = .Range("R1")
        End With
        
        rngWord.Copy
        
        Set Rng = wrdDoc.Range
        Rng.Collapse 0  'wdCollapseEnd
        Rng.PasteAndFormat (wdFormatPlainText)
        .Content.InsertParagraphAfter
        .Content.InsertAfter "Total Volume-"
        With Sheets("Calculator")
            Set rngWord = .Range("R2")
        End With
        
        rngWord.Copy
        
        Set Rng = wrdDoc.Range
        Rng.Collapse 0  'wdCollapseEnd
        Rng.PasteAndFormat (wdFormatPlainText)
        .Content.InsertParagraphAfter
        .Content.InsertAfter "Average Intensity-"
        With Sheets("Calculator")
            Set rngWord = .Range("R6")
        End With
        
        rngWord.Copy
        
        
        Set Rng = wrdDoc.Range
        Rng.Collapse 0  'wdCollapseEnd
        Rng.PasteAndFormat (wdFormatPlainText)
    End With
    
    With wrdDoc
          
         .SaveAs (pathName & "ADMIN SHEET!A2")
           .Close ' close the document
        End With
        wrdApp.Quit ' close the Word application
        Set wrdDoc = Nothing
        Set wrdApp = Nothing
    With Sheets
        Application.CutCopyMode = False
        MsgBox "Data has been exported"
    End With
    End Sub
    and am getting an error at this line, it says,
    Run-time error'4198':
    Method 'PasteAndFormat' of object 'Range' failed
        Rng.PasteAndFormat (wdFormatPlainText)
    This line is the 37th line down in the code.
    What is the fix for this?

  2. #2
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS 365 Family 64-bit 2502
    Posts
    27,185

    Re: Requesting debugging help

    This can't be reproduced without your files. At the point where it fails, what is Rng? What is rngWord? Did the copy happen properly? I suspect that the problem may be in the Copy statement rather than the statement that is raising the error. I think that at the point where your code stops, the clipboard should have your copied content on it and you can try a manual paste into a Word document and see if that works. If it fails it may give you insight as to what's wrong. You could also try inserting a line of code that pastes into Excel instead of Word and see what happens there.
    Jeff
    | | |會 |會 |會 |會 | |:| | |會 |會
    Read the rules
    Use code tags to [code]enclose your code![/code]

  3. #3
    Forum Contributor
    Join Date
    04-02-2012
    Location
    Ohio
    MS-Off Ver
    Excel 2010
    Posts
    452

    Re: Requesting debugging help

    I went through your questions and was able to find the error. Thank you.

  4. #4
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS 365 Family 64-bit 2502
    Posts
    27,185

    Re: Requesting debugging help

    Glad to hear it. Care to share what you found?

  5. #5
    Forum Contributor
    Join Date
    04-02-2012
    Location
    Ohio
    MS-Off Ver
    Excel 2010
    Posts
    452

    Re: Requesting debugging help

    As far as I can tell, the sheet had been modified but the code hadn't been updated to reflect the modification. It was trying to copy and paste a blank cell, when I changed the cell to be copy and pasted it worked.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Requesting help w/macro issue VBA
    By Jack7774 in forum Excel Programming / VBA / Macros
    Replies: 12
    Last Post: 08-21-2014, 04:32 PM
  2. Requesting help with date function
    By Kitkatlv in forum Excel General
    Replies: 2
    Last Post: 11-05-2012, 10:02 PM
  3. Help In Requesting Data
    By pasteis in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 07-03-2012, 01:14 PM
  4. Requesting Help with Macro
    By Endlin3 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 03-08-2010, 06:39 PM

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1