Using VBA inside Access2003/2007.
How to copy the contents of a string variable to the clipboard?
This site recommends a creating a zero length TextBox, copying the string to the TextBox, then running DoCmd.RunCommand acCmdCopy. Ugh. I mean, we may go down the route. But still. Ugh.
While the MS knowledgebase article shows us how to do it but it involves a number of Windows API calls. Yuk.
Are those the only two options?
VB 6 provides a Clipboard object that makes all of this extremely simple and convenient, but unfortunately that's not available from VBA.
If it were me, I'd go the API route. There's no reason to be scared of calling native APIs; the language provides you with the ability to do that for a reason.
However, a simpler alternative is to use the DataObject class, which is part of the Forms library. I would only recommend going this route if you are already using functionality from the Forms library in your app. Adding a reference to this library only to use the clipboard seems a bit silly.
For example, to place some text on the clipboard, you could use the following code:
Dim clipboard As MSForms.DataObject
Set clipboard = New MSForms.DataObject
clipboard.SetText "A string value"
clipboard.PutInClipboard
Or, to copy text from the clipboard into a string variable:
Dim clipboard As MSForms.DataObject
Dim strContents As String
Set clipboard = New MSForms.DataObject
clipboard.GetFromClipboard
strContents = clipboard.GetText
User Leigh Webber on the social.msdn.microsoft.com site posted VBA code implementing an easy-to-use clipboard interface that uses the Windows API:
http://social.msdn.microsoft.com/Forums/en/worddev/thread/ee9e0d28-0f1e-467f-8d1d-1a86b2db2878
You can get Leigh Webber's source code here
If this link doesn't go through, search for "A clipboard object for VBA" in the Office Dev Center > Microsoft Office for Developers Forums > Word for Developers section.
I created the two classes, ran his test cases, and it worked perfectly inside Outlook 2007 SP3 32-bit VBA under Windows 7 64-bit. It will most likely work for Access.
Tip: To rename classes, select the class in the VBA 'Project' window, then click 'View' on the menu bar and click 'Properties Window' (or just hit F4).
With his classes, this is what it takes to copy to/from the clipboard:
Dim myClipboard As New vbaClipboard ' Create clipboard
' Copy text to clipboard as ClipboardFormat TEXT (CF_TEXT)
myClipboard.SetClipboardText "Text to put in clipboard", "CF_TEXT"
' Retrieve clipboard text in CF_TEXT format (CF_TEXT = 1)
mytxt = myClipboard.GetClipboardText(1)
He also provides other functions for manipulating the clipboard.
It also overcomes 32KB MSForms_DataObject.SetText limitation - the main reason why SetText often fails. However, bear in mind that, unfortunatelly, I haven't found a reference on Microsoft recognizing this limitation.
-Jim
I couldn't figure out how to use the API using the first Google results. Fortunately a thread somewhere pointed me to this link:
http://access.mvps.org/access/api/api0049.htm
Which works nicely. :)
Easy TWO line code:
It's not so complicated, I don't understand why all solutions found in the net are so complicated.
Sub StoreData()
Set objCP = CreateObject("HtmlFile")
objCP.ParentWindow.ClipboardData.SetData "text", "Some text for clipboard"
End Sub
There are many examples listed here but none seem to cover the direct way of using the API that also works with Access and Excel 32 bit and 64 bit.
I don't want to steal anyone else's work so I'm pointing to an article that has a solution.
https://stackoverflow.com/a/35512118/1898524
Following up on David's idea, if you want to pass in an argument, it has to be double-quoted.
Public Sub SetClipboardText(ByVal Text As String)
Dim QuotedText As String
QuotedText = """" & Text & """"
Set HtmlFileObject = CreateObject("HtmlFile")
HtmlFileObject.ParentWindow.ClipboardData.SetData "text", Eval(QuotedText)
End Sub
Related
Problem : I am trying to automate the saving of a file (manage to save the file when the below IE bar appears at the bottom of the page)
My code is going on my intranet, clicking here and there and then I click on an "Export" button which will trigger this from IE :
I didn't manage to find a way to automate the saving of the file because the only way (I think) to interact with this "window" is to use SendKeys.
In order to do so I have to "activate" this window (Yes, I have it activated for the HTML scraping with this bit of code, but it's not the active window though):
I tried using AppActivate but for some reason it doesn't work.
There are 2 options to pass this obstacle :
Find a way to activate the IE window containing that saving bar so that I can use Application.SendKeys "%{S}" on it
(second option only) : Disable this saving bar or manage to have it open in a new window
So far I have gone through lots of posts talking about that subject but none gave an operational solution for that issue on IE 11.
Let me know if you want to see any bit of code, I have a lot of different attempts gathered from different posts but this would highly increase the length of this post.
Front-end automation is a tricky business. It can be really hard to find a solution that always works (those pesky users are free to move the mouse and click buttons at will, disrupting your code). Because you are taking the data from an intranet site, this suggests the data you need already exists within your organisation. If at all possible (and I know it isn't always) take the data from the servers/source systems, rather than via the UI.
AppActivate and SendKeys can be very fussy. Because the url is always the same a better approach would be to directly download it. Here is a example, based on another answer.
Before you can run code you will need to add two references:
Microsoft XML, v6.0
Microsoft ActiveX Data Objects 2.8 Library
From the VBA IDE click Tools >> References... and select from the list (the original answer does not use references, instead it uses late binding).
' Downloads a file from a given URL.
' URL String URL to get file. Ex: http:\\MySite\MyFile.
' Path String Location to save file. Ex: C:\Myfile.csv.
' Requires: Microsoft XML, v6.0
' Requires: Microsoft ActiveX Data Objects 2.8 Library.
Sub DownloadURL(ByVal URL As String, ByVal Path As String)
Dim httpXML As XMLHTTP60 ' Used to download data.
Dim st As ADODB.Stream ' Used to write downloaded data to file system.
Set httpXML = New XMLHTTP60
' Request file.
httpXML.Open "GET", URL, False
httpXML.send
' Download response.
If httpXML.Status = 200 And httpXML.readyState = 4 Then
Set st = New ADODB.Stream
st.Open
st.Type = adTypeBinary
st.Write httpXML.responseBody
st.SaveToFile Path, adSaveCreateOverWrite
st.Close
Else
MsgBox "Something went wrong :(", vbCritical, "An error occured"
End If
End Sub
How do I programmatically print source code in a VB6 Addin..? There are no print or preview methods that I can find for VBIDE in the Object Browser.
I've searched high & low on Google, and there's a strange lack of information on VBIDE code module printing. I get lots of hits for PrettyPrint, but that's all. The lack is so great that it makes me wonder if there's some fundamental concept that I'm completely missing.
I scared up a copy of the O'Reilly book mentioned by Herb in https://stackoverflow.com/a/41034211/2705042, and it makes no mention of the printing of source code. The only way I can see is to export the code to text files and print those through usual means unrelated to VBIDE.
I also checked Chip Pearson's guide to VBE at http://www.cpearson.com/excel/vbe.aspx, which is almost identical to VBIDE, and even there is no clue to printing of code, other than the idea I mentioned of saving to text files and then printing.
** Ideally, I'd like to use the existing VB6 File > Print dialog, with one extra checkbox added to it. I realize the addition of controls to an existing dialog is another topic, and I'm not averse to creating my own version of the print dialog.
It is possible with a CommandBarButton proxy and SendKeys.
Getting a handle to the Print CommandBarControl is simple enough, but pressing the button throws a dialog in your way, so we have to use SendKeys to set the options and submit the form....
You can use code similar to the following:
Dim printCommand As CommandBarControl
Set printCommand = Application.VBE.CommandBars.FindControl(ID:=4)
printCommand.Execute
'Yep, SendKeys, erghhh
Application.SendKeys "P" 'Force the whole project to print
Application.SendKeys "{ENTER}"
I would like to open my video using VBS and So far I have this and it works fine
Set wmp = CreateObject("WMPlayer.OCX")
wmp.openPlayer("C:\Users\myvideo.mp4")
I would also like to add one more line to make it full screen. How would one do that?
Also, my second question is probably more useful.
Is there a collective website to describe how to manipulate these objects?
When I played around with Matlab, I can at least press tab and see what can options are available under that object and I learned a lot that way (the website was also very helpful).
Is there a comparable way in VBS or for example, where can I find a extensive lists of command I could send to WM Player?
Thanks!
For switching the window to a full screen window you have to use Alt+Enter. This can be done using sendKeys method of the Shell object. Here's the code:
set WshShell = WScript.CreateObject("WScript.Shell")
WScript.Sleep 1000
WshShell.SendKeys "%{ENTER}"
Hope this suits you need. For further help check the documentation for SendKeys.
I'm updating old VB6 code to save its DataReports out to a PDF, rather than bringing up a print dialog.
I cannot simply write the PDF within the code (using a VB6 PDF library, etc.), since all our software already uses DataReports, and writing print code for each one would be tedious, at best. Currently, the process requires an employee to print the DataReport to a PDF print driver, naming it manually and saving it to where it needs to go. I need to automate this all, so that the name and location of the saved PDF can be specified programatically, rather than entered by hand.
The best solution would be if DataReport simply had a .SaveToPdf(filename) routine. Worst-case scenario, I see myself automating the process using SendKeys. However, this solution needs to work in an environment with multiple possible printers (so the PDF print driver might not be the default,) and on Windows XP, Vista, or 7.
I've fruitlessly tried Googling the problem, which returns only tutorials on how to do it by hand, much as we do now.
You might consider using a PDF Printer Driver that allows you to configure silent "printing" to a preset directory using auto-generated names.
For an example of such a product, see:
http://www.iteksoft.com/modules.php?op=modload&name=Sections&file=index&req=viewarticle&artid=21
I would create a dialog that lets the user enter the printer (driver) name, directory to save to, and a file naming guideline, then save that to either a local ini file or the registry. You would then need two print buttons / menus. One to print straight to the printer using the default (saved) settings, and one that opens the print window they see now so they can perform a custom print.
Remember an ellipsis on a menu item indicates additional dialogs, Print vs Print...
Just use Crystal Report Viewer Control and follow the steps:
Set objRpt = objApp.OpenReport("type report path and name")
objRpt.DiscardSavedData
dim filepath as string
filepath = report path & report filename
With objRpt
.ExportOptions.FormatType = crEFTPortableDocFormat
.ExportOptions.DestinationType = crEDTDiskFile
.ExportOptions.DiskFileName = 'filepath string goes here
.ExportOptions.PDFExportAllPages = True
.Export False
End With
Follow these steps and the export is done.
I've found a few tutorials that explain how to use the windows API to get a custom icon in the system tray.
These are all for Visual Basic, and they don't seem to be scaling to VBA well.
I'm following this short tutorial:
http://atchoo.org/vb/systray.php
Basically, you have to set the hIcon value (a 'long' variable) but it does not work.
I've tried to use the LoadPicture() function, which does not give me any errors, but also fails to add a new icon.
I can't supply Me.Icon, nor can I set it on Form_Load.
Does anyone have any experience with this?
Using loadpicture was the right approach, but not directly.
I had to define a new variable first, and load that.
Like this:
Dim myPicture As IPictureDisp
strPath = "F:\Databank\Icons\stone.ico"
Set myPicture = LoadPicture(strPath)
And then, somewhere along the way, I could set hIcon without problems:
.hIcon = myPicture
When I change the trayicon (like, say, adding a balloontip) I have to supply the icon information again, too.