Opening a Mail Merge Word Document from an Access Database - ms-access

I currently have a Microsoft Access Database (file format is 2007-2016) that we use to track open orders and print all the required documents. I have created a word document that pulls information from this database using MailMerge. This document works flawlessly by itself. Meaning that if I actually double click the document it opens correctly asking me if I want to pull the information from the database. The issue is that if I open it via a command button in the database I the document opens but I will not get the MailMerge dialog asking if I want to pull the information.
Here is the code I am using at the moment to open the document.
Private Sub Print_Click()
If IsNull(Me.DateShipped.Value) Then
Me.DateShipped = Date
End If
DoCmd.RunCommand acCmdSaveRecord
Dim wrdApp As Word.Application
Dim wrdDoc As Word.Document
Set wrdApp = CreateObject("Word.Application")
wrdApp.Visible = True
Set wrdDoc = wrdApp.Documents.Open("F:\Database\merge\INDIVIDUAL.docx")
Set wrdApp = Nothing
Set wrdDoc = Nothing
DoCmd.RunCommand acCmdSaveRecord
Any help will be appreciated.
Thank you
Joel

Related

Unable to view Navigation and Ribbon in MS Access DB

I've inherited an MS Access project and I'm trying to make changes to some of the forms. However, when I open the file in MS Access 2016, I'm not able to see the navigation menu, top ribbon, or the edit design in order to make any changes. I've searched that there are several ways to bypass this but also found out that each one of them can be disallowed by the developer. If each one can be disallowed, how would another developer make changes?
Below is what I've tried:
Pressed F11 but nothing shows up
Keep SHIFT clicked while double clicking the file but nothing shows up
Pressed CTRL+G to open VBE but nothing shows up
Are there any other ways for me to view edit, navigation, ribbon menus on this access database so that I can make changes to it?
Yes, you can easily bypass such "security" measures using OLE automation.
Use the following code from another Access database (or VBA application)
Public Sub UnlockAccess()
Dim pathToFile As String
pathToFile = "C:\Path\To\My\File.accdb"
Dim db As DAO.Database
Set db = DBEngine.OpenDatabase(pathToFile)
'Set some restrictive properties back to normal
On Error Resume Next
db.Properties!StartUpShowStatusBar = True
db.Properties!AllowFullMenus = True
db.Properties!AllowShortcutMenus = True
db.Properties!AllowBuiltInToolbars = True
db.Properties!AllowSpecialKeys = True
db.Properties!AllowToolbarChanges = True
db.Properties!AllowByPassKey = True
db.Close
On Error GoTo 0
Stop 'You can open up the database using the shift bypass key here, and enable whatever you want'
Dim app As New Access.Application
app.OpenCurrentDatabase pathToFile
app.Visible = True
app.UserControl = True
app.DoCmd.SelectObject acTable, , True
End Sub
An alternate way to modify security is to modify the restrictive VBA code. If you can't open the editor directly from the file, you can open another file, set a reference to the file you want to modify, and modify it from there.

Copy/Paste into Excel using the Exit event

