How to set printer properties using vb.net - vb.net

I am using EDRAW to browse a Microsoft Word and can be Print , Preview , ETC... And I am planning to add some features to it. I am adding 2 buttons for Print Short (8.5 by 11 inches) and Print Long (8.5 by 13 inches) and I have 2 printers for long and short. How to set the properties of the printers in each button?. If I click button short it will print short using printer 1 same as the button long but it is in printer 2.
I am following the code in the Link provided above.
Anyone has an idea about it?. Any suggestion well help and well be accepted. Thanks.. Cheers.. I will give a 50 bounty to it after two days..
Code as #Hadi request
Here is my button code for print.
Private Sub btnPrint_Click(sender As System.Object, e As System.EventArgs) Handles btnPrint.Click
AxEDOffice1.SetActivePrinter("Printer Name")
AxEDOffice1.ActiveDocument.PageSetup.PaperSize = Microsoft.Office.Interop.Word.WdPaperSize.wdPaperA4
AxEDOffice1.PrintDialog()
End Sub
and getting an error Object variable or With block variable not set in the line code of AxEDOffice1.ActiveDocument.PageSetup.PaperSize = Microsoft.Office.Interop.Word.WdPaperSize.wdPaperA4

After Checking the library all you have to do is using SetActivePrinter Method to change your default printer like the following:
AxEDOffice1.SetActivePrinter("Adobe PDF")
And to change PaperSize you have to use the following
AxEDOffice1.ActiveDocument.PageSetup.PaperSize = Microsoft.Office.Interop.Word.WdPaperSize.wdPaperA4
AxEDOffice1.ActiveDocument is an instance of Microsoft.Office.Interop.Word.WordDocumentClass
Code tested it and it works fine.
EDIT 1:
Object variable or With block variable not set
Read more about it on this MSDN article there are many suggestions.
EDIT 2:
To Print your document directly without showing PrintDialog you have to use PrintOut Function.
AxEDOffice1.PrintOut(EDOfficeLib.WdPrintOutRange.wdPrintAllDocument)

It looks like Change printername in PrintDialog through code deals with making sure a print dialog box pre-selects the printer based on printer name in vb.net.

Related

How can I cause a button click to change the image in a picture box?

I'm using VS2010 for VisualBasic, and I'm working with several similar forms. What I need to have happen is for the buttonclick on each page to cycle through the My.Resource images in order: adj_01, adj_02, adj_03,... and each form will have a different three-letter prefix.
This is what I have so far:
It might not be clear, but I'm trying to have the images cycle trough one after the other with each button click. Apparently there is an issue with either my referencing, or that the images are .png format. Simultaneously, I'm trying to have 2 separate label update information with each image change. This is what I have so far with that:
EDIT I just noticed an error that might confuse everyone on the photos: The first lines starting the If statements are checking to see if the PictureBox is empty. Needless to say, I don't know how to do that.
Here you go...
Private Sub NextAdjButton_Click(sender As Object, e As EventArgs) Handles NextAdjButton.Click
If AdjectivesPictureBox.Tag Is Nothing Then
AdjectivesPictureBox.Tag = 0
End If
Dim number As Integer = CInt(AdjectivesPictureBox.Tag)
If number < 5 Then
number = number + 1
AdjectivesPictureBox.Image = My.Resources.ResourceManager.GetObject("adj_" & number.ToString("00"))
AdjectivesPictureBox.Tag = number
End If
End Sub

Howto automatically load text from txt file to vb.net

I have a small program I use to launch applications (Visual Studios), I would like to add a Label that would (AUTOMATICALLY) display a message from a text.txt file once the application is load (NO BUTTONS). I will have about 10 of these small apps so this would make it easier to update in the future.I am new to program.
If you double-click your Form window in Visual Studio, you will create an event handler function called FormName_Loaded or similar. This function will be automatically called (no buttons required) when the form finishes loading.
In this function, you can set the value of your label text. For example, if your label is called lblFileData, you can put in:
lblFileData.Text = System.IO.File.ReadAllText("path\to\file\text.txt")
This loads all text from the file and assigns the text to the label. Just make sure that the label size and max length are sufficient to show all the text in the file.
You can write this
Label1.Text = My.Computer.FileSystem.ReadAllText("C:\Test.txt")
What metacubed suggested should work. Assuming the label was added through the designer. The code behind should look like this:
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Try
Label1.Text = IO.File.ReadAllText("C:\testFile.txt")
Catch ex As Exception
HandleException(ex)
End Try
End Sub
Helpful tip, you should always use a Try/Catch block when reading from files. Otherwise if an exception occurs, your application will crash and that's not user friendly. The "HandleException()" is just a method I made up that can do certain actions when exceptions occur such as displaying the text "Unable to read from file." on the Label1.

Programming VBA in an Outlook form

