Consulting

Results 1 to 14 of 14

Thread: Annoying "462"

  1. #1

    Annoying "462"

    Just as like the title. 1st time running smooth, but second time I have to cancel and re-run again.
    Any ideas where I need to tune. (I have tried to use oWORD as the object of "ParagraphFormat" with no luck)


    [CODE]
    Dim oWORD As Object
    Dim oDOC As Word.Document

    If oWORD Is Nothing Then
    Set oWORD = CreateObject("Word.application")
    End if

    Set oDOC = oWORD.Documents.Add
    With oDOC.Content.ParagraphFormat
    .LeftIndent = InchesToPoints(0)
    .RightIndent = InchesToPoints(0)
    .SpaceBefore = 0
    .SpaceBeforeAuto = False
    .SpaceAfter = 0
    .SpaceAfterAuto = False
    .LineSpacingRule = wdLineSpaceSingle
    .Alignment = wdAlignParagraphLeft
    .WidowControl = True
    .KeepWithNext = False
    .KeepTogether = False
    .PageBreakBefore = False
    .NoLineNumber = False
    .Hyphenation = True
    .FirstLineIndent = InchesToPoints(0)
    .OutlineLevel = wdOutlineLevelBodyText
    .CharacterUnitLeftIndent = 2
    .CharacterUnitRightIndent = 0
    .CharacterUnitFirstLineIndent = 0
    .LineUnitBefore = 0
    .LineUnitAfter = 0
    .MirrorIndents = False
    .TextboxTightWrap = wdTightNone
    End With

    ...............
    [/CODE}

  2. #2
    VBAX Master
    Joined
    Feb 2011
    Posts
    1,480
    Location
    Please use the VBA tags rather than the CODE tags. Your code will be much more readable on this forum.

    Where are you running this code from? Why are you creating a new instance of Word? If you are running this code from a VBA module within Word, then get rid of your oWORD object entirely.

    Don't know what 462 error is... it would be better to post what the error description is, rather than the error number.

    Also... post what you are trying to do. You are trying to create a new document with a specific set of formats when running a macro? If so, then you do not need VBA at all.

    Create a template (.dot or .dotx, depending on your version), format that blank template the way you want it to be formatted, and then simply create a new document based on that template.
    _______________________________________________
    Please don't cross-post without providing links to your cross-posts. We answer questions for free. Please don't waste the time of the people helping you.
    For cross-posting etiquette, please read: http://www.excelguru.ca/content.php?184

    - Frosty

  3. #3
    Frosty,

    Thanks for the rescue.
    The WORD oWORD object is created through Excel to transfer data to Word.
    1st time is ok always but NOT 2nd time, in which reset is necessary.
    The line got stuck is
    "With oDOC.Content.ParagraphFormat"
    with the following error
    Run-time error '462':
    The remote server machine does not exist or is unavailable

    I believe it is something to do with not recognizing the Word object.
    Thanks.

    [VBA]
    Dim oWORD As Object
    Dim oDOC As Word.Document

    If oWORD Is Nothing Then
    Set oWORD = CreateObject("Word.application")
    End if

    Set oDOC = oWORD.Documents.Add
    With oDOC.Content.ParagraphFormat
    .LeftIndent = InchesToPoints(0)
    .RightIndent = InchesToPoints(0)
    .SpaceBefore = 0
    .SpaceBeforeAuto = False
    .SpaceAfter = 0
    .SpaceAfterAuto = False
    .LineSpacingRule = wdLineSpaceSingle
    .Alignment = wdAlignParagraphLeft
    .WidowControl = True
    .KeepWithNext = False
    .KeepTogether = False
    .PageBreakBefore = False
    .NoLineNumber = False
    .Hyphenation = True
    .FirstLineIndent = InchesToPoints(0)
    .OutlineLevel = wdOutlineLevelBodyText
    .CharacterUnitLeftIndent = 2
    .CharacterUnitRightIndent = 0
    .CharacterUnitFirstLineIndent = 0
    .LineUnitBefore = 0
    .LineUnitAfter = 0
    .MirrorIndents = False
    .TextboxTightWrap = wdTightNone
    End With

    ...............
    oDOC.SaveAs ("C:\test1.docx")
    oDOC.Close
    Set oDOC = Nothing

    [/VBA}

  4. #4
    VBAX Master
    Joined
    Feb 2011
    Posts
    1,480
    Location
    When you're using the code tags... you need to close the tag properly with a bracket, not a curly bracket. "[" VBA "]" (no spaces, no quotes).

    When you're using CreateObject, that will create a new instance no matter what. I think you probably want to use GetObject (to see if an instance is already running) first, and then use create object. So it would be something like...
    [VBA]
    Public Sub Demo()
    Dim oWordApp As Object
    Dim oWordDoc As Word.Document
    Dim bAppCreated As Boolean

    On Error Resume Next
    'attempt to get an existing, ignoring any error
    Set oWordApp = GetObject(, "Word.Application")
    'reset error trapping
    On Error GoTo 0

    'if that attempt fails, then create it
    If oWordApp Is Nothing Then
    Set oWordApp = CreateObject("Word.application")
    bAppCreated = True
    End If

    Set oWordDoc = oWordApp.Documents.Add
    With oWordDoc.Content.ParagraphFormat
    .LeftIndent = InchesToPoints(0)
    .RightIndent = InchesToPoints(0)
    .SpaceBefore = 0
    .SpaceBeforeAuto = False
    .SpaceAfter = 0
    .SpaceAfterAuto = False
    .LineSpacingRule = wdLineSpaceSingle
    .Alignment = wdAlignParagraphLeft
    .WidowControl = True
    .KeepWithNext = False
    .KeepTogether = False
    .PageBreakBefore = False
    .NoLineNumber = False
    .Hyphenation = True
    .FirstLineIndent = InchesToPoints(0)
    .OutlineLevel = wdOutlineLevelBodyText
    .CharacterUnitLeftIndent = 2
    .CharacterUnitRightIndent = 0
    .CharacterUnitFirstLineIndent = 0
    .LineUnitBefore = 0
    .LineUnitAfter = 0
    .MirrorIndents = False
    .TextboxTightWrap = wdTightNone
    End With

    oWordDoc.SaveAs ("C:\test1.docx")
    oWordDoc.Close
    'this may be unnecessary, since you've already closed it
    'however, it doesn't hurt
    Set oWordDoc = Nothing

    'typical "good" garbage collection...
    'if the app was created, then quit that app
    If bAppCreated Then
    oWordApp.Quit
    Set oWordApp = Nothing
    End If
    End Sub
    [/VBA]

    However, the primary issue is that I'm not sure why you need to do this in the first place. In what context are you performing this function? It would be better to create a template with the formatting you desire (the code in your With...End With block), and then simply use that template.

    This is obviously a dummy routine, since you're creating a test document... but I suspect what you're trying to do is not what you really want to do.

    Can you give a bit more explanation?
    _______________________________________________
    Please don't cross-post without providing links to your cross-posts. We answer questions for free. Please don't waste the time of the people helping you.
    For cross-posting etiquette, please read: http://www.excelguru.ca/content.php?184

    - Frosty

  5. #5
    Frosty,

    The original need of
    "With oDOC.Content.ParagraphFormat

    ...

    End With"

    is to set the parameters of line spacing, left indent ... in Word 2007

    But somehow it cannot be rerun smoothly.
    I have plugged in the format you posted, but I still have the "462" error issue.
    Every time I learned something new from you.
    Thanks.

  6. #6
    VBAX Master
    Joined
    Feb 2011
    Posts
    1,480
    Location
    Hmm... try...
    Dim oWordDoc As Object
    instead of
    Dim oWordDoc As Word.Application

    The problem here is that the "The remote server machine does not exist or is unavailable" error means that you're not finding the application. The question is... why?

    It could be an issue with your Operating System, your permissions, how you're calling the application, or just some part of the code (and the way you're using it).

    Since you're not showing all of the code (you don't even show the Sub and End Sub), it's tough to diagnose.

    When you step through my code... does CreateObject happen every time, or does GetObject get used?

    Does the code work if you start Word normally and let it run in the back ground? Does GetObject get used in that circumstance?
    _______________________________________________
    Please don't cross-post without providing links to your cross-posts. We answer questions for free. Please don't waste the time of the people helping you.
    For cross-posting etiquette, please read: http://www.excelguru.ca/content.php?184

    - Frosty

  7. #7
    VBAX Master
    Joined
    Feb 2011
    Posts
    1,480
    Location
    Don't plug my code into whatever existing routines you have-- does my routine work multiple times, just by itself?
    _______________________________________________
    Please don't cross-post without providing links to your cross-posts. We answer questions for free. Please don't waste the time of the people helping you.
    For cross-posting etiquette, please read: http://www.excelguru.ca/content.php?184

    - Frosty

  8. #8
    No, Sir and same error "462".
    Can I set the ParagraphFormat with oWORD object rather than oDOC object?
    Thanks.

  9. #9
    VBAX Master
    Joined
    Feb 2011
    Posts
    1,480
    Location
    No, you can't set the ParagraphFormat object using the application object.

    Does this code run, multiple times? If not... what is the exact error description and what line does it fail on?
    [VBA]
    Public Sub Demo()
    Dim oWordApp As Object
    Dim oWordDoc As Word.Document
    Dim bAppCreated As Boolean

    On Error Resume Next
    'attempt to get an existing, ignoring any error
    Set oWordApp = GetObject(, "Word.Application")
    'reset error trapping
    On Error GoTo 0

    'if that attempt fails, then create it
    If oWordApp Is Nothing Then
    Set oWordApp = CreateObject("Word.application")
    bAppCreated = True
    End If

    Set oWordDoc = oWordApp.Documents.Add
    ' With oWordDoc.Content.ParagraphFormat
    ' .LeftIndent = InchesToPoints(0)
    ' .RightIndent = InchesToPoints(0)
    ' .SpaceBefore = 0
    ' .SpaceBeforeAuto = False
    ' .SpaceAfter = 0
    ' .SpaceAfterAuto = False
    ' .LineSpacingRule = wdLineSpaceSingle
    ' .Alignment = wdAlignParagraphLeft
    ' .WidowControl = True
    ' .KeepWithNext = False
    ' .KeepTogether = False
    ' .PageBreakBefore = False
    ' .NoLineNumber = False
    ' .Hyphenation = True
    ' .FirstLineIndent = InchesToPoints(0)
    ' .OutlineLevel = wdOutlineLevelBodyText
    ' .CharacterUnitLeftIndent = 2
    ' .CharacterUnitRightIndent = 0
    ' .CharacterUnitFirstLineIndent = 0
    ' .LineUnitBefore = 0
    ' .LineUnitAfter = 0
    ' .MirrorIndents = False
    ' .TextboxTightWrap = wdTightNone
    ' End With

    oWordDoc.SaveAs ("C:\test1.docx")
    oWordDoc.Close
    'this may be unnecessary, since you've already closed it
    'however, it doesn't hurt
    Set oWordDoc = Nothing

    'typical "good" garbage collection...
    'if the app was created, then quit that app
    If bAppCreated Then
    oWordApp.Quit
    Set oWordApp = Nothing
    End If
    End Sub
    [/vba]
    _______________________________________________
    Please don't cross-post without providing links to your cross-posts. We answer questions for free. Please don't waste the time of the people helping you.
    For cross-posting etiquette, please read: http://www.excelguru.ca/content.php?184

    - Frosty

  10. #10
    VBAX Master
    Joined
    Feb 2011
    Posts
    1,480
    Location
    Make sure to try the code when Word is already open vs. when Word is closed.

    If Word is closed, make sure to check your task manager to verify you do not have multiple WINWORD.EXE processes running on the processes tab. If you do, end process on all of them (making sure to save ANY work in Word you have open).

    I know a lot of this seems basic, but you need to make sure you don't have any process hooks to any winword.exe processes.
    _______________________________________________
    Please don't cross-post without providing links to your cross-posts. We answer questions for free. Please don't waste the time of the people helping you.
    For cross-posting etiquette, please read: http://www.excelguru.ca/content.php?184

    - Frosty

  11. #11
    Quote Originally Posted by doubtfire
    Just as like the title. 1st time running smooth, but second time I have to cancel and re-run again.
    Any ideas where I need to tune. (I have tried to use oWORD as the object of "ParagraphFormat" with no luck)


    [code]
    Dim oWORD As Object
    Dim oDOC As Word.Document

    If oWORD Is Nothing Then
    Set oWORD = CreateObject("Word.application")
    End if

    Set oDOC = oWORD.Documents.Add
    With oDOC.Content.ParagraphFormat
    .LeftIndent = InchesToPoints(0)
    .RightIndent = InchesToPoints(0)
    .SpaceBefore = 0
    .SpaceBeforeAuto = False
    .SpaceAfter = 0
    .SpaceAfterAuto = False
    .LineSpacingRule = wdLineSpaceSingle
    .Alignment = wdAlignParagraphLeft
    .WidowControl = True
    .KeepWithNext = False
    .KeepTogether = False
    .PageBreakBefore = False
    .NoLineNumber = False
    .Hyphenation = True
    .FirstLineIndent = InchesToPoints(0)
    .OutlineLevel = wdOutlineLevelBodyText
    .CharacterUnitLeftIndent = 2
    .CharacterUnitRightIndent = 0
    .CharacterUnitFirstLineIndent = 0
    .LineUnitBefore = 0
    .LineUnitAfter = 0
    .MirrorIndents = False
    .TextboxTightWrap = wdTightNone
    End With

    ...............
    [/CODE}

    I just tried your code in my Word VBA. It works perfectly fine, tried almost 10 times.

    Just added a line to see if my Word is actually creating an object.
    [vba]
    oWORD.Visible = True
    [/vba]

    On every execution the new word instance with a blank document added correctly.


    FYI: using Excel 2010 on Windows 7.

    Cheers

  12. #12
    Chandansify,

    Error "462" has to do with the Word.Application object since

    With oDOC.Content.ParagraphFormat
    .LeftIndent = InchesToPoints(0)
    .RightIndent = InchesToPoin


    ...

    End with

    I am creating Word object through Excel.
    I used oWORD instead of oDOC to do the ParagraphFormat and it works in
    Word 2007 in repeatingly creating documents.
    Thanks for the tips and I'm open to any advice/suggestion/tip.

  13. #13
    VBAX Master
    Joined
    Feb 2011
    Posts
    1,480
    Location
    Could you clarify for anyone else that comes along, doubtfire? No idea what you mean when you say you use oWORD instead of oDOC to do paragraphformat.

    As I said in the previous post-- you can not use the application object instead of a document object. You can't even use a document object. You have to use a range object.

    How you get to that range object can vary, but are you saying your issue has been solved in this thread? If so, how? Please show the code that you have working.
    _______________________________________________
    Please don't cross-post without providing links to your cross-posts. We answer questions for free. Please don't waste the time of the people helping you.
    For cross-posting etiquette, please read: http://www.excelguru.ca/content.php?184

    - Frosty

  14. #14
    Frosty,

    I do apologize that I am still trying to locate the old code or backup.
    Once I get a chance to find and see again the error code "462", I would definitely post it.

    Thanks again.

Posting Permissions

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