(Currently using Access 2003) Within my project I have a subform, frmSub, that holds the results of a query, located within a form (frmMain).
frmSub is a subform control which contains a query instead of a form (its SourceObject property is set to "Query.qrySearch")
Using various run time counters, the query orders the results with the 'best' at top. The user can then manually select down which ever results they require, or take all the results.
What I'd like to do is provide a way to open the results in Excel (either the user selection or all the results if no selection is made). Ignoring the likely more frequently used 'create temp export .xls file' method for the now, I'm currently looking at a copy/paste method, mimiking the ctl-C/V way the user currently uses.
I have placed an ‘Open in Excel’ button in frmMain.
In the frmSub's Exit event I look at .SelHeight, returning >0 if a selection is made…
If Me.frmSub.Form.SelHeight > 0 Then
DoCmd.RunCommand acCmdCopy
Else:
DoCmd.RunCommand acCmdSelectAllRecords
DoCmd.RunCommand acCmdCopy
End If
Behind the ‘Open in Excel’ button on frmMain I have…
Dim xlApp As Object
Set xlApp = CreateObject("Excel.Application")
With xlApp
.Workbooks.Add
.ActiveSheet.PasteSpecial Format:="Text", Link:=False, DisplayAsIcon:=False
.Cells.Select
.Cells.EntireColumn.AutoFit
.Visible = True
.Range("a1").Select
End With
Set xlApp = Nothing
Call EmptyClipboard
Call EmptyClipboard calls an api based routine that clears the clipboard.
While I realise that using the clipboard this way is likely not best practice, I’m not sure if that thought is universal if a reasonable use could be identified (It’s not expected that the user would be particularly disadvantaged here)
Problem
While the above works well there are issues with using the Exit event since it fires not only on clicking the ‘Exit to Excel’ button – and an Export to Excel, here, causes issues if the clipboard is empty.
Is there a way to determine if it was the ‘Exit to Excel’ button that initiated the sub form’s Exit event – and trap this in that the Exit event’s code? The only events I seem to have access to in frmSub is On Exit and On Enter.
If any copy/paste method would likely remain problematic then I could look at exporting to a tmp .xls file.
With some more time to work on this I’ve rearranged things, I think, for the better. In particular, the Exit event is now only used to store the datasheet’s selection parameters, thus shifting the Copy stage into the ‘Export to Excel’ button…
frmSub Exit event includes…
tmpSelHeight = Me.frmSub.Form.SelHeight
tmpSelTop = Me.frmSub.Form.SelTop
tmpSelLeft = Me.frmSub.Form.SelLeft
tmpSelWidth = Me.frmSub.Form.SelWidth
Behind the ‘Open in Excel’ button on frmMain there is now included…
If IsProcessRunning("excel.exe") Then
If MsgBox("Excel is currently running: click Yes to view in new Excel instance", vbYesNo, "") = vbNo Then Exit Sub
End If
If tmpSelHeight > 0 Then
Form! frmSub.SetFocus
Me. frmSub.Form.SelHeight = tmpSelHeight
Me. frmSub.Form.SelTop = tmpSelTop
Me. frmSub.Form.SelLeft = tmpSelLeft
Me. frmSub.Form.SelWidth = tmpSelWidth
DoCmd.RunCommand acCmdCopy
Else
Form! frmSub.SetFocus
DoCmd.RunCommand acCmdSelectAllRecords
DoCmd.RunCommand acCmdCopy
End If
Me. frmSub.Form.SelHeight = 0
Dim xlApp As Object
Set xlApp = CreateObject("Excel.Application")
With xlApp
.Visible = True
.Workbooks.Add
.ActiveSheet.PasteSpecial Format:="Text", Link:=False, DisplayAsIcon:=False
.Cells.Select
.Cells.EntireColumn.AutoFit
.Range("a1").Select
End With
Set xlApp = Nothing
Call EmptyClipboard
AppActivate "Microsoft Excel"
While things appear stable enough…
I’m still manipulating the clipboard though this is not expected to be an issue.
I could later look at using any existing Excel instance.
I could still consider exporting to a .xls file, or store in temp table & linking table to Excel, as alternative methods.

Display alerts are not enabling in excel file from access vba

I'm writing contents of tables from access to excel(Opening the excel file from access) After writing it i'm trying to save all workbooks in the appXL application. For that i'm making DispalyAlerts false before saving and trun on back after saving. After writing contents to excel i'm closing the access. After writing ,when i'm trying to close the excel, it is not giving any alerts like Do you want to save the contents?
My vba code
Sub Writexl()
Dim appXL As Excel.Application
Dim wb As Excel.Workbook
StrwbPath="C:\temp\sample.xls"
Set appXL = CreateObject("Excel.Application")
With appXL
Set wb = .Workbooks.Open(StrwbPath)
.Visible = True
End With
'here code for writing contents
'save workbook after writing
appXL.Application.DisplayAlerts = False
For Each w In appXL.Application.Workbooks
w.Save
Next w
appXL.Application.DisplayAlerts = True
DoCmd.Quit acQuitSaveAll
Application.Quit
End sub
You are setting the DisplayAlerts to the Application's Applicaiton
appXL.Application.DisplayAlerts = True
is conceptually equivalent to
Excel.Application.Application.DisplayAlerts = True
So the property is being set on the parent application to Excel.
Try
appXL.DisplayAlerts = True
After writing ,when i'm trying to close the excel, it is not giving
any alerts like Do you want to save the contents?
As #David Zemens says, why would it display a message to save all changes when you've already saved everything. Have you tried changing a cell and then exiting Excel?

