Manipulate visio shapes with data passed from vb.net - vb.net

I have the below code which is opening visio, opening a file in visio, printing the file and then closing; this all works fine.
However, now my task is to pass information over to the visio document page called 'Ticket Task', bind that information to some shapes and then print it.
I know this is possible with vb6 (that is what the outdated code is written in), however is there a way to do this in vb.net?
Thanks!
Code:
''Set up the file path
Dim docPath As String = System.Environment.GetFolderPath(System.Environment.SpecialFolder.MyDocuments) + "\" + splFileData(0) + "\" + splFileData(1) + ".vsd"
'Set up the attributes for the opening/printing of the document.
psi.UseShellExecute = True
psi.Verb = "print"
'psi.EnvironmentVariables.Add("Orders", "Hello")
psi.Arguments = printer
psi.WindowStyle = ProcessWindowStyle.Hidden
psi.FileName = docPath
Console.WriteLine("Printing: " + docPath)
'Start the process (open visio document, print, close)
Process.Start(psi)

You'll have to change the way you're opening and printing Visio documents. This tutorial shows how to use VB.NET to work with documents. The Open function returns an object of type Microsoft.Office.Interop.Visio.Document. You can use this object to attach information to shapes as discussed in here. In fact, the VB.NET code is very similar to VB6. If you want Visio to be invisible, you can open the document as follows:
Microsoft.Office.Interop.Visio.InvisibleApp application = new Microsoft.Office.Interop.Visio.InvisibleApp();
application.Visible = false;
Microsoft.Office.Interop.Visio.Document doc = application.Documents.Open...

Related

MS Word runs on background and requests documents to be saved even though it is already saved

I have a procedure that creates a PDF file according to an ms word template and its data is retrieved from a database.
It works fine, creates a PDF file perfectly , no run time errors. The problem is that whenever I shut off the computer, ms word prevents the shutdown and if I press cancel ms word shows a message;
The code goes like this;
Dim wordApp As Word.Application
Dim templateBookmarks As Word.Bookmarks
Dim templateName As String
Dim template As Word.Document
'Some other variables for computations
wordApp = CreateObject("Word.Application")
sourceTable = New DataTable
'Some other procs to fill the data table
templateName = "Foo Template.docx"
template = wordApp.Documents.Add(templatePath & templateName)
templateBookmarks = template.Bookmarks
templateBookmarks.Item("sample bookmark").Range.Text = "foo"
'Then fills the table in the template like...
template.Tables(1).Cell(1, 1).Range.Text = dataSource.Rows(0).Item(0)
'Then saves the document as a pdf
Dim saveName As String = "sample file"
template.SaveAs2(savePath & saveName, Word.WdSaveFormat.wdFormatPDF)
I have tried to force garbage collection for the word COM resources, as well as changing the template from an actual document i.e. docx to a word template .dotx. I also tried the method Quit() but it only shows the ms word message much earlier. This is the first time I needed to use interop so pardon if I don't have much idea about it.
The files I needed are saved, the only problem is the ms word message and unsaved and unnecessary files e.g. Document1,Document2,Document3 that seems to be created aside from the required PDF
Use the Document.Close method which closes the specified document after saving files using the PDF file format. It allows specifying the SaveChanges parameter which specifies the save action for the document. Can be one of the following WdSaveOptions constants: wdDoNotSaveChanges, wdPromptToSaveChanges, or wdSaveChanges.
On Error GoTo errorHandler
ActiveDocument.Close SaveChanges:=wdDoNotSaveChanges
errorHandler:
If Err = 4198 Then MsgBox "Document was not closed"

Printing a document from VB

I have an app which monitors a network location for new documents.
They can be word documents, PDF files, spreadsheets etc.
When a document is detected, it is copied to a local folder within c:\Temp.
What I need is for the document, once copied, to be sent to a specified (not default) printer.
Has anyone got any ideas on how I can achieve this?
Thanks!!
You may need to create a variety of functions to print your document. Like using LPR to print PDF or PostScript, Microsoft.Office.Interop for Word and Excel documents, and so on...
If it were me, I would use System.IO.Path.GetExtension(filename) to determine the file type and then call whichever function was most appropriate...or log that the file was not printable if the format was not handled.
Microsoft.Office.Interop
Using Microsoft.Office.Interop.Excel, you can call the PrintOutEx method on a Workbook or Worksheet and specify which printer to use.
See:
https://msdn.microsoft.com/en-us/library/microsoft.office.interop.excel.worksheets.printoutex.aspx
and
https://msdn.microsoft.com/en-us/library/microsoft.office.interop.excel._workbook.printoutex.aspx
With Microsoft.Office.Interop.Word you can set the ActivePrinter property of the Application and then use the PrintOut method on the Document.
See:
https://msdn.microsoft.com/en-us/library/microsoft.office.interop.word._application.activeprinter.aspx
and https://msdn.microsoft.com/en-us/library/microsoft.office.interop.word._document.printout.aspx
LPR
I wrote a tool once that prints PDF and PostScript files. The code was something like:
'Set the IP address of the printer to use.
If printer1 Then
printserver = printer1Address
ElseIf printer2 Then
printserver = printer2Address
ElseIf printer3 Then
printserver = printer3Address
End If
'Use LPR to print the file.
Dim lprProcess As New Process()
With lprProcess.StartInfo
.UseShellExecute = False
.FileName = "CMD"
.Arguments = "/c LPR -s " & printserver & " -P P3 " & myNewFileName
End With
lprProcess.Start()
LPR is not a included by default in Windows 7 and above, but it is a cake-walk to turn on. See steps 1-3 on http://campus.mst.edu/cis/desktop/documentation/pc/win7_x64/lpr_printer/install.htm

