Insert Macros Word 2010 into a Visual Basic 2008 Express Edition - visual-studio-2010

I created a Macro with Microsoft Word 2010:
Sub Macro1()
Selection.WholeStory
End Sub
At the event Button1_Click I would execute the macro :
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Selection.WholeStory
End Sub
I remember that Visual Basic 6 allowed to enter Macro object from Microsoft Application.
Now with Visual Basic 2008 Express Edition?

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'Funziona Estrapola in un file HTML
'Dim settaggio As New XsltSettings
'Dim xslt As New XslCompiledTransform
'xslt.Load("Stile.xslt", settaggio, New XmlUrlResolver())
'xslt.Transform("Fascicolo.xml", "Fascicolo.html")
'Fine funziona
' Create the XslTransform object and load the style sheet. File XSLT
Dim xslt As New XslCompiledTransform()
xslt.Load(Label4.Text)
' Load the file to transform. File XML
Dim doc As New Xml.XPath.XPathDocument(Label2.Text)
' Create the writer.
Dim writer As XmlWriter = XmlWriter.Create(DirListBox3.Path & "\" & "Fascicolo.doc", xslt.OutputSettings)
' Transform the file and send the output to the console.
xslt.Transform(doc, writer)
writer.Close()
Process.Start(DirListBox3.Path & "\" & "Fascicolo.doc")
Dim var As New Microsoft.Office.Interop.Word.Application()
Dim ciao As New Microsoft.Office.Interop.Word.Document
ciao.Activate()
var.Documents.Open(FileName:="C:\Users\f.irrera\Desktop\Fascicolo.doc")
var.Selection.WholeStory()
var.Selection.Copy()
var.ChangeFileOpenDirectory("C:\Users\f.irrera\Desktop\")
var.ActiveDocument.SaveAs2(FileName:="Fascicolo.daf", FileFormat:=2)
ciao.Close()
var.Documents.Close()
var.Application.Quit()
End Sub

Related

edit through visual studio an msword file

i am trying a program that can edit an existing ms word document through an instant editing at a visual studio 2010 program but im having trouble. my codes:
Imports Microsoft.Office.Interop
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim wd1 As Word.Application
Dim wd1Doc As Word.Document
wd1 = New Word.Application
wd1.Visible = True
wd1Doc = wd1.Documents.Add("C:\Users\DELL\Desktop\activity6\profile.dotx")
With wd1Doc
.FormFields("w_name").Range = TextBox1.Text
.FormFields("w_age").Range = TextBox2.Text
End With
wd1 = Nothing
wd1Doc = Nothing
End Sub
End Class
the errors says: "property range is read only"
Your code is almost correct. If you want to the the text of a Range object, you need to use its .Text property:
With wd1Doc
.FormFields("w_name").Range.Text = TextBox1.Text
.FormFields("w_age").Range.Text = TextBox2.Text
End With

Add combobox to selected file Visual Studio 2010 VB

i want to make an .exe file using VS2010 to select excel file, and read it's sheets through a combobox.
I have now sorted to make a 'browse' button to select the excel file and generate a msgbox but yet couldn't assign the combobox to read it's sheets.
here is my code
Imports Microsoft.Office.Interop
Imports Microsoft.Office.Interop.Excel
Public Class Form1
Public Sub btnBrowse_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBrowse.Click
Dim myFileDlog As New OpenFileDialog()
'Dim oXL As Excel.Application
'Dim oWB As Excel.Workbook
'Dim oSheet As Excel.Worksheet
'look for files in the c drive
myFileDlog.InitialDirectory = "c:\"
'specifies what type of data files to look for
myFileDlog.Filter = "All Files (*.*)|*.*" & _
"|Data Files (*.xls)|*.xls" & _
"|Data Files (*.xlsx*)|*.xlsx*" & _
"|Data Files (*.xlsm*)|*.xlsm*"
'specifies which data type is focused on start up
myFileDlog.FilterIndex = 2
'Gets or sets a value indicating whether the dialog box restores the current directory before closing.
myFileDlog.RestoreDirectory = True
'seperates message outputs for files found or not found
If myFileDlog.ShowDialog() = _
DialogResult.OK Then
If Dir(myFileDlog.FileName) <> "" Then
MsgBox("File Exists: " & _
myFileDlog.FileName, _
MsgBoxStyle.Information)
Else
MsgBox("File Not Found", _
MsgBoxStyle.Critical)
End If
End If
'Adds the file directory to the text box
txtFileDirectory.Text = myFileDlog.FileName
End Sub
Public Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
'Dim oXL As Excel.Application
Dim oWB As Excel.Workbook
Dim oSheet As Excel.Worksheet
'Dim oRng As Excel.Range
For x As Integer = 1 To FileDialog.Sheets.Count()
If TypeOf oWB.Sheets(x) Is Excel.Worksheet Then
ComboBox1.Items.Add(FileDialog.Sheets(x).name)
End If
Next
ComboBox1.SelectedIndex = 0
End Sub
End Class
Thanks in advance!
You need to set your workbook oWB first and then use the below.
Change
If TypeOf oWB.Sheets(x) Is Excel.Worksheet Then
ComboBox1.Items.Add(FileDialog.Sheets(x).name)
End If
to
If TypeOf oWB.Sheets(x) Is Excel.Worksheet Then
ComboBox1.Items.Add(oWB.Sheets(x).name)
End If
Similarly FileDialog.Sheets.Count() is incorrect

How to get Volume Serial Number using Visual Basic 2010?

I'm trying to get Volume Serial Number using Visual Basic 2010,
Is there a whole code example that shows me how to do this?
Thanks
I guess the simplest answer to my question was given by:
Hans Passant:
From his link,
I just copied and pasted this function and it works for Microsoft Visual basic 2010 express, Without any modifications
Public Function GetDriveSerialNumber() As String
Dim DriveSerial As Long
Dim fso As Object, Drv As Object
'Create a FileSystemObject object
fso = CreateObject("Scripting.FileSystemObject")
Drv = fso.GetDrive(fso.GetDriveName(AppDomain.CurrentDomain.BaseDirectory))
With Drv
If .IsReady Then
DriveSerial = .SerialNumber
Else '"Drive Not Ready!"
DriveSerial = -1
End If
End With
'Clean up
Drv = Nothing
fso = Nothing
GetDriveSerialNumber = Hex(DriveSerial)
End Function
I would like to thank everyone for their help,
And i apologize for repeating the question,
I did do a google search and a stackflow search,
But my search was"
"get hard drive serial number in visual basic 2010"
So this website did not show up,
Thanks again
This thread here http://social.msdn.microsoft.com/Forums/vstudio/en-US/43281cfa-51c8-4c35-bc31-929c67abd943/getting-drive-volume-serial-number-in-vb-2010 has the following bit of code that you could use/adapt.
I made a piece of code for you to show all drive information.
The Volume serial number is included you can get that by simple
putting some more if's in the code
Imports System.Management
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim drivetype() As String = {"Unknown", "NoRootDirectory", _
"RemoveableDisk", "LocalDisk", "NetworkDrive", "CompactDisk", "RamDisk"}
Dim allDrives() As String = Environment.GetLogicalDrives()
For Each drive In allDrives
Dim win32Drive As String = _
"Win32_LogicalDisk='" & drive.Substring(0, 2) & "'"
Dim Disk As System.Management.ManagementObject _
= New System.Management.ManagementObject(win32Drive)
Me.ListBox1.Items.Add(drive.ToString & drivetype(CInt((Disk("DriveType").ToString))))
For Each diskProperty In Disk.Properties
If Not diskProperty.Value Is Nothing Then
Me.ListBox1.Items.Add("---" & diskProperty.Name & "=" & diskProperty.Value.ToString)
End If
Next
Next
End Sub
End Class

visual basic 2010 copy paste subroutine

I am trying to write 2 simple subroutines in VB 2010 that could be used with toolstripbutton contorls for multiple textboxes in a form. I know simple copy paste could be done using the textbox1.Copy() and TextBox1.Paste() methodes. What i am trying to do is write a common subroutine which could be used on any textboxes in the form not just one particular textbox.
My codes are below, I know there are errors in it, just wondering how it could be achieved. Any help would be highly appreciated. Thanks.
Public Class Form1
Private Sub copytext()
Dim txt As Control
If TypeOf txt Is TextBox Then
Clipboard.Clear()
Clipboard.SetText(txt.SelectedText)
End If
End Sub
Private Sub pastetext()
Dim txt As Control
If TypeOf txt Is TextBox Then
txt.Text = Clipboard.GetText
End If
End Sub
Private Sub mnuCopy_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuCopy.Click
Call copytext()
End Sub
Private Sub mnuPaste_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuPaste.Click
Call pastetext()
End Sub
End Class

Visual Studio. How to copy record from database to word .doc and print it

In Visual studio 2010>New Project>Visual Basic>Windows>Windows forms Application, i have made a form (form1.vb) and a database (Local Database>"Database1.sdf") and a Table with 3 Columns ("Name","City","Age").
I like to copy this 3 fields and paste to document "test1.doc" (open this with Ms Office or Open Office Writer). I have bookmarks ("PasteName", PasteCity", "PasteAge") in specified places in test1.doc .
How to make a button to open the document "test1.doc" and copy - paste this 3 items from table to doc and preview before print it? (not for save - only print preview and close without save after printing)
I have find this code for MS Office but didn't work in Visual Studio. I like something similar. (this code is for a doc Form Fields - I have Bookmarks in my doc).
Private Sub cmdPrint_Click()
Dim appWord As Word.Application
Dim doc As Word.Document
Set appWord = GetObject(, "Word.Application")
Set appWord = New Word.Application
Set doc = appWord.Documents.Open("C:\WordForms\CustomerSlip.doc", , True)
With doc
.FormFields("fldCustomerID").Result = Me!CustomerID
.FormFields("fldCompanyName").Result = Me!CompanyName
.FormFields("fldContactName").Result = Me!ContactName
.Visible = True
.Activate
End With
Set doc = Nothing
Set appWord = Nothing
End Sub
Thanks programers people
This works for me. (button action)
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'Print customer slip for current customer.
Dim appWord As Word.Application
Dim doc As Word.Document
'Avoid error 429, when Word isn't open.
On Error Resume Next
Err.Clear()
'Set appWord object variable to running instance of Word.
appWord = GetObject(, "Word.Application")
If Err.Number <> 0 Then
'If Word isn't open, create a new instance of Word.
appWord = New Word.Application
End If
doc = appWord.Documents.Open("D:\Test.docx", , True)
doc.Visible()
doc.Activate()
With doc.Bookmarks
.Item("Name").Range.Text = Me.NameID.Text
.Item("City").Range.Text = Me.CityID.Text
End With
Dim dlg As Word.Dialog
dlg = appWord.Dialogs.Item(Word.WdWordDialog.wdDialogFilePrint)
dlg.Display()
'doc.Printout
doc = Nothing
appWord = Nothing
Exit Sub
errHandler:
MsgBox(Err.Number & ": " & Err.Description)
End Sub

Resources