What is the Vision VBA equivalent to Access' Application object? - vba

In Access VBA, you can access properties/methods of the running application by typing the word Application, followed by a period, followed by what you want.
For example, I can instantiate and show a FileDialog like so:
Dim openFileDialogue As Object
Set openFileDialogue = Application.FileDialog(3)
openFileDialogue.Show
If I wanted to do this in Visio VBA, what would I need to substitute Application for in the above code?
Thanks

Try this link: http://visguy.com/vgforum/index.php?topic=738.0
At the very bottom is a code sample from "Nikolay" that shows an alternative way of getting the Fi
It shows an alternative (though more complex) way of getting the File Dialog to show up in Visio

Related

Including source files at compile time with VB.NET and Visual Studio

As usual, I do my research in the various forums. My personal discipline is that if I have struggled for more than a day, I need to reach out for help. Because after a day, I can no longer see the wood because of the trees.
My web site has some code written in VB.NET which I use on many pages of the web site. Where possible, I try to write code only once. For obvious reasons. I include the code in a class.
However, when I use a class, I cannot access the page controls. For example, the HTTP context is available on the VB.NET code-behind but not in the class declared in the code-behind. Is there some way of achieving this result?
Another option is to have VB.NET code in a separate source file. And to include this source file into the main source code file at compile time. If I need to make a change, I can do it in the source code
“segment” once only. Right now, if I want to make a global change, I have to cut and paste through many web pages. I have read that this type of “include” was possible in VB prior to VB.NET. I can find no reference to this for VB.NET in Visual Studio. Any suggestions?
For example if this code is to be run on every web page, how would you set it up so that you only have one source? That is called on 150 pages?
Protected Sub getCookieVariable()
'
Dim myCookieName As String
Dim myCookieValue As String
'
myCookieName = "hfl3"
Dim myCookie As HttpCookie = HttpContext.Current.Request.Cookies(myCookieName)
If Request.Cookies(myCookieName) IsNot Nothing Then
myCookieValue = myCookie.Value
myCookie.Expires = DateTime.Now.AddDays(30)
Response.Cookies.Add(myCookie)
session("myCookieValue") = myCookieValue
End If
'
End Sub

How do I add a User Defined Function to Excel?