How to set a specific default directory for a Save As operation in VBA

We have a MS Access software which we need to be able to save a MS Word document from. It is not problem to get the save as dialog in word, using the VBA, but is it possible to get it to change the default directory to a path specified in the VBA? It just saves as on the desktop as default.
Edit: For the first answer:
You can use Application.FileDialog to ask the user for a path to save to:
Dim ofd As Office.FileDialog
Set ofd = Application.FileDialog(msoFileDialogSaveAs)
ofd.Title = "Save As..."
ofd.InitialView = msoFileDialogViewList
ofd.InitialFileName = "c:\temp\myfile"
' Show Dialog and abort if user clicks cancel:
If (ofd.Show = 0) Then Exit Sub
MsgBox ofd.SelectedItems(1) ' shows the selected filename
Use that path to directly save the word document.
You can just add the filename you would like to the dialog as such:
Application.GetSaveAsFilename("c:\temp\myfile")
This will open the dialog in the temp folder and suggest myfile as name for the file.
See here for other modify-able stuff in the dialog: https://msdn.microsoft.com/en-us/library/office/ff195734%28v=office.15%29.aspx

how can I convert pdf file to word file using vb.net

I'm trying to develop a program which allows the user to convert a pdf file to a word file using vb.net.
Is there any good API for this ?
And, is it as easy as it looks like?
try this,
' Path of input PDF document
Dim filePath As String = "d:\\Source.pdf"
' Instantiate the Document object
Dim document As Aspose.Pdf.Document = New Aspose.Pdf.Document(filePath)
' Create DocSaveOptions object
Dim saveOptions As DocSaveOptions = New DocSaveOptions()
' Set the recognition mode as Flow
saveOptions.Mode = DocSaveOptions.RecognitionMode.Flow
' Set the Horizontal proximity as 2.5
saveOptions.RelativeHorizontalProximity = 2.5F
' Enable the value to recognize bullets during conversion process
saveOptions.RecognizeBullets = True
' save the resultnat DOC file
document.Save("d:\\Resultant.doc", saveOptions)

How to stop a Word Document immediately when newly created

I have a VS2010 Vb.net program that creates a Word 2007 file.
My Normal.dot file is customised to give me a new Tab with Buttons in that do specific things via VBA in the Normal.dot program when those Buttons are pressed.
This all works fine, however, I now want to add some functionality whereas as soon as the new Word document is created, it edits a Task in Outlook.
I have edited the 2 "This Document" Procedures and you can see my Normal.Dot file in the attached Screenshot.
When I run my VB.Net program to create a brand new Word 2007 document, the program does NOT stop on either of the message boxes, it just continues and opens the Word document as before, my code is below, what am I doing wrong ?!?
'Open or Create Word document for Editing
myNewsLetter = myFolder + myLeague + "News" + mySession + ".doc"
If File.Exists(myNewsLetter) Then
'do nothing
Else
myTemplate = myTempFolder + "NL Skeleton.doc"
File.Copy(myTemplate, myNewsLetter)
Create_Blank_Newsletter()
End If
'Open Word Newsletter, or switch to it if it's already open
Dim myFileOpen As Boolean
myFileOpen = IsFileOpen(myNewsLetter)
If myFileOpen = False Then
MSDoc = MSWord.Documents.Open(myNewsLetter)
End If
MSWord.WindowState = Word.WdWindowState.wdWindowStateNormal
MSWord.Visible = True
MSWord.ActiveDocument.Bookmarks("\StartOfDoc").Select()
OK, sorted this, the full discussion can be found here ... http://www.vbaexpress.com/forum/showthread.php?p=286771#post286771
Basically, I'm not creating a NEW document, I am creating a new document via a Copy and then opening that existing document !!!