I created my own Outlook form to use it as standard surface to enter certain orders instead of the normal message form. The creation, editing and sending works perfectly fine and in the next step I want to insert some code via VBA.
My problem is that I can´t access the objects of my form in the VBA editor. E.g. I want to show a message box when a certain checkbox is checked. According code would be:
Sub example()
If CheckBox1.Value = True Then
MsgBox("Checkbox 1 is checked.")
End If
End Sub
When I run the code I get the error that the object could not be found. The same goes for every other object, like textboxes or labels etc.
I guess the solution is pretty simple, like putting Item. or sth. like that in front of each object. But so far I wasn't able to find the solution.
I´m using Outlook 2010.
I know this is a year too late but you'll want to do something like this example below. It's kinda a work around but you can get whatever value was selected.
Sub ComboBox1_Click()
Set objPage = Item.GetInspector.ModifiedFormPages("Message")
Set Control = objPage.Controls("ComboBox1")
MsgBox "The value in the " & Control.Name & _
"control has changed to " & Control.Value & "."
End Sub
You should be able to get the value, just get a handle on the object you want using the Inspector
The following is an excerpt from here
When you use a custom form, Outlook only supports the Click event for
controls. This is a natural choice for buttons but not optimal for
controls like the combo box. You write the code by inserting it into a
form’s VBScript editor. You need to have the Outlook form open in the
Form Designer and click the View Code button found in the Form group
of the Developer tab.
Sub CheckBox1_Click()
msgbox "Hello World"
End Sub
The code page is fairly minimal with no syntax highlighting. I just tried this now and it does work. Dont forget to Publish your form to pick up the new changes.
I know this is almost 6 years late but, in VB and VBA, simply start with the form name. (And if that doesn't work, just keep going up a parent object and you'll get there.) So, your code becomes:
Sub example()
If MYFORMNAME.CheckBox1.Value = True Then
MsgBox("Checkbox 1 is checked.")
End If
End Sub
Of course, after typing "MYFORMNAME." you'll know if it will work because typomatic will kick in when the system recognizes "MYFORMNAME" after you hit the period.

Report Viewer VB.NET

I'm just trying to figure out how to use Report Viewer in VB.NET.
The report has only one text box with the data element name set to ReportName.
The code is simple.
Private Sub frmCalibrationPreviewReport_Load(sender As Object, e As EventArgs) Handles MyBase.Load
If _CalibrationReportID <> -1 Then
With rvCalibrationReport
.LocalReport.DataSources.Clear()
.ProcessingMode = Microsoft.Reporting.WinForms.ProcessingMode.Local
Dim tmpData As DataTable = modDeclare.SelectSQL("SELECT ReportName FROM tblReportTypes")
.LocalReport.DataSources.Add(New Microsoft.Reporting.WinForms.ReportDataSource("tmpData", tmpData))
End With
End If
Me.rvCalibrationReport.RefreshReport()
End Sub
Nothing is appearing on the report, it should contain two records.
Where am I going wrong?
Jim
Here's a great article covering Report Viewer
I mention this link as it appears your new to this. I would recommend reading this first.
Try Changing
.LocalReport.DataSources.Add(New Microsoft.Reporting.WinForms.ReportDataSource("tmpData", tmpData))
To
.LocalReport.DataSources.Add(New Microsoft.Reporting.WinForms.ReportDataSource("tmpData", tmpData.defaultview))
I would also suggest looking at this question, very similar to yours...
Bind DataTable to RDLC and ReportViewer
The problem with this question is there are a lot of gears at work when using reporting in visual studio. The problem could also be in the report file itself (.RDLC) if the field is not referencing the data source correctly. With the limited amount of information all I could suggest is using a working template and slowly adding your desired elements one at a time.

Visual basic 2008 issue with adding a method to my dynamic objects

I have searched for an answer to my question since last thursday. A lot of answers about my exact same question have been answerd in vb.net. However, I am working on Visual Basic 2008 and those two language seems to have differences that are for me difficult to understand. So here is my issue.
I need to create several picture box and I have created them dynamicly as several sites recommanded. That part works fine. Issue begins when I want to click on them. I read enough to understant that it is not because I have created the object that I have created the method attached to them. Then I create the method. Still no problem except when I am running the code each button does the same thing because they are all attached to the same method. I came to a solution: I need to transfer with the method an argument to tell wich Picturebox I am clicking on, but because I am using addressof I can't. I know few sites that have talked about the same issues and solved it using a lamda expression. If someone could give me the code I should use I would be really thankful.
Here is my code:
For i = 0 To 7
'couleur is the name I give to my picturebox object and objet () is the sub in which I created my object
couleur(i) = objet()
Next
For x = 0 To 7
' initiasation of location, etc.
Next
' This is the issue !!! I do not know how to say this line into vb8
' I want to pass in argument X to know on which object I have cliked on and then use a seled case to make separated command afterward.
For x = 0 To 7
AddHandler couleur(i).Click, Function(senderobj, args) couleur_click(x)
Next
End Sub
Sub couleur_click(ByVal i As Integer)
' select case doing seperated things depending on the x receive in argument
End Sub
Thank all of you for help, sorry for my language it is not my first language.
Why don't you change couleur_click to take the sender as a parameter? You will then know the source of the click, from which you can find the index of the PictureBox in your couleur array:
' ...
For x = 0 To 7
AddHandler couleur(i).Click, AddressOf couleur_click
Next
' ...
Sub couleur_click(sender As Object, e As EventArgs)
Dim pictureBoxSource As PictureBox = sender
' Find the index of the source in the base collection
Dim index = Array.IndexOf(couleur, pictureBoxSource)
Select Case index
' ...
End Select
End Sub
Set the tag property of each PictureBox, then in the click event handler you can do a select case on the tag.
You can't add parameters to the built in event handlers.