PDA

View Full Version : Opening a pdf file from word document using command button



arrun
01-23-2008, 07:41 PM
I have put two command button in my word document. My intention is : when 1st button clicked a pdf file named "c:/aa.pdf" will be opened. and 2nd command button will search (when clicked of course) whether that aa.pdf is opened or not. if open then it will close that file.

Can anyone give me any clue what will be required VBA code?

Regards,

lucas
01-23-2008, 07:58 PM
You have to have acrobat installed...of course but this works for me....it is set up to open the pdf file in the same directory as the word file you are calling it from......example attached
Sub OpenPDFdoc()
MsgBox "This will fail if you don't have Adobe Acrobat installed :o)"
On Error GoTo 1
ActiveWorkbook.FollowHyperlink ActiveWorkbook.Path & _
"\AA cover.pdf", NewWindow:=True
Exit Sub
1: MsgBox Err.Description
End Sub

fumei
01-25-2008, 11:20 AM
"ActiveWorkbook"...hmmmm, yes, that will work inside of Word.

For the first commandbutton (or whatever you use to fire it):ActiveDocument.FollowHyperlink _
Address:="c:\test\aaTest.pdf", NewWindow:=TrueAdjust for Address path requirements. If it is in the same path as the ActiveDocument, you can use code like Steve's.

As for #2 = checking to see if it is open, that is another, quite different, kettle of fish. As the PDF is not open in Word, then Word does not know about it, or can know about it. You are going to have to use some sort of API to check running processes, I think. This is not something I normally do, so I can not jump in with an immediate suggestion.

TonyJollans
01-27-2008, 05:47 AM
Be aware that there are issues with hyperlinks and Acrobat 7. I believe you need at least version 7.1.8 for this to work.

arrun
01-29-2008, 12:47 AM
Really really it is great help for me. I need two more modifications.

Actually instead of opening a pdf file now I am using it to open excel file as well. However I seek help in following:

1. Excel file (or any file like Adobe in previous case) should be opened in maximized window.
2. While opening Excel file by this way, "Web"-toolbar is popping up. I want to disable it as well.

And one more. I understand that closing Adobe file perhaps is complicated. But what about closing Excel file using button placed in word?

Your help will be highly appreciated.

Thanks and regards,

TonyJollans
01-29-2008, 06:41 AM
Hyperlinks do not allow you significant control over the target application so you cannot either determine the state of the window or the toolbars on display - and nor should you try.

A button in one app (Word) can have code that checks to see what other application windows are open. In the case of apps that expose an object model it should be possible to close them cleanly - it is probably more awkward with Acrobat or the Adobe Reader. Do realise, though, that you don't actually know, for sure, what application is invoked from a hyperlink (unless you check the registry) and you don't know if it was already open or has otherwise been put to use since opening.

arrun
02-29-2008, 12:18 PM
I need one more clarification regarding opening an excel file from word. Previously I used that :
Private Sub CommandButton3611_Click()
'ThisDocument.FollowHyperlink Address:="C:\D Drive\ss.xls", NewWindow:="True"
End Sub

in the mean time I come through a document here : http://word.mvps.org/FAQs/InterDev/ControlXLFromWord.htm

Hence I changed my code as below :

Private Sub CommandButton3611_Click()
'ThisDocument.FollowHyperlink Address:="C:\D Drive\ss.xls", NewWindow:="True"
Load WorkOnAWorkbook
End Sub

Sub WorkOnAWorkbook()

Dim oXL As Excel.Application
Dim oWB As Excel.Workbook
Dim oSheet As Excel.Worksheet
Dim oRng As Excel.Range
Dim ExcelWasNotRunning As Boolean
Dim WorkbookToWorkOn As String

.............................................

If ExcelWasNotRunning Then
oXL.Quit
End If

End Sub

However I am getting following error :

Compile error:
Expected function or variable

Can anyone tell me why I getting this error?

Regards,

