Jump to content


 


Register a free account to unlock additional features at BleepingComputer.com
Welcome to BleepingComputer, a free community where people like yourself come together to discuss and learn how to use their computers. Using the site is easy and fun. As a guest, you can browse and view the various discussions in the forums, but can not create a new topic or reply to an existing one unless you are logged in. Other benefits of registering an account are subscribing to topics and forums, creating a blog, and having no ads shown anywhere on the site.


Click here to Register a free account now! or read our Welcome Guide to learn how to use this site.

Photo

Visual Basic 6


  • Please log in to reply
8 replies to this topic

#1 heltune

heltune

  • Members
  • 59 posts
  • OFFLINE
  •  
  • Local time:07:07 PM

Posted 04 September 2008 - 01:16 PM

please help anyone..
im making a simple project and i have a problem with opening word documents using filelistbox. actually i was able to make a workable command button that opens existing word document but the problem is when i open a document with a filename that has spaces like " payroll and pay wages" like that with spaces in between words there is a prompt as microsoft office document opens up that the file that i am trying to open doesn't exist. but in glorious files such as "payroll.doc" which has no space since its only one word i can 100 percent opens it as in... but when it comes to other files with spaces and some charcters in between i am really sorry but i cant open it leading to a missing file... FYI with my search over the web about visual basic.. i maybe worng but i thinks it has got to do with the strings.. am i right or wrong... please help me please... im stucked!!! everything else work but only to some filenames that have no spaces in between... how can i do that please correct me ... thank you and god bless....

Edited by heltune, 04 September 2008 - 01:18 PM.


BC AdBot (Login to Remove)

 


#2 Billy O'Neal

Billy O'Neal

    Visual C++ STL Maintainer


  • Malware Response Team
  • 12,304 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Redmond, Washington
  • Local time:04:07 PM

Posted 04 September 2008 - 03:50 PM

How are you actually opening the documents?

Are you simply passing a system call? Such as
Winword %file%

If so, quote the file:
winword "%file%"

Billy3

EDIT: Forgot a %

Edited by Billy O'Neal, 04 September 2008 - 03:50 PM.

Twitter - My statements do not establish the official position of Microsoft Corporation, and are my own personal opinion. (But you already knew that, right?)
Posted Image

#3 heltune

heltune
  • Topic Starter

  • Members
  • 59 posts
  • OFFLINE
  •  
  • Local time:07:07 PM

Posted 04 September 2008 - 11:35 PM

whats a quote? sori i don't know that. anyway i would want that i can open any file from my "my documents" folder that is any microsoft document 2007 files... i can really open files with filenames that has no spaces or a dash ( - ) in the filename. but when it comes to filenamkes with spaces or dashes... it prompts in the winword application a missing file etc.

for a clearer view here is my code::

i added this code on my command button start_click()

Private Sub Start_Click()
Dim FileNam As String, ProgNam As String

If File1.FileName = "" Then Exit Sub
FileNam = File1.Path

'If path doesn't end with a \ we have to
'complete the path + filename

If Right(FileNam, 1) = "\" Then
FileNam = FileNam + File1.FileName
Else
FileNam = FileNam + "\" + File1.FileName

End If

'If filename is an .exe, run the program
'otherwise, call Wordpad or IExplorer to open the file

Select Case Extension.ListIndex
Case 0
ProgNam = "D:\Program Files\Microsoft Office\OFFICE11\WINWORD.EXE"
Response = Shell(ProgNam + " " + FileNam, vbNormalFocus)
Case 1
ProgNam = "D:\Program Files\Microsoft Office\Office11\EXCEL.EXE"
Response = Shell(ProgNam + " " + FileNam, vbNormalFocus)
Case 2
ProgNam = "D:\Program Files\Microsoft Office\Office11\POWERPNT.EXE"
Response = Shell(ProgNam + " " + FileNam, vbNormalFocus)
End Select
End Sub

for additional info on my project:

Private Sub Dir1_Change()
File1.FileName = Dir1.Path
End Sub
Private Sub file1_Click()
FileNameBox.Text = File1.FileName
End Sub

Private Sub file1_DblClick()
Start_Click
End Sub

'Populate the list of searchable extensions at Load time

Private Sub Form_Load()
' Initialize the DriveListBox to point to the C: drive ...
Drive1.Drive = "D:\Documents\Sanggunian\SPAgenda"

' Likewise for the "mstrDrive" variable ...
mstrDrive = "D:\Documents\Sanggunian\SPAgenda"

' Set the path of the DirListBox to point to the root of C:
Dir1.Path = "D:\Documents\Sanggunian\SPAgenda"

Extension.AddItem "Word Document (.doc, .docx)"
Extension.AddItem "Excel Document (.xls, .xlsx)"
Extension.AddItem "PowerPoint Document (.ppt, .pptx)"
Extension.ListIndex = 0
Extension_Click
End Sub
Private Sub Drive1_Change()
'-----------------------------------------------------------------------------
Dir1.Path = Drive1.Drive
' An error would occur if you attempted to change to a CD or floppy drive
' where the media was not ready ...revisions on differentt cp config

On Error Resume Next

' When the drive changes, set the Path of the DirListBox to point to the
' the newly selected drive ...
If Err.Number <> 0 Then
' There was a problem with the drive - most likely a CD or floppy drive was
' selected, but there was no CD or disk in the drive.
' Inform the user ...
MsgBox "Drive selected is unavailable.", vbInformation, "Drive Unavailable"
' Reset the Drive property of the DriveListBox to the last valid drive ...
Drive1.Drive = mstrDrive
Else
' Drive change was OK, update the "mstrDrive" variable with the newly
' selected drive ...
mstrDrive = Drive1.Drive
End If
End Sub
...
where would i put that :