I have a simple stored procedure that returns a Description and a Name when you give it a ID. I need to enable this inline in multiple Excel Sheets. Something like =ItemLookup('12345') that would then return the aforementioned info.
I have not done a lot with Excel programming and am simply wondering what my options are for tackling this. Is this a VBA thing or should this be an external DLL that I COM register? Both felt like overkill but then I realized I had no idea if they were. I really wanted to use VSTO for this but it sounds like that is not possible for Cell level UDF's without having to modifiy each Workbook with some VBA.
The best way to add UDF functions to Excel is with Excel-DNA (which is a free, open-source library I develop), and any of the .NET languages - VB.NET, C# and F# are all fine.
To get started you make a new 'Class Library' project in Visual Studio (any edition), install the 'Excel-DNA' package from the NuGet package manager, and add your code:
Public Module MyDataAccessFunctions
<ExcelFunction(Description:="Gets the Item from the database")>
Public Function ItemLookup(code As String) As String
' Here you have to do some work to get the data
Return "Hello " & code
End Function
End Module
Pressing F5 builds and starts Excel, and you're done - try putting =ItemLookup("Paladin") into a cell.
The resulting add-in is a single .xll file, which you can copy and use on any machine that has .NET without any installation or admin permissions. It works with old Excel versions too.
The best place for support (including absolute beginners' questions) is the Excel-DNA Google group.
You can use Excel to create a VBA UDF pretty easily, just hit alt+f8, right click your project in the project hierarchy on the left of the screen, and click add module.
Here is a quick Hello World function you can just paste into the module, then click play (or alt+f8 from worksheets)
sub test()
msgbox "helloworld"
end sub
If it was me, I would probably just create a list of the file paths that need to be searched. Then create a VBA macro that opens them in excel, searches them for the key, and returns other information from the row the key was found on.
You can open files with the 'Application.Open' method, simply pass in
the file path as an argument. 'Application.Open' returns a workbook
object.
Each workbook will have several worksheets, you can access them
through the workbook's 'Worksheet' property
Getting each used cell in a workbook can be done via looping through the 'UsedRange'
property in each worksheet
Get the value of a cell for comparison from the cell's 'value' property
Cells also have a 'row' property so you can find other items on the same row
If you're used to VBA you could get this running in less than an hour. But since you're just starting out it'll probably take a 3+ hours since you'll have more research/debugging

Get clipboard into Watch window when Debugging VBA

I'm running a macro on a word document.
After doing a Selection.Copy and running on, while debugging, I want to be able to see what was actually copied, and if it was mistakenly replaced.
Is there a way to "watch" Windows' clipboard?
I found a way. At the beginning of my code:
Dim obj As New DataObject
obj.GetFromClipboard
I needed to reference Microsoft Forms 2.0 Object Library to have the "DataObject".
At first I couldn't find it on the list - had to browse for FM20.dll.
The second line could be typed on need in immediate window, instead of writing it in the code. I found it easier this way.
Then, in the watch window, I put obj.GetText as a watch expression and voilà!
clipbrd.exe is the Windows clipboard viewer.

Accessing Excel Object without using Shape.Activate() on a Word document using VBA

I have a tried reading an embedded excel document in a word document. I followed the code specified at this blog article:
http://vbadud.blogspot.com/2010/08/how-to-read-excel-sheet-embedded-in.html
Dim oWB As Excel.Workbook
Dim oIShape As InlineShape
For Each oIShape In ActiveDocument.InlineShapes
If InStr(1, oIShape.OLEFormat.ProgID, "Excel") Then
oIShape.OLEFormat.Activate
Set oWB = oIShape.OLEFormat.Object
oWB.Sheets(1).Range("A1").Value = "ProdID"
End If
Next oIShape
It works fine but the Activate line causes the document to flicker on each excel document I read. I tried to remove the oIShape.OLEFormat.Activate code but it causes the next line to throw a "Runtime error '430' (class does not support Automation or does not support expect).
The question is there any other way to access embedded excel without calling the Activate method?
This is tricky! The short answer is, no. Not with an embedded Excel.
I did some experimentation and some research. Since I could not find any sources that specifically explained the behavior. this is somewhat a guess on my part. It appears that when you embed the Excel spreadsheet into your word document essentially Word stores a link of spreadsheet, which displays only the appearance because it needs to be interpreted with the Excel program. Until you actually active the shape, you cannot interact with it because that cannot be done with Word directly. This article alludes to the behavior, but doesn't explain it. Here's a quote:
If you edit the object in Word, click anywhere outside the object to return
to the destination file.
If you edit the object in the source program in a separate window,
click Exit on the File menu of the source program to return to the
destination file.
You may have noticed that even if you use. Application.ScreenUpdating = false it still does the flickering you mention. This is because you are using a different application when you access the shapes! Every time you active the shape, the object specific menus etc load.
A possible work around:
If instead of embedding Excel Spreadsheets via the insert menu, you can instead add a control. On my machine using Office 2003 the comparible one is: Microsoft Office Spreadsheet 11.0 This is technically a web control, but the methods and behavior are very comparable to an Excel workbook.
Using the control instead of the handy inserted object, with a slight variation of your code I was able to comment out your activate command and the code ran as expected. Specifically, I had to change these lines:
Dim oWB As Spreadsheet instead of Excel.Workbook.
If InStr(1, oIShape.OLEFormat.ProgID, "OWC11.Spreadsheet.11") Then instead of "Excel"
Basically you can decide... Activate your embedded object that requires Excel to interpret, or use a different control that doesn't require activation.

Is there a way to get full IntelliSense for VBA in Access and Excel 2007?

In VB.NET, the IntelliSense pops up as soon as you start typing which gives you a pretty full list of things you can use at that moment. The IntelliSense in VBA however, doesn't kick on for me until a period is put after the part you're using. For example, I go into the VBA editor in Excel 2007 and start typing the word "Range" but the IntelliSense doesn't come up until I type "Range." after which it will give me a list of things I can use at that point.
Is there any way to make IntelliSense come up sooner in the VBA editor, so that I'm able to see a list of things that can be used like Davg, DCount, etc?
Just press Ctrl + Space at any time in the editor.
No, but you can type Excel first. Put a period after that, and you'll find out more than you ever wanted to know about Excel.
Same with Access. Type Access and a period, and you will find DCount there.
Unfortunately in Excel some objects are declared 'As Object' and Object doesn't have any methods so Intellisense won't be able to display any. The main culprit being Excel.ActiveSheet.
If you know what the type actually is then you can declare it explicitly and assign it to the value you want. EG:
Dim mySheet As Worksheet
Set mySheet = ActiveSheet 'This line would cause a type mismatch error if mySheet was declared as something other than a WorkSheet
mysheet.[All The Intellisense For a Worksheet Pops Up]
VBA is a different beast than .NET so I'm not sure how to bring up the IntelliSense quicker. I find I have the same problem you're having in 2003.
I would suggest checking out the Object Browser though. In 2003, it's View > Object Browser, or F2, in the VB Editor. I find it's a great way to explore the class libraries available. It will show you everything that you currently have referenced and once you reference more libraries, they will also show up in the Object Browser.