How to display the text file while clicking the button - vb6

How to display the file(*.txt) while clicking the command button
How to display the content of the file while clicking the button
Data's are stored in the text file, ex 1.txt
when am clicking the command buttion, 1.txt file will open and 1.txt data's should display

Add a textbox to a form, make it multiline=true, add a button to the form.
And in the buttons click handler add this:
Private Sub Button1_Click()
Dim iFile As Long
Dim strFilename As String
Dim strTheData as String
strFilename = "C:\1.txt"
iFile = FreeFile
Open strFilename For Input As #iFile
strTheData = StrConv(InputB(LOF(iFile), iFile), vbUnicode)
Close #iFile
text1.text=strThedata
End Sub
This will read the text in the file and add it to the textbox.
Edit: Changed the line that reading the content to be more robust as pointed out by MarkJ in this answer (Cred goes to to MarkJ to pointing out that.)

Stefan's answer contains a flaw: the code to read a text file into a string isn't very robust. It's a very common mistake - the same flawed code is on some excellent VB6 web sites. His code is
Open strFilename For Input As #iFile
strTheData = Input$(LOF(iFile), #iFile)
Close #iFile
Unfortunately this throws an error 62 "input past end of file" if the text file contains ASCII zero characters. Also it doesn't work in all countries (it throws an error for most strings in double byte character sets like Chinese or Japanese).
Perhaps those problems are a bit obscure: but there's better code to do this job in the VB6 manual (here), it's also three lines, and it never fails.
Open strFilename For Input As #iFile
strTheData = StrConv(InputB(LOF(iFile), iFile), vbUnicode)
Close #iFile
It looks more complicated: but actually the only difference is that the conversion from ANSI to Unicode is explicit rather than implicit. It runs just as fast, and it always works.

No offence intended, but it sounds like you need a beginners tutorial on VB6.
(I think this because you don't seem to be able to articulate exactly what you need help with, possibly because you don't know enough about what you're trying to do).
Googling for VB6 Tutorial will give lots of links, this one looks good
Hope this helps, and apologies if I'm wrong :)

To just open a file using the current default file handler try using the ShellExecute API function.
Here's an example.

Related

how to show a dialog windows during applescript works

I wrote an applescript to convert a to b,
I want to show a dialog when script is working,
script first ask for a file and a folder from user, after this I want to show a dialog until my last dialog which say work is done.
I couldn’t find any help here or other sites regarding this way of implementing dialogs, thanks for you help.
to be clear, there is no way of having an input for steps, because it's doing a convert on one file only, but the file could be large & take some time, I'm looking for something that can input for example line of codes & progress depend on which line it's working right now or something else which start with the first line & close just before the end dialog

Text kerning in Microsoft rich text box

This one has me awfully confused...
I am trying to display kerned RTF text in a Visual Studio Visual Basic RichTextBox control (having so far tried under VS2010 and VS2012). Simply, I create a Windows Form project, add two RichTextBox's (RichTextBox1 and RichTextBox2) to the form, no change to default properties, and include the following VB code:
Public Class Form1
Private Sub Initialise(sender As System.Object, e As System.EventArgs) Handles Me.Load
Dim txtRTF As String = "{\rtf1\ansi" & _
"{\fonttbl{\f0\froman\fprq2\fcharset0 Times New Roman;}}" & _
"\f0\pard" & _
"\expndtw-60 a" & _
"\expndtw200 b" & _
"\expndtw-20 c}"
RichTextBox1.Rtf = txtRTF
RichTextBox2.Paste() ' RichTextBox2 formats properly iff clipboard holds ANY valid rtf content
RichTextBox2.Rtf = txtRTF
End Sub
End Class
The txtRTF String contains, as best as I can tell, minimal valid RTF markup and text.
Here's the confusing bit: text displayed in RichTextBox1 is not kerned, despite \expndtw (expand twips) RTF markup, BUT text displayed in RichTextBox2 is properly kerned, if and only if the clipboard holds ANY valid RTF content (e.g., any text has first been copied into the clipboard from an MS Word document). Text displayed in RichTextBox2 is not properly kerned if clipboard contents is not RTF format.
Result of running if the clipboard does not contain RTF-formatted data (or if the RichTextBox2.Paste() code is removed or commented out):
Result of running if the clipboard holds any random RTF-formatted text:
Questions:
Why on earth should it matter that I have previously pasted RTF-format (and not non-RTF format) into the RichTextBox2 control before setting the RichTextBox2.Rtf field?
More importantly, how can I (in VB) programmatically display properly kerned text in a RichTextBox control without the absurdity of first pasting random RTF format text into it?
Well, that took a lot of work! Nevertheless, problem now solved.
It turns out that, although the RTF spec notes that \ltrch (left-to-right character run) is the default state, it seems that RichTextBox objects don't necessarily agree. Including a \ltrch (or even, oddly, a \rtlch) control sequence in the RTF markup stream completely solves the failure-to-kern problem. RTF text kerning via \expndtwN and \expndN now works perfectly well. No need for silly Paste() commands to pre-configure the RichTextBox control into its proper state!

Print File(NotePad) Without the name of the file appearing in the printing document

Dim ProcessProperties As New ProcessStartInfo()
ProcessProperties.FileName = "notepad.exe"
ProcessProperties.Arguments = "/p c:/doc.txt" 'command line arguments ''
''ProcessProperties.WindowStyle = ProcessWindowStyle.Maximized
Dim myProcess As Process = Process.Start(ProcessProperties)
when printing using the code above i have "doc.txt" printed. I don't want the file name to appear when printing is over
It is not possible to pass an argument through command line to notepad to hide the header, the only parameter accepted for the /P argument is the file name. (See link)
The only way to hide the header from printing is by opening notepad and going to File->Page Setup, and changing the header command. (See link)
If it is really needed to print using a process, you can try searching for third party editing tools which support printing arguments.
Another way is to implement functionality for printing the text file, see this Stackoverflow article (Link)
You will have to configure the header and footer options of Notepad in order to prevent or override the file name appearing on the print page. This can be done by simulating user input to bring up the Page Setup dialog and clear the header and footer.
There are a number of ways to send user input (mouse and/or keyboard). For starters you could look at these two:
Windows Input Simulator
Application and Global Mouse and Keyboard Hooks .Net Libary in C#

Margins Incorrect in Word Document after pasting from one document to another using VB6

I have inherited a VB6 app and I could do with some help with part of it.
The code opens a word document and copies its contents. Once this is complete it will open another document and paste the contents from the first document into the second. The opening, copying and pasting works ok, the issue comes with the formatting of the pasted text and the section break it follows. Instead of appearing straight after the section break it is being put on another page, the section break does however still say it is continuous. I've done some digging and tried what it says in the following
Stop Margin Adjustment when pasting - Microsoft Community
Problems with margins when I copy and paste a document into template - Microsoft Community
Section break causes unexpected page break in word
Troubleshoot page breaks and section breaks - Word - Office.com
None of these have helped. A cut down version of the code is as follows:
GetWord97Object objWordApp
objWordApp.Visible = True
objWordApp.documents.Open strCopyFromDoc
DeleteHeadersAndFooters objWordApp.documents(strCopyFromDoc)
objWordApp.documents(strCompyFromDoc).content.Copy
objWordApp.documents.Open strCopyToDoc
objWordApp.documents(strCopyToDoc).characters(objWordApp.ActiveDocument.characters.Count).Select
Set objRng = objWordApp.ActiveDocument.content ' Range used so as not to overwrite original text
objRng.Collapse Direction:=0
If IsWordAppVersionLessThan2002(CInt(objWordApp.Version)) Then
objRng.Paste
Else
objRng.PasteAndFormat wdPasteDefault
End If
I've tried the paste and format but that hasn't helped.
The version of Word I am using is 2002 SP3 but I need it to work with 2002 and up. The VB6 is at SP6.
Thanks in advance for your help.
I've managed to get rid of the problem. It looks like it was something to do with the document, rather than the code. I've given copying the header and footer from one document to another and that seems to have worked this time. Previous attempts at copying didn't seem to make any difference. Not an ideal solution but at least it is sorted.
I've found the solution and It's more simple that I thought. Just save the document before you paste your content. That makes Word to keep the original margins definitions. In my code I did that.
Private Sub CommandButton4_Click()
Dim Item As String
Dim i As Integer
For i = 0 To ProcList.ListCount - 1
Dim docNew As Document
Dim docproc As Document
Set docNew = Word.ActiveDocument
docNew.Content.Copy
Set docproc = Documents.Add
With docproc
.SaveAs FileName:=ProcList.List(i)
Selection.ClearParagraphAllFormatting
Selection.Paste
End With
Next i"

Copy Selected Text with VBA and the Adpbe PDF Reader Control

I am trying to copy selected text directly out of a PDF viewer I have made with the Adobe PDF Reader Control in Visual Basic 2010.
I can utilize highlight text with a macro in Word using something this:
Private Sub CommandButton1_Click()
Dim Sel As Selection
Set Sel = Application.Selection
If Sel.Type <> wdSelectionIP Then
MsgBox Sel.Text
End If
End Sub
I am having trouble figuring out whether this bit of code can be used to perform actions with the highlighted text with Adobe PDF Reader. If not, does anyone know how I would go about doing that?
I don't think that the Adobe PDF viewer supports VBA.
However, I would suggest an indirect approach, using the clipboard.
By sending a control+c key, the content of the selection will be transferred to the clipboard, and the clipboard could be processed using "native" VBA.
Sending a control+c is done by using SendKeys("^C"), see This link
And the Clipboard can be manipulated using This exampe

Resources