Open Report or Word Doc

D Willett
SilverLounger
Posts: 1728
Joined: 25 Jan 2010, 08:34
Location: Stoke on Trent - Staffordshire - England

Open Report or Word Doc

Post by D Willett »

I've looked at trying to open a Word Doc if it is not an Access Report:
My code is not right but can this be done;

Code: Select all

Private Sub cmdPreviewasPDF_Click()

Select Case lstReports.Column(1)

Case "Visual Heath Check.docx"

Application.FollowHyperlink "C:\MMSW\Visual Heath Check.docx"
Case Else

    DoCmd.OutputTo acOutputReport, Forms![frmJobReports]!lstReports.Column(1), acFormatPDF, _
        "C:\Temp\" & Forms![frmJobReports]!lstReports.Column(1) & ".pdf", True
        
End Select
DoCmd.Close acForm, Me.Name
End Sub
Cheers ...

Dave.

User avatar
HansV
Administrator
Posts: 78586
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Open Report or Word Doc

Post by HansV »

Following a hyperlink to a Word document should work. What is the problem? Do you get an error message? If so, what does it say?
Best wishes,
Hans

D Willett
SilverLounger
Posts: 1728
Joined: 25 Jan 2010, 08:34
Location: Stoke on Trent - Staffordshire - England

Re: Open Report or Word Doc

Post by D Willett »

Runtime error:2059.
Access can't find the object... followed by the path of the file.
The file is in the correct path.
Cheers ...

Dave.

User avatar
HansV
Administrator
Posts: 78586
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Open Report or Word Doc

Post by HansV »

There must be an error in either the path or the filename - please check very carefully. Shouldn't it be Health instead of Heath?
Best wishes,
Hans

D Willett
SilverLounger
Posts: 1728
Joined: 25 Jan 2010, 08:34
Location: Stoke on Trent - Staffordshire - England

Re: Open Report or Word Doc

Post by D Willett »

Got it... although the spelling was incorrect it was the same in the table so it wouldn't have made a difference.
What I hadn't done was include the path which lstReports returned. I've renamed the file and removed the spaces.

Code: Select all

Private Sub cmdPreviewasPDF_Click()

Select Case lstReports.Column(1)

Case "C:\MMSW\VisualHealthCheck.docx"

Application.FollowHyperlink "C:\MMSW\VisualHealthCheck.docx"
Case Else
    DoCmd.OutputTo acOutputReport, Forms![frmJobReports]!lstReports.Column(1), acFormatPDF, _
        "C:\Temp\" & Forms![frmJobReports]!lstReports.Column(1) & ".pdf", True
End Select

DoCmd.Close acForm, Me.Name
End Sub
This now works.

Just a thought, as the doc is called from the command cmdPreviewAsPDF, can the Word doc be opened in Acrobat pdf file type as I do with the Access report?
Cheers ...

Dave.

User avatar
HansV
Administrator
Posts: 78586
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Open Report or Word Doc

Post by HansV »

Adobe Reader cannot open Word documents.
You could use Adobe Acrobat or similar to create a .pdf file from the Word document, but is it worth the trouble?
Best wishes,
Hans

D Willett
SilverLounger
Posts: 1728
Joined: 25 Jan 2010, 08:34
Location: Stoke on Trent - Staffordshire - England

Re: Open Report or Word Doc

Post by D Willett »

Hmm, just thinking if a user only has Access Runtime and no Word installed.. just forward thinking. For now it's ok as it is.
Glad my code worked though, thanks for the little prompt on how to fix it :-)

Regards
Cheers ...

Dave.

User avatar
HansV
Administrator
Posts: 78586
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Open Report or Word Doc

Post by HansV »

If the user doesn't have Word, where does the Word document come from?
Best wishes,
Hans

D Willett
SilverLounger
Posts: 1728
Joined: 25 Jan 2010, 08:34
Location: Stoke on Trent - Staffordshire - England

Re: Open Report or Word Doc

Post by D Willett »

Its a master document which I will deploy to each user when I let them have access to the database. As all the users are our staff here there will never be a time when a user doesn't have Word installed as its part of the setup here. I was just thinking out of the box thats all..

Thanks Hans
Cheers ...

Dave.

User avatar
HansV
Administrator
Posts: 78586
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Open Report or Word Doc

Post by HansV »

If you ever need it, you could convert the master document to PDF (you'd have to do it only once, on your own computer) and deploy the PDF file instead of the Word document.
Best wishes,
Hans

D Willett
SilverLounger
Posts: 1728
Joined: 25 Jan 2010, 08:34
Location: Stoke on Trent - Staffordshire - England

Re: Open Report or Word Doc

Post by D Willett »

Yep, I have that option too.. I'll ask my users if they have a preference. I don't think they fill the form( doc ) electronically, its printed and filled in with a good old fashioned pen :-)
Cheers ...

Dave.

BenCasey
4StarLounger
Posts: 495
Joined: 13 Sep 2013, 07:56

Re: Open Report or Word Doc

Post by BenCasey »

You could also try something like this (error handling removed):
Private Sub ButDirect_Click()
' open the letter if it is there
Dim objWord As Object, strTemplate As String
If Nz(Me!LetterName, "") <> "" Then
strTemplate = Me!FullPath
DoCmd.Hourglass True
If Dir(Me!FullPath) <> "" Then
Set objWord = CreateObject("Word.Application")
objWord.Documents.Open strTemplate
'objWord.Documents.Add Template:=strTemplate, NewTemplate:=False
DoCmd.Hourglass False
objWord.Visible = True
objWord.Activate
End If
Else
MsgBox "There is no DIRECT Letter available. Either it is missing or it is a GROUP Mailing you clicked on."
End If
exithere:
DoCmd.Hourglass False
Set objWord = Nothing ' Release reference to the application and spreadsheet.
Exit Sub
End Sub
Regards, Ben

"Science is the belief in the ignorance of the experts."
- Richard Feynman

D Willett
SilverLounger
Posts: 1728
Joined: 25 Jan 2010, 08:34
Location: Stoke on Trent - Staffordshire - England

Re: Open Report or Word Doc

Post by D Willett »

Cheers Ben. I could probably use this within another project.
Kind Regards
Cheers ...

Dave.