Computing.Net > Forums > Office Software > Excel VB code needs tweaking please

Excel VB code needs tweaking please

Reply to Message Icon

Original Message
Name: aparmar
Date: September 24, 2007 at 01:33:33 Pacific
Subject: Excel VB code needs tweaking please
OS: xp
CPU/Ram: na
Model/Manufacturer: na
Comment:

Hi,

I need some help tweaking this VB code so that I can open up PDF files instead of the word documents once activated using a macro.

Heres what I've done so far: -

Sub openWordDoc(q)
Dim app As Object
Set app = CreateObject("Word.Application")
app.Visible = True
With app.Documents.Open("U:\Amit\U:\Amit\Precedents\q")
.GoTo(wdGoToPage, wdGoToAbsolute, 3).Select
End Sub

Any help would be great to change the word stuff into Adobe PDF.

Thanks
Amit


Report Offensive Message For Removal


Response Number 1
Name: DerbyDad03
Date: September 24, 2007 at 09:16:37 Pacific
Subject: Excel VB code needs tweaking please
Reply: (edit)

Don't ask me to explain the code below...I just know that it works.

The original can be found at http://xcelfiles.homestead.com/Exce...

Any comment lines that start with 2 single quotes ('') are lines that I either added, modified or commented out to make the code work with PDF documents. Please refer to the original code to see the differences.

Option Explicit

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

Private Declare Function SendMessage Lib "user32" _
Alias "SendMessageA" ( _
ByVal hwnd As Long, _
ByVal wMsg As Long, _
ByVal wParam As Long, _
lParam As Any) As Long

Const SW_SHOWNORMAL = 1
Const SE_ERR_NOASSOC = 31
Const ERROR_FILE_NOT_FOUND = 2&
Const WM_CLOSE = &H10

Sub OpenAnyFile()
Dim strVisio As String
''I added the next line
Dim strPDF As String
Dim strChm As String
Dim strMail As String
Dim lngRet As Long
Dim Msg As String

'// //
'// Example strings //
'// //

'// Open a Visio File
''strVisio = "C:\VisioFiles\WIP\MyDrawing.VSD"

'// Open a chm Help file
''strChm = "C:\WINDOWS\HELP\msoe.chm"

'// Open up default Mailer
''strMail = "mailto:ivanmoala@xtra.co.nz"
'

'// Note: You don't have to specify the "open" command
'// by default Shell will default to open if you use
'// vbNullString, "Open" is just used for clarity.

'' I added the next line and replaced strChm with strPDF in the "lngRet ="section

strPDF = "C:\Documents and Settings\My_Account\Desktop\MFS.pdf"

lngRet = ShellExecute( _
0, _
"Open", _
strPDF & vbNullString, _
vbNullString, _
vbNullString, _
SW_SHOWNORMAL)

Select Case lngRet
Case SE_ERR_NOASSOC
Msg = " is NOT Associated with any Application!"
Case ERROR_FILE_NOT_FOUND
Msg = " could NOT be found!"
End Select

'' I commented out the next line...don't know why the author wants to display the lngRet value.
''MsgBox lngRet
SendMessage lngRet, WM_CLOSE, 0, 0

If Len(Msg) = 0 Then Exit Sub

MsgBox strVisio & Msg


End Sub



Report Offensive Follow Up For Removal







Use following form to reply to current message:

   Name: From My Computing.Net Settings
 E-Mail: From My Computing.Net Settings

Subject: Excel VB code needs tweaking please

Comments:

 


  Homepage URL (*): 
Homepage Title (*): 
         Image URL: 
 
Data Recovery Software




How often do you use Computing.Net?

Every Day
Once a Week
Once a Month
This Is My First Time!


View Results

Poll Finishes In 3 Days.
Discuss in The Lounge