winword "Filex" ?

thank you for the help...please enlighten me .. god bless

#4 heltune

heltune
  • Topic Starter

  • Members
  • 59 posts
  • OFFLINE
  •  
  • Local time:07:07 PM

Posted 05 September 2008 - 02:34 AM

another thing... is there any other way that i can do for opening an existing word doc or any other document?

i found this word viewer ocx, the program is good and i would like to emulate if i cant solve the problems (hehe)... but the probs is it is a shareware.. howd i wish it was a freeware... anyway... i know that there solutions to that.. i hope u can help me with that...

one main thing is that i want the program run like that where users can click on the drive, dir, and file ... listbox and able to open the document either by double clicking on the filelistbox (which i was able to do) or by a command button (which i was able to do it). but the porbs come when my document has spaces or dashes on the filename...

can u suggest besides what i am doing other ways that i can do the opening... god bless

#5 Romeo29

Romeo29

    Learning To Bleep


  • Members
  • 3,194 posts
  • OFFLINE
  •  
  • Gender:Not Telling
  • Location:127.0.0.1
  • Local time:06:07 PM

Posted 05 September 2008 - 06:12 AM

Use ShellExecute API to open the files with attached application. Like .TXT would be automatically opened by Notepad, .DOC by MS Word etc.

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

Private Sub Command1_Click()
	Dim sFile As String
	sFile = "E:\Personal\movie titles.rtf"
	ShellExecute Me.hwnd, "open", sFile, vbNullString, vbNullString, 1
End Sub


Also the way you are trying to open .DOC files is by giving direct command line. In that case wrap the complete file path within quotes. For example ,

From C:\Windows\This Is My File.DOC
To "C:\Windows\This Is My File.DOC"


Hope it helps you out.

#6 heltune

heltune
  • Topic Starter

  • Members
  • 59 posts
  • OFFLINE
  •  
  • Local time:07:07 PM

Posted 05 September 2008 - 09:06 AM

im sori but it doesnt work... i dont know if im worng here anyway... what im trying to say is that i have created a filelistbox named file1 on the form with dirlistbox and drivelistbox to complete the connections (its like an explorer style) and of course a command button named start. i was able to use the three tools (filelistbox, dirlistbox and drivelistbox) all working together and it is fine. Now the next problem i got is that how am i going to incorporate the command button named start with a code Private Sub Start_Click()... all i want is that everytime i click the command button on the specific filename on the FILELISTBOX (which i assume that there are several word documents on that list) is that the filename which i selected on the filelistbox will automatically opens to microsoft word application so that i can edit or save the file... unfortunately only filenames (e.i agenda.doc or mypersonal.doc or heaven.ly.doc) on my filelistbox that has no spaces in between or dashes can be opened. everything else (e.i. agenda 2007.doc or maybe this time.doc etc.) can be opened but microsoft word prompts me that the file is missing and other messageboxes (path directory etc.... i dont know that sori)... i figured it out personally that only filenames with no spaces and dashes can be open smoothly with no hassle. the rest prompt me of a missing path etc. i though of it (maybe im wrong) as a string problem. it has got to do with the string ... please correct me .... thank you god bless....

i hope i will get some enlightenment


thank you po!

#7 groovicus

groovicus

  • Security Colleague
  • 9,963 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Centerville, SD
  • Local time:05:07 PM

Posted 05 September 2008 - 10:11 AM

Try wrapping the file name in quotation marks. Instead of:
my file.txt
Make it:
"my file.txt"

Otherwise Windows will look for a file simply named 'my'.

#8 Romeo29

Romeo29

    Learning To Bleep


  • Members
  • 3,194 posts
  • OFFLINE
  •  
  • Gender:Not Telling
  • Location:127.0.0.1
  • Local time:06:07 PM

Posted 05 September 2008 - 04:31 PM

Start new project. Put a command button, a filelist, drivelist and a dirlist control on the main Form. Dont change the default names of controls.

Now copy-paste the following code to the main form.

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

Private Sub Command1_Click()
	Dim sFile As String
	sFile = File1.Path
	If Right(sFile, 1) <> "\" Then sFile = sFile & "\"
	sFile = sFile & File1.List(File1.ListIndex)
	sFile = """" & sFile & """" 'Wrap in double-quotes
	'Now you can use it
	ShellExecute Me.hwnd, "open", sFile, vbNullString, vbNullString, 1
End Sub

Private Sub Dir1_Change()
	File1.Path = Dir1.Path
End Sub

Private Sub Drive1_Change()
	Dir1.Path = Drive1.Drive
End Sub

Does it work?

#9 heltune

heltune
  • Topic Starter

  • Members
  • 59 posts
  • OFFLINE
  •  
  • Local time:07:07 PM

Posted 06 September 2008 - 12:46 AM

Hello Romeo29... OMG it worked thank you!!!! you are a blessing... thank you for all of your helped to grovicoius and billy o neal also... thanks for the suggestions... God bless po!

i modified the code so that i can double click the filelistbox and be able to open it... it worked also.... thank you for the insight.... it was my mistake that i did not declare a private function... anyway... thank you all

GOD BLESS PO! SALAMAT...

Edited by heltune, 06 September 2008 - 12:48 AM.





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users