Results 1 to 3 of 3

Thread: Word document

  1. #1
    Join Date
    Mar 2006
    Posts
    42

    Unanswered: Word document

    I have a function that open a word document to write notes in it.The functions works OK but only when a doc file name Notes is available.is it possible to create this word file in case it is not existing? In my function there is line called create object but somehow it does not work.

    My function is :
    Public Function MyNotes()
    Dim appW As Object
    On Error Resume Next
    Set appW = GetObject(, "Word.Application")
    Dim nErr As Integer
    nErr = Err
    On Error GoTo 0
    If nErr Then
    Set appW = CreateObject("Word.Application")
    End If
    appW.Visible = True
    appW.Documents.Open (" C:\be\Notes")
    End Function

  2. #2
    Join Date
    Feb 2004
    Location
    One Flump in One Place
    Posts
    14,912
    I'm not quite sure that is exactly what your code does but...

    Do you want to check whether or not "C:\be\Notes" exists and create it if not?
    Do you then want to check whether or not "C:\be\Notes" is currently open and open it if not?
    Testimonial:
    pootle flump
    ur codings are working excelent.

  3. #3
    Join Date
    Mar 2006
    Posts
    163
    First you should check if the document exists like this and then create it if it doesn't.
    Code:
    Public Function MyNotes()
    Dim appW As Object
    Dim doc As Object
    Dim Response As Variant
    Dim nErr As Long
    
        On Error Resume Next
        Set appW = GetObject(, "Word.Application")
    
        nErr = Err
        On Error GoTo 0
        If nErr Then
            Set appW = CreateObject("Word.Application")
        End If
    
        If Dir("C:\be\Notes.doc") = "" Then
            Response = MsgBox("Notes document doesn't exist, do you want to create it?", vbYesNo)
    
            If Response = vbYes Then
                Set doc = appW.Documents.Add
                doc.SaveAs "C:\be\Notes.doc"
            Else
                Exit Function
            End If
        Else
            appW.Visible = True
            appW.Documents.Open (" C:\be\Notes.doc")
        End If
    End Function

Posting Permissions

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