Using Windows Explorer to get path(string) of a file to store in a table

I currently have a database set up to store the paths of pictures associated with my data and display them in imageframes with VBA. It works great, but the process of adding a picture is a bit tedious, and users struggle to use it correctly(type the wrong path, forget to include the extension, etc). This results in a bunch of garbage entries in the database. I would like to simplify this process. Ideally, when "add picture" is clicked, I would like for it to open up windows explorer, have the user select the desired picture, get the path of that picture, and insert it into the table. Again, I'm not using an OLE, just a text field for the path. Is this possible?
As Remou referenced, the FileDialog object can be used to accomplish this. For many people, it may be necessary to add references to the MS Office Object Library(The Access Library is not sufficient). The code that I used to collect a path name from a file selected in explorer is as follows:
Public Sub ShowFileDialog()
Dim dlgOpen As FileDialog
Set dlgOpen = Application.FileDialog(msoFileDialogOpen)
With dlgOpen
.AllowMultiSelect = False
.InitialFileName = "Z:\" 'Initial Path when explorer is opened
.Show
If .SelectedItems.Count = 0 Then
MsgBox ("No file Selected") 'No file selected
Else
Me.txtPath = .SelectedItems(1) 'sets textbox on the form to the path selected
End If
End With
End Sub

Access query to excel 2010 to create a graph via vba

After an extensive search I have been unable to find any information on this that I could understand. there are numerous examples, but these are all for access 2003, and these do not work in access 2010.
I need to run a vba code that will export the results of a query (QryTotalSale) to excel 2010 and automatically create a bar chart of the data and show this over the database that is running.
If anyone could give me some advise then I would greatly appreciate it, or even a link to a valid resource that will work in 2010.
So far I can get excel to open, and display the results of the query in question.
To make it more difficult I really need the query to open a specific excel file, which will be on a folder on the desktop and every time the button is pressed to run the VBA, a new page should be added to the excel workbook, and the new graph be shown, and saved into the spreadsheet, so that at a later date the entire excel file can be viewed.
I have the below code, but it does not work. the bit about ranges would work in excel, but access does not seem to recognise range ( which does not really suprise me as it does not really work with ranges to my knowledge.)
My second thought was to have the first two doCmd's run, then have the next bit be forced to auto run in the excel file.
Private Sub SalesImage_Click()
DoCmd.OpenQuery "QryTotalSale"
DoCmd.RunCommand acCmdOutputToExcel
Dim myRange as range
Set myRange = B2 * C30
Charts.Add
ActiveChart.ChartType = xlColumnClustered
ActiveChart.SetSourceData Source:=myRange, _
PlotBy:=xlColumns
ActiveChart.Location Where:=xlLocationAsNewSheet
End Sub
I found a adodb code type thing for the 2003 versions of access and excel, but could not get this to work. half of the options no longer seem to be recognised by access...
I am a long way off and would really appreciate any help.
Thanks
Sam
Here are some notes. I have used late binding, so you do not need to set a reference to the Excel library, however, I have included notes on the types.
Dim xl As Object ''Excel.Application
Dim wb As Object ''Excel.Workbook
Dim ws As Object ''Excel.Worksheet
Dim ch As Object ''Excel.Chart
Dim myRange As Object
Set xl = CreateObject("Excel.Application")
sExcelWB = "z:\docs\testchart.xls"
''This will overwrite any previous run of this query to this workbook
DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9, "Query1", _
sExcelWB, True
Set wb = xl.Workbooks.Open(sExcelWB)
''Sheets are named with the Access query name
Set ws = wb.Sheets("Query1")
Set ch = xl.Charts.Add
ch.ChartType = xlColumnClustered
xl.Visible = True
xl.UserControl = True
''Still not saved