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.
Related
I think the answer to this will be "no" but it doesn't hurt to ask - and get people thinking.
I'd like to be able to programmatically add VBA as a text string to a PowerPoint presentation I'm creating with python-pptx.
I know I can confect the XML to create a button that, when clicked, runs a macro that already exists in the .pptm file.
Because the macro is stored compiled I don't think I can inject it as text programmatically. (I checked the format of a .pptm file with a "Hello World" macro in and there is an extra file in it with non-text material in.)
I also don't know if I can write a .pptm (macro-enabled .pptx) file with python-pptx.
So, Has anyone experimented with this?
The best I think I might be able to do - in md2pptx - is allow the user to add buttons that call pre-existing macros in the template file when clicked on. Unless the user builds their own template file off one I supply I don't think that enables me to add general function (such as deleting the first slide or note taking).
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}"
We have an in house button control, and quite frankly it sucks. I'd like to replace it but I don't want to go onto every form in our project and delete/add a new control. It seems to me that if I design a new button that has all the same properties as the old one then I ought to be able to give it the same name as the old one and just replace all the reference lines in the vbp files to point to the new control.
Has anyone tried this (better yet have you heard of a tool that will do it for you) and if so what 'gotchas' should I be on the look out for?
Thanks!
The *.vbp files are one place you'll need to change. There are also references to the used control libraries in the files containing GUIs -- that's form (*.frm), control (*.ctl), and property page (*.pag) files. These files are in a plain text format and you can see the references at the top. They look like this:
Object = "{831FDD16-0C5C-11D2-A9FC-0000F8754DA1}#2.0#0"; "mscomctl.ocx"
Those refs will need to be added or updated in all relevant files if the new control is a compiled OCX. If it's in the same project I don't think it needs any reference there, and if it's in a different project in the same project group I'm not sure. Save a test form with the new control to see.
Note that you don't have to keep the same control class name. Inside the *.frm/ctl/pag files, instances of individual controls on them are represented by a simple format like this:
Begin VB.CommandButton Command2
Caption = "Cancel"
Height = 375
Left = 2460
TabIndex = 1
Top = 2400
Width = 1455
End
The syntax of the first line there is "Begin LibraryOrProjectName.ClassName NameOfThisInstance". So, provided the offending control's name is distinctive it should be easy to search & replace references to it both in the BASIC source and in the GUI layouts. You might want a plain text editor that can perform search and replace across multiple files (Notepad++ is one).
Some control properties are stored like this:
Picture = "frmMain.frx":292F
These correspond to the *.frx, *.ctx, and *.pgx files, which contain binary data for the values of certain control properties. I don't think these files should need altering or cause any problems. They don't appear to contain control names.
Use a full compile (Ctrl+F5) to be sure no problems linger in parts of the source afterward.
Never tried it. Good luck.
There is only one tip to be added to the accepted answer.
If you need to replace any generic VB control with 3rd party or custom ActiveX control you must replace the:
BeginProperty Font
with
BeginProperty Font {0BE35203-8F91-11CE-9DE3-00AA004BB851}
Omiting to do so results with run-time error 713 when trying to edit/open the form.
If there is no BeginProperty statement in the block then control uses default font and this replacement is not needed.
An additional scenario to look for is if the classes in the OCX are referenced directly in code.
In other words, if the control class was ABCButton then you need to look for ABCButton in all .BAS and .CLS files as well, and make appropriate changes.
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