Results 1 to 5 of 5
  1. #1
    Join Date
    May 2005
    Posts
    1,191

    Unanswered: Application problem

    I have some code on the On-Click event of a button on a form. When the button is clicked, the code is supposed to open a word document and then do some stuff with it. Unfortunately, I can't even open the document, Access yells at me before that can even happen.

    Here's my code:
    PHP Code:
    Dim oApp As Word.Application
    Set oApp 
    = New Word.Application
        oApp
    .Visible True
        oApp
    .Documents.Open ("O:\PPAP-Pilot\PPAPTemplate.doc"
    When I run the code, it gives me a problem with the first line and pops up the attached msg box. Anyone know why it's yelling at me (other than the fact that I'm obviously coding something stupidly)?
    Attached Thumbnails Attached Thumbnails err.JPG  
    Me.Geek = True

  2. #2
    Join Date
    Dec 2004
    Location
    Madison, WI
    Posts
    3,926

    Code to open

    Here's some code I have to open docs/pics, etc. Maybe it will help in some way or another.

    Also, I just added in some code from this website which worked great:
    http://www.mvps.org/access/api/api0018.htm

    Private Sub cmdOpen_Click()
    Refresh
    Dim strFileName As String
    strFileName = GetNamePart(Me!BugSourceFileName)
    'strFileName = GetNamePart(dlgGetFile.FileName)
    Dim PType As String
    PType = right(Me!BugSourceFileName, 3)
    Call OpenProgram(PType, Me!BugSourceFileName)
    End Sub

    Function GetNamePart(strIn As String) As String
    Dim intCounter As Integer
    Dim strTmp As String
    For intCounter = Len(strIn) To 1 Step -1
    If MID$(strIn, intCounter, 1) <> "\" Then
    strTmp = MID$(strIn, intCounter, 1) & strTmp
    Else
    Exit For
    End If
    Next intCounter
    GetNamePart = strTmp
    End Function

    Function OpenProgram(PType As String, FN As String)
    DoCmd.OpenForm "StandBy"
    Forms!Standby!LblCaption.Caption = "Stand By - Opening File: " & strFileName
    Dim strFN As String
    strFN = GetNamePart(FN)
    On Error GoTo DD
    Select Case PType
    Case "doc"
    On Error GoTo DD
    Call Shell("WINWORD.exe " & Chr(34) & FN & Chr(34), vbMaximizedFocus)
    Exit Function
    Case "xls", "csv"
    On Error GoTo DD
    Call Shell("Excel.exe " & Chr(34) & FN & Chr(34), vbMaximizedFocus)
    Exit Function
    Case "BMP", "JPG"
    On Error GoTo DD
    Call Shell("MSPaint.exe " & Chr(34) & FN & Chr(34), vbMaximizedFocus)
    Exit Function
    Case "txt"
    On Error GoTo DD
    Call Shell("Notepad.exe " & Chr(34) & FN & Chr(34), vbMaximizedFocus)
    Exit Function
    End Select
    DD:
    On Error GoTo Dend
    Dim PN As Variant
    PN = InputBox("Still could not load this file type. The file could already be open or the application to open this file could not be found. Please type in the program name (i.e. executable application name such as MSWord or Excel) to load this file and I'll attempt to load the file again:")
    LUName = PN & " " & Chr(34) & FN & Chr(34)
    Call Shell(LUName, vbMaximizedFocus)
    Exit Function
    Dend:
    MsgBox ("Still could not load file. Contact your dbAdministrator or Network Administrator to locate the program to open this file type.")

    End Function
    Last edited by pkstormy; 11-09-06 at 16:37.
    Expert Database Programming
    MSAccess since 1.0, SQL Server since 6.5, Visual Basic (5.0, 6.0)

  3. #3
    Join Date
    May 2005
    Posts
    1,191
    Hey Paul,

    Thanks for the reply.

    Is it possible to use your method to open a particular Word document, perform a Save As to a specified location, load data into the form fields, print the document and then close it? That's what I'm trying to accomplish with my code. The crazy thing is, this same (or rather, similar) code works on a previous dB I made.

    Here's all the code I'm using at this point, but that is not working.

    'Open the Template
    Dim oApp As Word.Application
    Set oApp = New Word.Application
    oApp.Visible = True
    oApp.Documents.Open ("O:\PPAP-Pilot\PPAPTemplate.doc")

    'Import data and save as
    Dim oDoc As Word.Document
    With oApp
    Set oDoc = .Documents.Open("O:\PPAP-Pilot\PPAPTemplate.doc")
    .ActiveDocument.SaveAs (strFilePath)
    With oDoc
    .FormFields("FldWONo").Result = Nz(strWONo, "")
    .FormFields("FldPartNo").Result = Nz(strPartNo, "")
    .FormFields("FldDate").Result = Nz(strDate, "")
    End With
    .ActiveDocument.Save
    .ActiveDocument.Print
    .Close
    End With
    Me.Geek = True

  4. #4
    Join Date
    May 2005
    Location
    Nevada, USA
    Posts
    2,888
    Provided Answers: 6
    Related to your first post, do you have the Word library checked in Tools/References?
    Paul

  5. #5
    Join Date
    Dec 2004
    Location
    Madison, WI
    Posts
    3,926
    Not sure on your second questions. The code I supplied was based on the user entering and saving the WordDoc filename in a text field in one of my tables and then I used that to open (shell to) Word and then open the filename. I'm assuming that you would be able to do any of the Word menu items since it's shelling to Word. If you're merging the actual Word document into MSAccess, Paul (pbaldy) has a good point on checking the references.
    Expert Database Programming
    MSAccess since 1.0, SQL Server since 6.5, Visual Basic (5.0, 6.0)

Posting Permissions

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