Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations dencom on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Code to OPEN a word Doc

Status
Not open for further replies.

darinmc

Technical User
Feb 27, 2005
171
GB
Hi
I'm not sure if this is the right forum.
The code below is in a command button which opens a word doc to a specific employees registration Number.
This works fine (if the document already exists) BUT how would i be able (check that a doc exists 1st) to open a NEW Word document if 1 does not exist? (I would assume an if statement would be required?)

It would then be better to go 1 step further and create a new document if required with the name = EmpregNbr.

Hope you are able to help
Thx
Darin


Code:
Private Sub cmdOpenWord_Click()
Dim stAppName As String
Dim stPathA As String
Dim stPathB As String
Dim stPathC As String
Dim stCombinedPath As String
Dim EmpRegNbr As Integer

EmpRegNbr = Me.EmpRegNo

stPathA = SysCmd(acSysCmdAccessDir)
stAppName = "WINWORD.EXE "
stPathB = "\\Darin\LynxDatabase\EmpWord\" '"c:\1\"
stPathC = ".doc"
stCombinedPath = stPathA & stAppName & stPathB & EmpRegNbr & stPathC
Call Shell(stCombinedPath, 1)

End Sub
 
If, instead of shelling it out you use Word, you can do something on these lines ..
Code:
Private Sub cmdOpenWord_Click()

Dim stPathB As String
Dim stPathC As String
Dim EmpRegNbr As Integer

[blue]Dim appword As Word.Application
Dim docword As Word.Document[/blue]

EmpRegNbr = Me.EmpRegNo

stPathB = "\\Darin\LynxDatabase\EmpWord\" '"c:\1\"
stPathC = ".doc"

[blue]Set appword = CreateObject("Word.Application")
appword.Visible = True
On Error Resume Next
Set docword = appword.Documents.Open(stPathB & EmpRegNbr & stPathC)
If Err.Number = 5174 Then [green]' Document not found[/green]
    On Error GoTo 0
    Set docword = appword.Documents.Add
    docword.SaveAs stPathB & EmpRegNbr & stPathC
Else
    [green]' Deal with 'real' error[/green]
End If[/blue]


End Sub

Enjoy,
Tony

------------------------------------------------------------------------------------
We want to help you; help us to do it by reading this: Before you ask a question.

Professional Office Developers Association
 
Thx Tony

I am ending up with a compile error:
user-defined type not defined

Is there something else i need to do? I'm no expert lol

Thx
 
You may try this (late binding):
Code:
Private Sub cmdOpenWord_Click()
Dim stPathB As String
Dim stPathC As String
Dim EmpRegNbr As Integer
Dim appword As Object
Dim docword As Object

EmpRegNbr = Me.EmpRegNo

stPathB = "\\Darin\LynxDatabase\EmpWord\" '"c:\1\"
stPathC = ".doc"

Set appword = CreateObject("Word.Application")
appword.Visible = True
If Dir(stPathB & EmpRegNbr & stPathC) <> "" Then
  Set docword = appword.Documents.Open(stPathB & EmpRegNbr & stPathC)
Else
  Set docword = appword.Documents.Add
  docword.SaveAs stPathB & EmpRegNbr & stPathC
End If
Set docword = Nothing
Set appword = Nothing
End Sub

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Hi Darin,

You can either use late binding as PHV says, or you will need a Reference ((VBE Menu >) Tools > References...) to the Word library (Microsoft Word nn.0 Object Library - depending on version)

Enjoy,
Tony

------------------------------------------------------------------------------------
We want to help you; help us to do it by reading this: Before you ask a question.

Professional Office Developers Association
 
Thx, it worked very well..
Could you please tell me how I could put a heading in the new Document created
ie: the persons Name and Surname?

me.EmpForename
me.EmpSurname (Would be nice if it would create it as a heading in Bold and say a font size of 16)

Also to set focus To the document (so that the user doesnt have to enter these details themselves) then go to a new line without bold and font size 12 again..

Thx Guys
Darin
 
Well, now would be a good time to learn about Styles and Templates. This will simply put out your data in Heading 1 style ...
Code:
    With ActiveDocument.Range
        .Style = ActiveDocument.Styles("Heading 1")
        .InsertAfter Text:=me.EmpForename & " " & me.EmpSurname 
        .InsertParagraphAfter
    End With

Enjoy,
Tony

------------------------------------------------------------------------------------
We want to help you; help us to do it by reading this: Before you ask a question.

Professional Office Developers Association
 
Thx Tony
This is where u see i'm a beginner..
Do I put this code in this part?

Code:
If Dir(stPathB & EmpRegNbr & stPathC) <> "" Then
  Set docword = appword.Documents.Open(stPathB & EmpRegNbr & stPathC)
Else

  Set docword = appword.Documents.Add
  docword.SaveAs stPathB & EmpRegNbr & stPathC
      
      With ActiveDocument.Range
        .Style = ActiveDocument.Styles("Heading 1")
        .InsertAfter Text:=Me.EmpForename & " " & Me.EmpSurname
        .InsertParagraphAfter
        End With
End If

AM I supposed to create a Dim part??

Thx
 
With [!]docword[/!].Range

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
me.EmpForename
me.EmpSurname (Would be nice if it would create it as a heading in Bold and say a font size of 16)

Also to set focus To the document (so that the user doesnt have to enter these details themselves) then go to a new line without bold and font size 12 again..
"Would be nice"....

As Tony mentioned, this would be a good time to learn about Styles and Templates.

If this was using a template, AND using your own defined Styles, AND using late-binding, you could do:
Code:
  Set docword = appword.Documents.Add Template:="C:\blahblah\Templates\ThisTemplate.dot"
With docword.Range
  .Style = "MyHeading1"
  .Text = Me.EmpForename & " " & Me.EmpSurname
  .InsertParagraphAfter
End With
You could make the created Style "Myheading1" whatever format you want (say Bold, 16 pts, 18 pts SpaceAfter), AND have the Style of following paragraph set to a created Style (say MyBodyText) that is...whatever format you want.
The code above would then:

1. add a new document
2. attach a template to that document
3. insert your text (formatted as style MyHeading1)
4. insert a paragraph mark which by the definition in MyHeading1 changes the now current paragraph style to MyBodyText.

If you want to put the Selection (the cursor) at this line, you will need to move it.
Code:
docword.Selection.EndKey Unit:= 6
The use of "6" comes from the use of late-binding. If you were using early-binding, you could use wdStory.

Gerry
My paintings and sculpture
 
Thx every1
Will look into styles and templates...
Any suggestions which is the best place to look, where its quite easy to understand the code and explanations?

thx
Darin
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top