Outlook External Application/Service Start - vba

Is there any way to have outlook start an external application or service based on an outlook calendar task, event, appointment? Also if so, is there a way to get it to pass parameters to it?

Yes you can do this using the Shell method.
Private Sub TestAcrobatReader()
Const strcProgramName As String = _
"C:\Program Files\Adobe\Reader 9.0\Reader\AcroRd32.exe"
Const strcFilePath As String = _
"C:\Program Files\Adobe\Reader 9.0\Reader\plug_ins\" _
& "Annotations\Stamps\Words.pdf"
Dim dblProgTaskID As Double
Dim strPathName As String
strPathName = strcProgramName & " " & strcFilePath
dblProgTaskID = Shell(strPathName, vbMaximizedFocus)
MsgBox "Program Task ID: " & dblProgTaskID
End Sub
Code borrowed from here. You can pass additional parameters by concatenating them on the strPathName.
For automating based on Outlook Calendar there is a wealth of information here.

Related

File Download via shdocvw.dll with custom headers

I need to download a really large file in msaccess via a vba application.
Using the objects MSXML2.ServerXMLHTTP.6.0 and WinHttp.WinHttpRequest.5.1 result in an error stating that there is not enough storage available to complete this operation. Therefore i resorted in using the DoFileDownload method from shdocvw.dll.
What i want to do is pass an extra header (an API key) to the request sent by the function.
Here is roughly what i want to do.
Private Declare Function DoFileDownload Lib "shdocvw.dll" _
(ByVal lpszFile As String) As Long
Public Sub Download()
sDownloadFile = StrConv(<link_to_download>, vbUnicode)
'set a header before calling DoFileDownload
Call DoFileDownload(sDownloadFile)
End Sub
How do i approach this problem?
A WebRequest downloading a whole file at once stores the whole data in response.
Although there are options to chunk response, using Wget is less coding, but more options.
Private Sub DownloadFileWget()
Const PathToWget As String = "" 'if wget is not in path use "Path\To\Wget"
Dim LinkToFile As String
Dim SavePath As String
With CreateObject("WScript.Shell")
LinkToFile = "http://download.windowsupdate.com/microsoftupdate/v6/wsusscan/wsusscn2.cab" 'huge file > 500MB
SavePath = "C:\doc" 'folder to save download
.CurrentDirectory = SavePath
.Run Chr(34) & PathToWget & "wget.exe" & Chr(34) & " --header='name: value' " & Chr(34) & LinkToFile & Chr(34) & " -N", 1, True
' -N: Continue download only if the local version is outdated.
End With
End Sub

Open a file in a new instance of program

