Log in

View Full Version : Annoying "462"



doubtfire
08-23-2012, 09:07 AM
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)
:banghead:

[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}

Frosty
08-23-2012, 11:56 AM
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.

doubtfire
08-23-2012, 02:33 PM
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}

Frosty
08-23-2012, 02:45 PM
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...

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


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?

doubtfire
08-23-2012, 03:03 PM
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.

Frosty
08-23-2012, 03:16 PM
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?

Frosty
08-23-2012, 03:17 PM
Don't plug my code into whatever existing routines you have-- does my routine work multiple times, just by itself?

doubtfire
08-23-2012, 03:26 PM
No, Sir and same error "462".
Can I set the ParagraphFormat with oWORD object rather than oDOC object?
Thanks.

Frosty
08-23-2012, 03:31 PM
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?

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

Frosty
08-23-2012, 03:33 PM
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.

chandansify
08-30-2012, 02:12 AM
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)
:banghead:

[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.:thumb

:hi: Just added a line to see if my Word is actually creating an object.

oWORD.Visible = True


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


FYI: using Excel 2010 on Windows 7.

Cheers :beerchug:

doubtfire
08-30-2012, 07:08 AM
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.
:bow:

Frosty
08-30-2012, 07:48 AM
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.

doubtfire
09-01-2012, 06:07 PM
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.