Tinbendr
02-29-2008, 01:56 PM
Load WorkOnAWorkbook
I believe this should be Call WorkOnAWorkBook or simply WorkOnAWorkBook

arrun
02-29-2008, 07:06 PM
ahhh...........it's working. thank you

arrun
03-01-2008, 08:30 AM
I need a small modification. If my excel is already opened then it produces an error. Therefore I need to check whether a this particular excel file is already opened or not. Can anyone please help me how to check that? What is the VBA code for that?

Regards,

arrun
03-01-2008, 08:48 AM
opps it is already there : http://www.vbaexpress.com/kb/getarticle.php?kb_id=625

lucas
03-01-2008, 09:01 AM
"ActiveWorkbook"...hmmmm, yes, that will work inside of Word.



..........sorrry for the confusion.

fumei
03-03-2008, 04:12 PM
Ah come on Steve, you know I was razzin'.

arrun, good on you to both find your own answer, and to tell us you did. You need to possibly look up some research on instances, and early/late binding.

It is VERY important to understand, and control, instances of applications. How to create one. What the difference is between quitting one, and destroying one. When to do one, and when to do the other.

Especially if you are messing around with Word and Excel. And I have to admit that Word is worse than Excel....maybe, probably, likely...hard to say.




Never mind.

lucas
03-03-2008, 09:14 PM
Ah come on Steve, you know I was razzin'.

And I have to admit that Word is worse than Excel....maybe, probably, likely...hard to say.

Never mind.

I know you were just giving me a hard time Gerry, no problem there... It was still a bad thing though because if someone was trying to learn from my example it would have just confused them....:doh:

Word is less user friendly than excel, but it is worth learning because it is the best word processor program I have used.....in my opinion. :yes

arrun
03-09-2008, 01:19 PM
I am getting an irritating error when I use same code on different file :

Sub WorkOnAWorkbook()

'
Dim oSheet As Excel.Worksheet
Dim oRng As Excel.Range
Dim ExcelWasNotRunning As Boolean
Dim WorkbookToWorkOn As String

----------------------------------------


It is :
Compile error
user-defined type no defined

with highlight on the 1st line i.e. Dim oSheet As Excel.Worksheet

Am I missing something?

arrun
03-09-2008, 01:35 PM
One more addition.........
If I delete 1st two code, then it is working fine. Therefore should I assume that that two codes has no importance?

Can anyone please clarify me?

TonyJollans
03-09-2008, 04:17 PM
I guess that depends on what you consider important.

The two lines declare your variables, OSheet and oRng. If they are deleted, the variables will be implicitly defined - and your code may well work.

Virtually all developers - and I am no exception - recommend that you use "Option Explicit" in your code. If you did this, your code, relying on implicit declarations, would not work.

Your explicit declarations did not work because Word did not understand the Excel types you declared them as. You have two choices: you can tell Word about the Excel types, or you can tell it not to bother about what types the variables are. These two ways of codeing have names: early binding and late binding.

To use late binding - which means Word finds out about the types at run time - you define the variables to be some generic type. The most generic type is Variant, which is the default, so you could just code:

Dim oSheet
Dim oRng

A better generic type for these variables, however, would be Object:

Dim oSheet As Object
Dim oRng As Object

To use early binding you must tell Word - at compile time - what precise types the variables will be. You do this by declaring them as y ou had them - and by telling Word where to find the library which tells it what these types are. You do this by setting a Reference:

Select Tools > References from the (VBE) Menu
In the Dialogue, scroll down the list and find "Microsoft Excel vv.m Object Library", where vv.m is the version (e.g. 11.0 for Excel 2003).
Check the checkbox beside the library name..
Close the Dialogue.

The choice is yours. The primary reason for using "Option Explicit" is to avoid implicit declaration of wrongly typed names and the consequeent failure to use the correct variable. The pros and cons of early and late binding are more finely balanced and I do not intend going into detail at the moment.