Results 1 to 5 of 5
  1. #1
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Word code works second time only (Office2003/Word2003)

    You don't specify what CentimetersToPoint belongs to. Try this version:
    <code>
    .TopMargin = objWord.CentimetersToPoints(1.34)</code>

  2. #2
    3 Star Lounger
    Join Date
    Jan 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    314
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Word code works second time only (Office2003/Word2003)

    My problem seems to be the opposite of the 'early binding, global object' problem discussed widely on the web.
    When I run the following code from scratch, it fails at the last line shown. ( .TopMargin = CentimetersToPoints(1.34))
    giving the error message 462 "the remote server machine does not exist or is unavailable".
    I've searched for any unqualified references but can't find any.
    If I stop the code execution, then run it again, it works THE SECOND TIME.
    But it does this even if I kill any instance of word running in task scheduler, i.e.
    start again from what is ostensibly the same situation with regard to any running instances of Word.
    The problem usually discussed is the other way around, where the code does not run correctly the second time.
    Mine runs the second time!
    Any ideas here please?
    CODE
    Dim objWord As Word.Application
    Dim docWord As Word.Document
    Dim docActive As Word.Document
    Dim strOriginalPrinter As String
    Dim wrdDoc As Word.Document

    'if Word is already running, use that instance
    On Error GoTo NoInstanceExists
    Set objWord = GetObject(, "Word.Application")
    GoTo CreateNewDocument

    NoInstanceExists:
    If TypeName(objWord) = "Nothing" Then
    'Word was not open
    Set objWord = CreateObject("Word.Application")
    GoTo CreateNewDocument
    End If

    CreateNewDocument:
    objWord.Visible = True
    If objWord.Documents.Count > 0 Then
    Set docActive = objWord.ActiveDocument
    End If
    Set docWord = objWord.Documents.Add
    Debug.Print docWord.Name

    '>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

    With objWord.Documents(docWord).PageSetup
    .LineNumbering.Active = False
    .Orientation = wdOrientLandscape
    .TopMargin = CentimetersToPoints(1.34)

    etc etc

  3. #3
    3 Star Lounger
    Join Date
    Jan 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    314
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Word code works second time only (Office2003/Word2003)

    Thanks Hans,
    I'm glad it was that easily fixed.
    I 'created' the code using a macro in Word which clearly knows what object CentimetersToPoints is a method of.
    However, the error has not arisen very often in practice - just enough to be annoying.
    Why would it not arise every time the code is run?

  4. #4
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Word code works second time only (Office2003/Word2003)

    It depends on whether Word was already running, and in general, this kind of problem is hard to pin down exactly.

  5. #5
    3 Star Lounger
    Join Date
    Jan 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    314
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Word code works second time only (Office2003/Word2003)

    Thanks. That makes sense, even if I don't quite precisely why!
    David

Posting Permissions

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