+ Reply to Thread
Results 1 to 2 of 2

Run-time error '462' when I run macro more than once

Hybrid View

  1. #1
    Registered User
    Join Date
    11-12-2011
    Location
    london
    MS-Off Ver
    Excel 2003
    Posts
    6

    Run-time error '462' when I run macro more than once

    Hello again all, I'm back again with a small problem that I'm encountering when I run the macro I have been tinkering with more than once.

    I run the macro and it populates a word document for me with no problem, however when I close the test word documents and run the macro again I get a Run-time error '462'

    I have done a bit of reading and I believe this error is generated by the word application not closing down and running in the background (although I'm not really very sure about how or why this would be occuring). If anyone has come accross this sort of error before or can see any glaring problems in my code I'd really appreciate a few pointers, thanks in advance. K

    Private Sub CommandButton4_Click()
        Dim objWord As Word.Application
        Dim objDoc As Word.Document
        Dim objExcel As Object
        Dim theCombo As Control
        Dim x As Integer
        Dim y As Integer
        Dim Txt1 As String
        Dim Txt2 As String
        Dim Txt1Rplce As String
        Dim Txt2Rplce As String
        
        x = iAddedCount
        y = 1
        
        Set objWord = CreateObject("Word.Application")
        objWord.DisplayAlerts = False
        objWord.Visible = True
        objWord.Documents.Open ActiveWorkbook.Path & "\OSR Advisory Letter.dotm", ReadOnly:=True
        objWord.Activate
        objWord.Selection.Start = 1382
        
            For Each theCombo In OSRData.MultiPage1.Pages(2).Controls
                    
                    If x > 0 Then
    
                        objWord.Selection.FormFields.Add Range:=objWord.Selection.Range, Type:= _
                        wdFieldFormTextInput
    
                            With objWord.Dialogs(wdDialogFormFieldOptions)
                                .Name = ("Com" & y)
                                .Execute
                                objWord.Selection.ParagraphFormat.LeftIndent = InchesToPoints(0.63) 'not working throws an error when run a second time
                                objWord.Selection.Font.Bold = False
                                objWord.Selection.MoveRight
                                objWord.Selection.TypeParagraph
                                objWord.Selection.TypeParagraph
                            End With
                                
          
                        objWord.Selection.FormFields.Add Range:=objWord.Selection.Range, Type:= _
                        wdFieldFormTextInput
                        
                            With objWord.Dialogs(wdDialogFormFieldOptions)
                                .Name = ("Text1" & y)
                                .Execute
                                objWord.Selection.ParagraphFormat.LeftIndent = InchesToPoints(0.63) 'this line does not seem to generate the error when the previous line is commented out
                                objWord.Selection.Font.Bold = True
                                objWord.Selection.MoveRight
                                objWord.Selection.TypeParagraph
                                objWord.Selection.TypeParagraph
                            End With
                        
                            
                        objWord.Selection.FormFields.Add Range:=objWord.Selection.Range, Type:= _
                        wdFieldFormTextInput
                        
                            With objWord.Dialogs(wdDialogFormFieldOptions)
                                .Name = ("Text2" & y)
                                .Execute
                                'objWord.Selection.ParagraphFormat.LeftIndent = InchesToPoints(0.63)
                                objWord.Selection.Font.Bold = True
                                objWord.Selection.MoveRight
                                objWord.Selection.TypeParagraph
                                objWord.Selection.TypeParagraph
                            End With
                            
                            
                            With objWord.ActiveDocument
                                Txt1 = Me("Text1" & y).Text
                                
                                Txt2 = Me("Text2" & y).Text
                                
                                Txt1Rplce = Replace(Txt1, vbLf, "")
                                
                                Txt2Rplce = Replace(Txt2, vbLf, "")
                                
                                objWord.ActiveDocument.FormFields("Com" & y).Result = Me("Combo" & y).Text
                              
                                objWord.ActiveDocument.FormFields("Text1" & y).Result = "Observations:" & vbCr & Txt1Rplce 'Me("Text1" & y).Text
                                
                                objWord.ActiveDocument.FormFields("Text2" & y).Result = "Action(s):" & vbCr & Txt2Rplce 'Me("Text2" & y).Text
                                
                            End With
    
                            
                    End If
                    
                        y = y + 1
                        x = x - 1
                        
           Next theCombo
    
    End Sub
    the offending line is the

    objWord.Selection.ParagraphFormat.LeftIndent = InchesToPoints(0.63)
    I have no idea why a formatting issue would throw an error, any ideas?

  2. #2
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Run-time error '462' when I run macro more than once

    Cross post.
    If posting code please use code tags, see here.

+ 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. Replies: 0
    Last Post: 10-20-2014, 05:33 PM
  2. [SOLVED] Sort Macro Run-time error '1004': Application/Object-defined error.
    By sam1212 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 08-22-2014, 10:05 AM
  3. MACRO ERROR WITH EXCEL 2013 - Run-Time Error '-2147417848 (80010108)'
    By graiggoriz in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 05-29-2014, 11:07 AM
  4. [SOLVED] Simple Calendar pop up macro --> error Run-time error '424': Object required
    By am_hawk in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 10-11-2013, 10:38 AM
  5. [SOLVED] Macro error - Run time error 1004 (App defined/Object Defined error)
    By jlax34 in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 06-06-2012, 03:02 PM
  6. Excel Macro Error - Run time error 1004 - Paste method of worksheet class failed
    By kvflynn in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 05-15-2012, 10:51 AM
  7. Run-time error '1004': Macro error, only when shared
    By Pergo in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 04-13-2011, 12:57 PM

Tags for this Thread

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