More Control While Opening Word

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • smugcool
    New Member
    • Apr 2007
    • 81

    More Control While Opening Word

    hi,
    I am having a problem while providing more control while opening word..Below is the code where i am able to open the file. in this code when somebody clicks on command2 button a input box opens and it asks for CRC ID & then it opens the required docuemnt from the path as below.

    But i want
    1)if any CRCID is not found in the path, one msgbox will appear.
    2) When somebody clicks on cancel button of input box. It should go to form only.

    Kindly help me out with this problem. Thanx in Advance

    Private Sub Command2_Click( )
    Dim intCRID, i As Integer
    Dim StrFile As String
    Dim appword As New Word.Applicatio n
    intCRID = InputBox("CRC ID:", "OPEN CRC ANALYSIS DOCUMENT", "")
    appword.Visible = True
    appword.Documen ts.Open ("D:\projects\C RC MANAGEMENT\vb\" & intCRID & " .doc")
    End Sub
  • QVeen72
    Recognized Expert Top Contributor
    • Oct 2006
    • 1445

    #2
    Hi,

    To check the file exists or not use this code:

    [code=vb]
    If Dir("D:\project s\CRC MANAGEMENT\vb\" & intCRID & " .doc",vbDirecto ry) <> "" Then
    appword.Documen ts.Open ("D:\projects\C RC MANAGEMENT\vb\" & intCRID & " .doc")
    Else
    Msgbox "Word Doc " intCRID & " Not Found "
    End If
    [/code]

    REgards
    Veena
    Last edited by QVeen72; Oct 6 '07, 07:12 AM. Reason: code tags

    Comment

    • smugcool
      New Member
      • Apr 2007
      • 81

      #3
      Originally posted by QVeen72
      Hi,

      To check the file exists or not use this code:

      [code=vb]
      If Dir("D:\project s\CRC MANAGEMENT\vb\" & intCRID & " .doc",vbDirecto ry) <> "" Then
      appword.Documen ts.Open ("D:\projects\C RC MANAGEMENT\vb\" & intCRID & " .doc")
      Else
      Msgbox "Word Doc " intCRID & " Not Found "
      End If
      [/code]

      REgards
      Veena

      Thanx Veena,

      i got my first questions answer. I am having another similar problem when i create a analysis document.

      I want when i will click on cancel button of input box it should return to the form. Currently its giving me "runtime error 4198" i.e. command failed.

      below is the code for the button.

      Private Sub Command1_Click( )
      Dim appword As New Word.Applicatio n
      Dim intCRID, i As Integer
      Dim strDate, strFileName As String
      Dim Range As Range
      'Define the path where to save the CR.doc
      intCRID = InputBox("CRC ID:", "Create ANALYSIS Document", "")
      strDate = Format(Now(), "yyyy-mm-dd")
      strFileName = "CR " & intCRID & " cr.doc"
      'Open the RMA Word Template
      appword.Documen ts.Open ("D:\projects\C RC MANAGEMENT\vb\C R.doc")
      ' Make Word visible through the Application object.
      appword.Visible = True
      ' Save the new CRID document
      appword.ActiveD ocument.SaveAs "D:\projects\CR C MANAGEMENT\vb\" & intCRID & " .doc"
      End Sub

      Comment

      • QVeen72
        Recognized Expert Top Contributor
        • Oct 2006
        • 1445

        #4
        Hi,

        When Cancel is Clicked, the Return value from Inputbox is empty,
        you can check :

        When you have declared intCrID as Integer, you need to take Val of InputBox,
        And Check For
        intCrID = 0



        Regards
        Veena

        Comment

        • smugcool
          New Member
          • Apr 2007
          • 81

          #5
          No dear its not working.
          Again the same kind of error is coming.

          Comment

          • QVeen72
            Recognized Expert Top Contributor
            • Oct 2006
            • 1445

            #6
            Hi,

            Write appropriate Error Handler..

            In your Code at the beginning, write "On Error Resume Next"
            and before end sub , write this code :

            [code=vb]
            If Err.Number = 4198 Then
            objwordapp.Quit
            MsgBox "User cancelled"
            End If
            [/code]

            Regards
            Veena

            Comment

            • smugcool
              New Member
              • Apr 2007
              • 81

              #7
              Thats fine Veena. I think you got it right.

              But what i want is that after canceling the input box it should not resume to the next step i.e. opeing the word file. But it should go the mainform.

              Comment

              • QVeen72
                Recognized Expert Top Contributor
                • Oct 2006
                • 1445

                #8
                Hi,

                write this after InputBox :

                Dim intCRID As String

                intCRID = InputBox("CRC ID:", "Create ANALYSIS Document", "")
                If Trim(intCrID) ="" Then
                ' Write Unload Me/ Or any other function to exit
                '
                Exit Sub
                End If

                REgards
                Veena

                Comment

                • smugcool
                  New Member
                  • Apr 2007
                  • 81

                  #9
                  Originally posted by QVeen72
                  Hi,

                  write this after InputBox :

                  Dim intCRID As String

                  intCRID = InputBox("CRC ID:", "Create ANALYSIS Document", "")
                  If Trim(intCrID) ="" Then
                  ' Write Unload Me/ Or any other function to exit
                  '
                  Exit Sub
                  End If

                  REgards
                  Veena


                  Thanx very much Veena.Without your support i couldn't have submited this project. I think it was a great help from you that i am about to complete this project by monday only. Thanx a lot again for your great support.

                  Comment

                  • QVeen72
                    Recognized Expert Top Contributor
                    • Oct 2006
                    • 1445

                    #10
                    Hi,

                    You are always welcome.
                    Glad you could complete your project on time.

                    Regards
                    Veena

                    Comment

                    Working...