All;
I have a bit of code I've written that opens a design blueprint when I scan a bar code. It works well enough, but I'd like to open a new instance of the design software (Solidworks) and have the print display in the new instance. Right now, no matter how many Solidworks instances I have open, the print will only open in the first instance started.
The line commented out below is the line that works, just not in the right instance. The line below that is what I'd expect to work, but it returns a 'file not found' even though the path to solidworks and the print path are both correct.
Any explanation as to why this isn't working would be much appreciated as I'm obviously very new at this...and have no idea what I'm doing.
Private Sub Button1_Click_1(sender As Object, e As EventArgs) Handles Button1.Click
Try
Dim barcode As String = tb_barcode.Text
Dim filename As String = tb_barcode.Text
'Add File Extension to end of path
Dim ext As String = ".SLDDRW"
'Split job number from detail number in barcode textbox
barcode = Split(tb_barcode.Text, ".")(0)
filename = Split(tb_barcode.Text, ".")(1)
'- This works, just in primary instance
'System.Diagnostics.Process.Start("G:\Fixtures\" & barcode & "\Details\" & barcode & " DET " & filename & ext)
'- This does not work
System.Diagnostics.Process.Start("'C:\Program files\Solidworks Corp\Solidwork\SLDWORKS.exe' 'G:\Fixtures\" & barcode & "\Details\" & barcode & " DET " & filename & ext + "'")
Catch
MessageBox.Show("File Not Found")
End Try
End Sub
Sorry for naive approach but shouldn't there be a comma in Process.Start between 2 arguments?
Start(String, String)
Starts a process resource by specifying the name of an application and a set of command-line arguments, and associates the resource with a new Process component. docs
Why don't you use the Application.ExecutablePath.That returns the Application's path with its full name. Then your code should be
System.Diagnostics.Process.Start(Application.Executablepath, "G:\Fixtures\" & barcode & "\Details\" & barcode & " DET " & filename & ext + "'")
Also make sure that the second string argument is a valid path.

how can a target the current user's documents folder?

So we have a system that requires users to log in and it has there own roaming profile. So in this string of code how can i target the current users document folder? (FYI excel 2010)
'WORKAROUND:
Dim PID As Double
Dim strRootPath As String
Const strExpExe = "explorer.exe"
Const strArg = " " '" /e,/root, "
'this is where i need to figure out how to target current user's documents
'// Change rootpath here
strRootPath = "C:\Data Files"
PID = Shell(strExpExe & strArg & strRootPath, 3)
the rest of the function does great... it opens file explorer i just cant figure the syntax for telling it to look for the current user.
Probably the best way would be with a function like this:
Function docsFolder() As String
docsFolder = CreateObject("WScript.Shell").SpecialFolders("MyDocuments")
End Function
There are other ways too but this one will work on any version of Windows and with user customizations.
For example, in my case, I have my documents folder on a mapped X: drive, so simply stuffing my username into a C:\ path would not work.
More Information:
Stack Overflow : Language independent way to get “My Documents” folder in VBA
Microsoft Technet : What is the path to My Documents?
MSDN : Wshell: SpecialFolders Property
I'm not sure how flexible you want this to be but you could try the following
strRootPath = "C:\Users\" + Environ("Username") + "\Documents"
Got it! thanks! for anyone that might care... The final string that made her run!
Function docsFolder() As String
docsFolder =
CreateObject("WScript.Shell").SpecialFolders("MyDocuments")
End Function
Private Sub test()
Dim PID As Double
Dim strRootPath As String
Const strExpExe = "explorer.exe"
Const strArg = " " '" /e,/root, "
'// Change rootpath here
strRootPath = "C:\Users\" + Environ("Username") + "\Documents"
PID = Shell(strExpExe & strArg & strRootPath, 3)
End Sub

Retrieve CustomXMLParts in Excel Addin using VB.NET

I'm needing a little help retrieving the values stored in a CustomXMLPart in an Excel Addin using VB.NET. I've searched and haven't found a lot of detail on this. From what I've found, the code that I have should work. At first, I thought my xml part wasn't being added so to persist across sessions, but I displayed the count of the CustomXMLParts collection before (3) and after (4) adding my custom xml part and the count did increase by one. I, also displayed the count upon opening the saved Excel workbook and it was the same number (4) after adding my CustomXMLPart to the collection. Here's the pertinent code below. Any help would be greatly appreciated. Need more information just let me know.
In the Excel addin, I have a popup window where I ask for user input and that's the information that I need to persist. In the code behind, that's where I create the xml and add to the collection.
Code:
Dim workbook As Excel.Workbook = Globals.ThisAddIn.Application.ActiveWorkbook
Dim xml As String
xml = "<?xml version=""1.0"" encoding=""utf-8"" ?>" _
& "<refreshViewPointData xmlns=""http://refreshviewpointdata.com"">" _
& "<dataReference>" _
& "<system>" & cboSystem.Text & "</system>" _
& "<library>" & cboLibraries.Text & "</library>" _
& "<view>" & txtObject.Text & "</view>" _
& "<headers>" & chkInclColumnHdrs.Checked.ToString() & "</headers>" _
& "<numOfRecords>" & txtRowCount.Text & "</numOfRecords>" _
& "<reference>" & txtReference.Text & "</reference>" _
& "</dataReference>" _
& "</refreshViewPointData>"
workbook.CustomXMLParts.Add(xml, System.Type.Missing)
In the ThisAddIn_Startup() method of ThisAddIn.vb file is where I attempt to retrieve the CustomXMLPart. I call a RetrieveCustomXMLPart() method from ThisAddIn_Startup().
Code for RetrieveCustomXMLParts():
Dim parts As Microsoft.Office.Core.CustomXMLParts
parts = Application.ActiveWorkbook.CustomXMLParts.SelectByNamespace("http://refreshviewpointdata.com")
If parts.Count > 0 Then
RefreshData(parts.ToString())
End If
Code for RefreshData():
Dim r As New RibbonViewPoint
Dim viewXMLPart As New XmlDocument
Dim system, library, sObject, reference As String
Dim headers As Boolean
Dim numRecords As Integer
'Load the xml from the string.
viewXMLPart.LoadXml(part)
'Retrieve the values from the xml document.
system = viewXMLPart.SelectSingleNode("/dataReference/system").Value
library = viewXMLPart.SelectSingleNode("/dataReference/library").Value
sObject = viewXMLPart.SelectSingleNode("/dataReference/view").Value
headers = CType(viewXMLPart.SelectSingleNode("/dataReference/headers").Value, Boolean)
numRecords = CType(viewXMLPart.SelectSingleNode("/dataReference/numOfRecords").Value, Integer)
reference = viewXMLPart.SelectSingleNode("/dataReference/reference").Value
'Call method to run the object to refresh the data.
r.RunSelectedObject(system, library, sObject, headers, reference, numRecords)
In the RetrieveCustomXMLPart() method, the SelectByNamespace() method is not returning my CustomXMLPart that clearly has the same namespace as what I'm passing in. Anyone know what's wrong?
Also, if anyone has any insight on something else I didn't understand that would be great as well. In the RefreshData() method, I made the viewXMLPart variable as an XMLDocument to load the data and get the values from there. Prior to I had it defined as "Dim viewXMLPart As New Microsoft.Office.Core.CustomXMLPart", which kept giving me a syntax error saying that "'Microsoft.Office.Core.CustomXMLPartClass.Friend Sub New()' is not accessible in this context because it is Friend."
Thanks!!!
Found a solution. The below is what worked.
Dim workbook = Application.ActiveWorkbook
Dim customXMLParts = workbook.CustomXMLParts.SelectByNamespace("urn:viewpoint-refresh")
Dim customXMLPart = customXMLParts.Cast(Of CustomXMLPart)().FirstOrDefault()

How do I customize the auto commenting text in Visual Studio?

When I type the trigger the auto comment feature in Visual Studio (by typing "'''" or "///"), most of the XML commenting details show up that I like. However, I typically add the history tag to the documentation so I can track and changes that are made to the method over time.
Is there any way I can customize the auto commenting feature so that it will add the history tag, and potentially some generic Name - Date - Change placeholder text?
I'd suggest using GhostDoc. It generates very smart comments using /// based on your method names and parameters. Also, it is fully customizable.
I think that you could use a tool as dgarcia said but try to chose one that makes the version control insetad, Personally I'm not a huge fan of keep the "history" or track of the project using comments in the code.
If you like that way you could create your own customized version of the snippet, this is easier if you use a tool like Snippy
Copy this file to your
My Documents\Visual Studio 2005\Code Snippets[Language]\My Code Snippets\
Just be carefull to change the file if you gonna use it in VB.NET
Hope this help
Just as followup to the comment to Olivier. Here is a copy of the macro now, look for the '' Do History section to see where I inserted code.
''// InsertDocComments goes through the current document using the VS Code Model
''// to add documentation style comments to each function.
''
Sub InsertDocComments()
Dim projectItem As ProjectItem
Dim fileCodeModel As FileCodeModel
Dim codeElement As CodeElement
Dim codeElementType As CodeType
Dim editPoint As EditPoint
Dim commentStart As String
projectItem = DTE.ActiveDocument.ProjectItem
fileCodeModel = projectItem.FileCodeModel
codeElement = fileCodeModel.CodeElements.Item(1)
''// For the sample, don't bother recursively descending all code like
''// the OutlineCode sample does. Just get a first CodeType in the
''// file.
If (TypeOf codeElement Is CodeNamespace) Then
codeElement = codeElement.members.item(1)
End If
If (TypeOf codeElement Is CodeType) Then
codeElementType = CType(codeElement, CodeType)
Else
Throw New Exception("Didn't find a type definition as first thing in file or find a namespace as the first thing with a type inside the namespace.")
End If
editPoint = codeElementType.GetStartPoint(vsCMPart.vsCMPartHeader).CreateEditPoint()
''// Make doc comment start.
commentStart = LineOrientedCommentStart()
If (commentStart.Length = 2) Then
commentStart = commentStart & commentStart.Chars(1) & " "
ElseIf (commentStart.Length = 1) Then
commentStart = commentStart & commentStart.Chars(0) & commentStart.Chars(0) & " "
End If
''// Make this atomically undo'able. Use Try...Finally to ensure Undo
''// Context is close.
Try
DTE.UndoContext.Open("Insert Doc Comments")
''// Iterate over code elements emitting doc comments for functions.
For Each codeElement In codeElementType.Members
If (codeElement.Kind = vsCMElement.vsCMElementFunction) Then
''// Get Params.
Dim parameters As CodeElements
Dim codeFunction As CodeFunction
Dim codeElement2 As CodeElement
Dim codeParameter As CodeParameter
codeFunction = codeElement
editPoint.MoveToPoint(codeFunction.GetStartPoint(vsCMPart.vsCMPartHeader))
''//editPoint.LineUp()
parameters = codeFunction.Parameters
''// Do comment.
editPoint.Insert(Microsoft.VisualBasic.Constants.vbCrLf)
editPoint.LineUp()
editPoint.Insert(Microsoft.VisualBasic.Constants.vbTab & commentStart & "<summary>")
editPoint.Insert(Microsoft.VisualBasic.Constants.vbCrLf)
editPoint.Insert(Microsoft.VisualBasic.Constants.vbTab & commentStart & "Summary of " & codeElement.Name & ".")
editPoint.Insert(Microsoft.VisualBasic.Constants.vbCrLf)
editPoint.Insert(Microsoft.VisualBasic.Constants.vbTab & commentStart & "</summary>")
editPoint.Insert(Microsoft.VisualBasic.Constants.vbCrLf)
editPoint.Insert(Microsoft.VisualBasic.Constants.vbTab & commentStart)
For Each codeElement2 In parameters
codeParameter = codeElement2
editPoint.Insert("<param name=" & codeParameter.Name & "></param>")
editPoint.Insert(Microsoft.VisualBasic.Constants.vbCrLf)
editPoint.Insert(Microsoft.VisualBasic.Constants.vbTab & commentStart)
Next ''//param
''// Do history tag.
editPoint.Insert(Microsoft.VisualBasic.Constants.vbCrLf)
editPoint.LineUp()
editPoint.Insert(Microsoft.VisualBasic.Constants.vbTab & commentStart & "<history>")
editPoint.Insert(Microsoft.VisualBasic.Constants.vbCrLf)
editPoint.Insert(Microsoft.VisualBasic.Constants.vbTab & commentStart & "Name MM/DD/YYYY [Created]")
editPoint.Insert(Microsoft.VisualBasic.Constants.vbCrLf)
editPoint.Insert(Microsoft.VisualBasic.Constants.vbTab & commentStart & "</history>")
editPoint.Insert(Microsoft.VisualBasic.Constants.vbCrLf)
editPoint.Insert(Microsoft.VisualBasic.Constants.vbTab & commentStart)
End If ''//we have a function
Next ''//code elt member
Finally
DTE.UndoContext.Close()
End Try
End Sub
For some reason, after a save, rebuild, and a restart of Visual Studio, I'm not getting the history tag. Can anybody see something here I'm missing?
vb uses a xml file to load the defults. It is VBXMLDoc.xml and it depends on what version you are running as to the location of the file.