I can create a docx file from dotx file with vb.net. However, I need a pdf file from this dotx file template. I tried to create a pdf file from the MS Office Word by saving itself as pdf, I hope that it can be done also by coding in vb.net
Imports Word = Microsoft.Office.Interop.Word
Public Class frmWordTemplate
Private wdApp As Word.Application
Private wdDocs As Word.Documents
Const sPath As String = "D:\"
Private sFileName As String
Private Sub btnCreate_Click(sender As Object, e As EventArgs) Handles btnCreate.Click
sFileName = "export-file"
wdApp = New Word.Application
wdDocs = wdApp.Documents
Dim wdDoc As Word.Document = wdDocs.Add(sPath & "template_sample.dotx")
Dim wdBooks As Word.Bookmarks = wdDoc.Bookmarks
wdBooks("bkClient_name").Range.Text = txtClient.Text.ToString
wdBooks("bkDate").Range.Text = dtpDate.Text.ToString
wdDoc.SaveAs2(sPath & sFileName & ".docx")
ReleaseObject(wdBooks)
wdDoc.Close(False)
ReleaseObject(wdDoc)
ReleaseObject(wdDocs)
wdApp.Quit()
Adding a fileformat in saving
wdDoc.SaveAs2(sPath & sFileName & ".pdf", Word.WdSaveFormat.wdFormatPDF)
Jobs done!
Related
I am Using a web browser object to browse to the pdf file on form load event.
The file lives in thae same folder as the front-end.
I am not sure what I am doing wrong here. I have tried CurrentProject.Path & "\Internal Framework Summary.pdf" with no luck.
The code is:
Private Sub Form_Load()
Dim Thefile As String
Thefile = Application.CurrentProject.Path & "\Internal Framework Summary.pdf"
Me.web1Control.Object.Navigate Thefile
End Sub
I have learnt that using the code on the form open event by itself doesn't work sometimes. It doesn't work on the form load event either. As a result I have created a module
Sub loadPDFFile()
Dim webBrowser2 As WebBrowser
Dim pdfPath As String
Set webBrowser2 = webControl.Object
pdfPath = Application.CurrentProject.Path & "\Internal Framework Summary.pdf"
webBrowser2.Navigate pdfFilePath
End Sub
Then on the Form Open event:
Private Sub Form_Open(Cancel As Integer)
Me.webControl.ControlSource = Application.CurrentProject.Path & "\Internal Framework Summary.pdf"
End Sub
Finally on the Form Load event:
Private Sub Form_Load()
loadpdf
End Sub
This seems to be working now but I am not sure if there is a better and more efficient way of doing it.
I try to follow this LINK but this tutorial is viewing direct an MS Word that has already an data to it.
I also try this code below
Dim objWord As Word.Application
Dim objDoc As Word.Document
objWord = CreateObject("Word.Application")
objDoc = objWord.Documents.Open("G:\Folder\NewFolder\HHHHH.docx")
objWord.Visible = True
but this code is fix that if the MS Word it in that path and view it.
All I want to achieve is Browse the MS Word wherever it is located on my PC and display it in the form. And if possible click a button and preview it in crystal report. So I can print it.
You can use OpenFileDialog:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim filedialog As New OpenFileDialog
filedialog.InitialDirectory = "C:\"
filedialog.Filter = "Word Files|*.docx"
filedialog.ShowDialog()
End Sub
works fine for me.
Hi there i am looking to use the print dialog box when printing from my VB program. I am trying to print Word Documents, i can successfully print one document, however if i specify more than one document in print dialog box then the printer will still only print one. All i need is to be able to choose how many documents i want to print.
Thanks
Below is my code for the relevant button:
Private Sub Button19_Click(sender As Object, e As EventArgs) Handles Button19.Click
Dim f As New OpenFileDialog
Dim p As New PrintDialog
Dim app As Word.Application
Dim doc As Word.Document
'Open file and print dialogs to get desired document and printer
If p.ShowDialog = Windows.Forms.DialogResult.OK Then
'Create instance of Word Application
app = New Word.Application
'Set Printer
app.WordBasic.FilePrintSetup(Printer:=p.PrinterSettings.PrinterName, DoNotSetAsSysDefault:=1)
'Set filename to object type
Dim filename As Object = f.FileName
Dim m As Object = System.Reflection.Missing.Value
'Open document
doc = app.Documents.Open("C:\CamJam\Tickets\Monday\drivingrange_mon_pm.docx")
'Print document
app.PrintOut()
'Close document
app.Documents.Close()
'Quit word application
app.Quit()
'Release
app = Nothing
End If
End Sub
I came up with this site: http://forums.codeguru.com/showthread.php?15568-WRITING-DATA-TO-WORD-DOCUMENT-FROM-VB
Private Sub btnPrint_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrint.Click
Dim wd As Word.Application
Dim wdDoc As Word.Document
wd = New Word.Application
wd.Visible = True
wdDoc = wd.Documents.Add("D:\Employees.dotx") 'Add document to Word
With wdDoc
.FormFields("E_Name").Result = txtLastName.Text [error]
.FormFields("E_NName").Result = txtFirstName.Text
.FormFields("E_Address").Result = txtMiddleName.Text
End With
wdDoc.PrintPreview() 'Opens print Preview Window
wdDoc.SaveAs("D:\doc1.DOC") 'Saves the Document
wd.Application.Quit() 'Closing Word Application
wd = Nothing 'Releasing References to Variable
End Sub
Error: The requested member of the collection does not exist.
Can anyone help me with connecting microsoft word with VB.net
The video you are referring is using vb6. In VS 2010, you can do it like this:
Private Sub btnPrint_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrint.Click
Static wd1 As Word.Application
Static wd1Doc As Word.Document
wd1 = New Word.Application
wd1.Visible = True
wd1Doc = wd1.Documents.Add("yourFilePath\profile.dot") 'example: "D:\profile.dot"
With wd1Doc
.FormFields("W_Lname").Result = txtLastName.Text 'In VS2010, property `Range` is Readonly.
.FormFields("W_Fname").Result = txtFirstName.Text 'You need to use `Result`
.FormFields("W_Mname").Result = txtMiddleName.Text
End With
wd1 = Nothing
wd1Doc = Nothing
End Sub
But first, you need to add Microsoft Word <ver> Object Library to your reference.
And if you still see the errors, you might need to import:
Imports Microsoft.Office.Interop
Note: If you are using MS Office 2013, your file extension should be .dotx (profile.dotx)
So you might need to change
wd1Doc = wd1.Documents.Add("yourFilePath\profile.dotx")
^
Try to browse this link and read carefully all the topics http://wiki.smartsimple.com/wiki/Adding_Form_Fields_to_a_MS_Word_Document
Dim wd As Word.Application
Dim wdDoc As Word.Document
wd = New Word.Application
wd.Visible = True
wdDoc = wd.Documents.Add("D:\Employees\Employees.dotx") 'Add document to Word
With wdDoc
.FormFields("E_Name").Result = txtLastName.Text
.FormFields("E_Nickname").Result = txtNickname.Text
.FormFields("E_Address").Result = txtAddress.Text
.FormFields("E_Position").Result = cboPosition.Text
End With
wdDoc.SaveAs("D:\Employees\" & txtLastName.Text & "" & txtFirstName.Text & ".DOC") 'Saves the Document
MsgBox("Please check the folder of employees and open the name of the employee to print his/her document", MsgBoxStyle.OkOnly)
wd.Application.Quit() 'Closing Word Application
wd = Nothing 'Releasing References to Variable
wdDoc = Nothing
That will do the trick, no errors..
Note:Please create a destination folder for your documents like this one
"D:\Employees\Employees.dotx", this line of codes instantly save the word document in the destination folder for later printing purposes. Cheers
I can see my pdf file
Private Sub Form_Load()
Dim FILENAME As String
FILENAME = "C:\Users\USER\Desktop\abandon.pdf"
AcroPDF5.LoadFile FILENAME
End Sub
But I want search in my pdf and find abandon and show it on a label on a form.