index > Visual Basic for Applications (VBA) > Open a Word Document

Open a Word Document


Hello i need to find out how to open a word document from my access database any one know a good solution.

i have an ole object and you can drag and drop the file onto it but i need a way to have it load up in Word so it can be read/written/printed via word 2003.




Student on Computer Science Degree Placement
SD_Stu

There's a better way to open a word document using the Windows API.

Copy this code to the top of your form/module under Option Compare Database...

Private Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" _
(ByVal hwnd As Long, ByVal lpOperation As String, ByVal lpFile As String, ByVal lpParameters As String, _
ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long

Copy this as a function in your form/module....

Private Sub OpenFile(File As String)
On Error GoTo cmdOpenFile_Err
'open the document in whatever application it requires
ShellExecute 0, "open", File, vbNullString, vbNullString, vbNormalFocus
Exit Sub
cmdOpenFile_Err:
MsgBox "An unexpected error has occurred.", vbOKOnly + vbCritical, "Error Conditon in Opening File"
End Sub

Then call....

OpenFile("C:\MyDocuments\MyWordDoc.doc")

This will cause word to open and the file to be displayed just as though someone double clicked the file. You don't need to know where word is stored as basically your asking windows to open the file for you.

This will work for images, pdf, any document at all.

They will all open in their default application.




www.dsmyth.net/blog
Derek Smyth

The following code actually opens a Word document in Microsoft Office Word 2003 from a ASP.NET 2005 /Framework 2.0 Webform.

Randy R Jackson

You can use Word objects to open a word document

Make sure that you include the Word 9.0 Object Library and then add the following code

Dim oWd as Word.application

Dim oDoc as Word.Document

Set oWd= New Word.application

Set oDoc = oWd.open(C:\MyDoc.doc")

'--- Now you get all the Word objects and you can print and play with them

' -- Finally destroy your objects

Set oDoc = nothing

Set oWd = nothing

Shasur




http://www.vbadud.blogspot.com
Shasur

Thanks all both solutions seem to be perfect i have experience using the open file command though so i'll most likely end up using that 1.

thanks again for the help




Student on Computer Science Degree Placement
SD_Stu
reply 5

You can use google to search for other answers

 

More Articles

• Updating Hyperlink Addresses in VBA (MS Access)
• vb in excel - on data modified?
• VBA code to search and modify in Word
• .onAction assign value
• Sorting Subreport
• Programmatically inserted inline image is dithered in Word 2003
• Runtime Error '1004': Copy of method Worksheet Class Failed
• VBA excel - problem with having clause in sql with adodb.connecti...
• Novice VBA user trying to consolidate info from mult wrkshts and ...
• is weekday
Welcome to Bokebb   New Update   Joins the collection  
 

New Articles

• Accessing Tables in Access 2000 that are
• Absolute Links in Excel
• ADO Connection in Ms Access
• GroupBox Borders
• Access 2003: Connect to Word Document.do
• I am searching a VBS scripting example f
• Runtime err 1004 when attempting to set
• How do I create and use 3D ranges in Exc
• VBA WORKSHEET
• Emailing from an Access form.
• File open dialog in Outlook 2003
• DLL/OCX Redirection from Word template
• Collection Persistence
• Extracting email addresses (from column)
• winHTTP access to secure ASP site?

Hot Articles

• last Modified date in Excel spreadsheet
• Copy...Paste...by Macro!
• PowerPoint, WORD, and Macros
• Need to copy Paragraphs from other docum
• Selecting Data By....
• Problem in Saving Excel 2003 Workbook in
• VBA Editor Crashed
• Ms Access form creation
• File Protection from Users
• Just curious (an Excel VBA question)
• format numbers in html
• (0x800A03EC): Paste method of Worksheet
• ADO Recordset CursorType Property
• ActiveDocument.FollowHyperlink acts funny
• Excel Unprotect WorkSheet problem in Exc

Recommend Articles

• Application Hosted Scripting IDE in .NET
• Change Data Source in Word Document
• How do I write this!!
• Excessive Lag with VBA APP
• Accessing Active Directory trough Access
• DDE connection
• User Input on Range Selection
• Adding a table column using VBA in Access
• Generating a Printable Document based on
• automatical start macro in access2003
• How do I create and use 3D ranges in Exc
• Getting New Textbox value without using
• Applying InsertBreak to a style?
• Does the VBA save word as XML???
• macro script?