Juan, I wanted to open a .doc file (my mistake)

J

Just4fun

Hi All,

hope one of you can help me.

I would like to open a file by pushing a button.
Oke..I know how to.
for instance:
Workbooks.Open Filename:= _
"C:\My documents\dowcument.doc"


But it's a little more difficult.

say I ask for a 4 digits numeric input in a cell.


as an example: 1300 is entered.


Now, by (double)clicking the cell or
selecting the cell and pressing a button.
I would like to open this file:

"C:\My documents\1300_document.doc"


This was the example i received from Juan:



MyFile = "C:\Documents and Settings\TG\Mijn Documenten\" &
ActiveCell.Value & "_document.xls"

Application.Workbooks.Open MyFile





But this opens a .xls not a .doc
What do I have to change to make it open a .doc file?


Your help would do me a great favor.

Thanks in advance, Theo.
 
D

Don Guillett

Right click sheet tab>view code>insert this.Type in name of file to goto in
a cell and double click on the cell. If open, file will be activated. If
not, it will be opened. Modify to suit.

Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As
Boolean)
Application.DisplayAlerts = False
If ActiveCell.Value = "" Then Exit Sub
workbookname = ActiveCell.Value
On Error GoTo OpenWorkbook
Windows(workbookname & ".xls").Activate
Exit Sub
OpenWorkbook:
Workbooks.Open(workbookname & ".xls").RunAutoMacros xlAutoOpen
end sub
 
D

Don Guillett

I just noticed you wanted WORD. Try one of these ideas

Sub openxmas()
Dim appWD As Object
Dim wdDoc As Document
Set appWD = CreateObject("Word.Application")
appWD.Visible = True
appWD.ChangeFileOpenDirectory ActiveWorkbook.Path
appWD.Documents.Open Filename:="XmasListEnvelopes.doc"
Set wdDoc = appWD.ActiveDocument
End Sub

Sub OpenDocument() 'NOT used
Dim wdApp As Word.Application
strFilename = "XmasListEnvelopes1.doc"
Set wdApp = CreateObject("Word.Application")
With wdApp
.Documents.Open Filename:=ThisWorkbook.Path & "\" & strFilename
.Visible = True
End With
Set wdApp = Nothing
End Sub
 
J

Juan Sanchez

Don

Sorry to step in, now I'm interested two...
I've tried both exaples but an error ocurrs, it says:

Compile Error
User-Defined Type not Defined
[ok] [Help]

Any ideas....

Juan
 
J

Juan Sanchez

I just tryed changing the definition type for
wdApp from word.application to object and it works great...

Thanks Don

Theo, I think you can figure it out from here don't you...

Don's code works (the second one) changing

dim wdapp as Word.Application to
dim wdapp as object

Cheers
Juan
-----Original Message-----
Don

Sorry to step in, now I'm interested two...
I've tried both exaples but an error ocurrs, it says:

Compile Error
User-Defined Type not Defined
[ok] [Help]

Any ideas....

Juan

-----Original Message-----
I just noticed you wanted WORD. Try one of these ideas

Sub openxmas()
Dim appWD As Object
Dim wdDoc As Document
Set appWD = CreateObject("Word.Application")
appWD.Visible = True
appWD.ChangeFileOpenDirectory ActiveWorkbook.Path
appWD.Documents.Open Filename:="XmasListEnvelopes.doc"
Set wdDoc = appWD.ActiveDocument
End Sub

Sub OpenDocument() 'NOT used
Dim wdApp As Word.Application
strFilename = "XmasListEnvelopes1.doc"
Set wdApp = CreateObject("Word.Application")
With wdApp
.Documents.Open Filename:=ThisWorkbook.Path & "\" & strFilename
.Visible = True
End With
Set wdApp = Nothing
End Sub

--
Don Guillett
SalesAid Software
(e-mail address removed)



.
.
 
J

Juan Sanchez

Don, Theo

Both codes work, just define

dim appWD as object
dim wdDoc as object

in the fisrt one and

dim wdApp as Object
in the second one...

and of course route to an existing file...;)

Cheers
Juan


-----Original Message-----
Don

Sorry to step in, now I'm interested two...
I've tried both exaples but an error ocurrs, it says:

Compile Error
User-Defined Type not Defined
[ok] [Help]

Any ideas....

Juan

-----Original Message-----
I just noticed you wanted WORD. Try one of these ideas

Sub openxmas()
Dim appWD As Object
Dim wdDoc As Document
Set appWD = CreateObject("Word.Application")
appWD.Visible = True
appWD.ChangeFileOpenDirectory ActiveWorkbook.Path
appWD.Documents.Open Filename:="XmasListEnvelopes.doc"
Set wdDoc = appWD.ActiveDocument
End Sub

Sub OpenDocument() 'NOT used
Dim wdApp As Word.Application
strFilename = "XmasListEnvelopes1.doc"
Set wdApp = CreateObject("Word.Application")
With wdApp
.Documents.Open Filename:=ThisWorkbook.Path & "\" & strFilename
.Visible = True
End With
Set wdApp = Nothing
End Sub

--
Don Guillett
SalesAid Software
(e-mail address removed)



.
.
 
H

Harlan Grove

Juan Sanchez said:
Sorry to step in, now I'm interested two...
I've tried both exaples but an error ocurrs, it says:

Compile Error
User-Defined Type not Defined
[ok] [Help]

Any ideas....
-----Original Message-----
I just noticed you wanted WORD. Try one of these ideas

Sub openxmas()
Dim appWD As Object
Dim wdDoc As Document
....

It's 'Document'. In *Excel* VBA, ain't no Document class. In Excel VBA
projects, you need to declare wdDoc as Object.
 
J

Just4fun

Guys....

you realy made a guy happy.

Don...Thanks a lot.

Juan..thanks again.



I had to work the macro a little, bit now it works !!!
(and being the noob I am, that was more luck than skills ;)



Dim wdapp As Object
strFilename = "_document.doc"
Set wdapp = CreateObject("Word.Application")
With wdapp
.Documents.Open Filename:="C:\my documents\" & ActiveCell.Value &
strFilename
.Visible = True
End With
Set wdapp = Nothing
End Sub



But it works....!!!

When nearby drop in for a beer or Lemonade.



Greets,Theo. (Holland, Helmond)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top