473,413 Members | 1,795 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,413 software developers and data experts.

any idea about VBA code that open, writes data in EXCEL/WORD file???

overcomer
Hi,

My problem is i dont know the syntax for opening/writing data/closing excel/word file via vba ms access.. If you know links or sites that could be helpful...

thanks so much...
Nov 11 '08 #1
2 1738
MrDeej
157 100+
Here is code to write data from query and into Excel

Expand|Select|Wrap|Line Numbers
  1. Function sql_til_excel(sqlString As String, navn As String)
  2.  
  3.  
  4. Call Opprett_Spørring("Excel_" & navn & "_liste", sqlString)
  5. Call overfør_spørring_til_excel("Excel_" & navn & "_liste", "Excel_" & navn & "_liste")
  6. Call Slett_temp_spørring("Excel_" & navn & "_liste")
  7.  
  8.  
  9. End Function
  10. Function Opprett_Spørring(Spørringnavn As String, strsql As String)
  11.  
  12. Dim NtLogin As String
  13.  
  14. NtLogin = Environ("Username")
  15.  
  16.   Dim dbs As Database
  17.   Dim strQueryName As String
  18.   Dim qryDef As QueryDef
  19.  
  20.  
  21.   Set dbs = CurrentDb
  22.   strQueryName = "tempqry " & NtLogin & " " & Spørringnavn
  23.  
  24.   Dim q As QueryDef
  25.  
  26. For Each q In dbs.QueryDefs
  27.  
  28.     If q.Name = strQueryName Then
  29.     dbs.QueryDefs.Delete strQueryName
  30.     End If
  31.  
  32. Next
  33.  
  34. Set qryDef = dbs.CreateQueryDef(strQueryName, strsql)
  35.  
  36.  
  37. End Function
  38.  
  39.  
  40. Function Slett_temp_spørring(Spørringnavn As String)
  41. Dim NtLogin As String
  42.  
  43. NtLogin = Environ("Username")
  44.  
  45. DoCmd.DeleteObject acQuery, "tempqry " & NtLogin & " " & Spørringnavn
  46.  
  47. End Function
  48.  
  49.  
  50. Public Function overfør_spørring_til_excel(Template As String, Optional Tempspørring As String, Optional Spørring As String) As String
  51. Dim NtLogin As String
  52.  
  53. NtLogin = Environ("Username")
  54.  
  55.  
  56. Dim appExcel As Excel.Application
  57.    Dim wbk As Excel.Workbook
  58.    Dim wks As Excel.Worksheet
  59.  
  60.    Dim sTemplate As String
  61.    Dim sTempFile As String
  62.    Dim Info As String
  63.  
  64.    Dim dbs As DAO.Database
  65.    Dim rst As DAO.Recordset
  66.    Dim sSql As String
  67.    Dim lRecords As Long
  68.    Dim iRow As Integer
  69.    Dim iCol As Integer
  70.    Dim iFld As Integer
  71.    Dim sOutPut As String
  72.    Const cTabTwo As Byte = 2
  73.    Const cStartRow As Byte = 4
  74.    Const cStartColumn As Byte = 3
  75.  
  76. sOutPut = ahtCommonFileOpenSave(, "Skrivebord", , , , Template & " " & Date & ".xls", Template & " " & Date & ".xls", , False)
  77. If sOutPut = "" Then
  78. Exit Function
  79. End If
  80.  
  81.  
  82.  
  83.  
  84.    Application.SetOption "Error Trapping", 0
  85.  
  86.    sTemplate = CurrentProject.Path & "\templ\" & Template & ".xls"
  87.    If Dir(sOutPut) <> "" Then Kill sOutPut
  88.  
  89.    FileCopy sTemplate, sOutPut
  90.  
  91.    Set appExcel = CreateObject("Excel.Application")
  92.    Set wbk = appExcel.Workbooks.Open(sOutPut)
  93.    Set wks = appExcel.Worksheets(1)
  94.  
  95.    If Not Tempspørring = "" Then
  96.     sSql = "SELECT * From [" & "tempqry " & NtLogin & " " & Tempspørring & "]"
  97.     End If
  98.     If Not Spørring = "" Then
  99.     sSql = "SELECT * From [" & Spørring & "]"
  100.    End If
  101.  
  102.    Set dbs = CurrentDb
  103.    Set rst = dbs.OpenRecordset(sSql, dbOpenSnapshot)
  104.    If Not rst.BOF Then rst.MoveFirst
  105.  
  106.    iCol = cStartColumn
  107.    iRow = cStartRow
  108.    rst.MoveLast
  109.    Dim rc As Long
  110.    rc = rst.RecordCount
  111.    rst.MoveFirst
  112.  
  113.  
  114.    If rc > 1000 Then
  115.     svar1 = MsgBox("Du skal til å overføre " & rc & " poster." & vbCrLf & "Vil du fortsette?", vbYesNo, "LogiDose")
  116.         If svar1 = vbNo Then
  117.         On Error Resume Next
  118.         Set wks = Nothing
  119.         Set wbk = Nothing
  120.         appExcel.Quit
  121.         Set appExcel = Nothing
  122.         Set rst = Nothing
  123.         Set dbs = Nothing
  124.         DoCmd.Hourglass False
  125.         Exit Function
  126.         End If
  127.    End If
  128.  
  129.  
  130.    Call Fremdriftsindikator("ja", rc, "Initierer exceloverføring")
  131.    Do Until rst.EOF
  132.       iFld = 0
  133.       lRecords = lRecords + 1
  134.       Call Fremdriftsindikator(, , "Overfører post nr " & lRecords & " av " & rc, , 1)
  135.       For iCol = cStartColumn To cStartColumn + (rst.Fields.Count - 1)
  136.          wks.Cells(iRow, iCol) = rst.Fields(iFld)
  137.  
  138.          If InStr(1, rst.Fields(iFld).Name, "Date") > 0 Then
  139.             wks.Cells(iRow, iCol).NumberFormat = "mm/dd/yyyy"
  140.          End If
  141.  
  142.          wks.Cells(iRow, iCol).WrapText = False
  143.          iFld = iFld + 1
  144.       Next
  145.  
  146.       wks.Rows(iRow).EntireRow.AutoFit
  147.       iRow = iRow + 1
  148.       rst.MoveNext
  149.    Loop
  150.  
  151. Call Fremdriftsindikator(, , , "ja")
  152.    Application.FollowHyperlink sOutPut
  153.  
  154.  
  155. exit_Here:
  156.    On Error Resume Next
  157.    Set wks = Nothing
  158.    Set wbk = Nothing
  159.    'appExcel.Quit
  160.    Set appExcel = Nothing
  161.    Set rst = Nothing
  162.    Set dbs = Nothing
  163.    DoCmd.Hourglass False
  164.  
  165. End Function
You will also need this module:
Expand|Select|Wrap|Line Numbers
  1. Option Compare Database
  2. Global GL_UserName As String
  3. Global GBL_Access_Level As String
  4. Public Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
  5.  
  6. Public Function init_globals()
  7. GBL_Access_Level = "Ingen"
  8. End Function
  9.  
  10. Public Function Get_Global(gbl_parm)
  11. Select Case gbl_parm
  12.     Case "GBL_Access_Level"
  13.         Get_Global = GBL_Access_Level
  14. End Select
  15. End Function
  16.  
  17.  
  18.  

The function fremdriftsindikator i use for a form with progressbar. This you should just delete or add the code for the progress bar form

Expand|Select|Wrap|Line Numbers
  1. Public Function Fremdriftsindikator_v2(Optional åpne As String, Optional maxhoved As Long, Optional maxdel As Long, Optional etiketthoved As String, Optional etikettdel As String, Optional lukk As String, Optional plushoved As Long, Optional plusdel As Long)
  2.  
  3. If åpne = "ja" Then
  4. If Not CurrentProject.AllForms("MAIN progressbar v2").IsLoaded Then
  5. DoCmd.OpenForm "MAIN progressbar v2"
  6. End If
  7. End If
  8.  
  9.  
  10. If Not maxhoved = 0 Then
  11. [Form_MAIN Progressbar v2].ProgressBar_hoved.max = maxhoved
  12. End If
  13.  
  14. If Not maxdel = 0 Then
  15. [Form_MAIN Progressbar v2].Progressbar_del.max = maxdel
  16. End If
  17.  
  18. If Not etiketthoved = "" Then
  19. [Form_MAIN Progressbar v2].etk_hoved.Caption = etiketthoved
  20. End If
  21.  
  22. If Not etikettdel = "" Then
  23. [Form_MAIN Progressbar v2].etk_del.Caption = etikettdel
  24. End If
  25.  
  26.     Dim val As Integer
  27.  
  28. If Not plushoved = 0 Then
  29.  
  30.     val = [Form_MAIN Progressbar v2].ProgressBar_hoved.Value + plushoved
  31.     If val > [Form_MAIN Progressbar v2].ProgressBar_hoved.max Then
  32.         val = [Form_MAIN Progressbar v2].ProgressBar_hoved.max
  33.     End If
  34.  
  35.     [Form_MAIN Progressbar v2].ProgressBar_hoved.Value = val
  36. End If
  37.  
  38. If Not plusdel = 0 Then
  39.     val = plusdel
  40.     If val > [Form_MAIN Progressbar v2].Progressbar_del.max Then
  41.         val = [Form_MAIN Progressbar v2].Progressbar_del.max
  42.     End If
  43.  
  44.     [Form_MAIN Progressbar v2].Progressbar_del.Value = val
  45. End If
  46.  
  47. [Form_MAIN Progressbar v2].Repaint
  48. If lukk = "ja" Then
  49. DoCmd.Close acForm, "MAIN progressbar v2"
  50. End If
  51. End Function
  52.  
Nov 11 '08 #2
thank u so much... ^_^
Nov 11 '08 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

8
by: Sandy Norton | last post by:
Hi folks, I have been mulling over an idea for a very simple python-based personal document management system. The source of this possible solution is the following typical problem: I...
8
by: gremlinbass | last post by:
I was wondering if I can check to see if a file is open? Specifically, is there any way I can code Access to check to see if a text file (datasource.txt to be specific) is open, returning a true...
1
by: | last post by:
The following code: Private Sub ClearControls(ByVal ctrl As Control) Dim i As Int32 For i = ctrl.Controls.Count - 1 To 0 Step -1 ClearControls(ctrl.Controls(i))
4
by: Simon Cheng | last post by:
Hi, How do I open a Word or Excel document inside an event handler (e.g., Page_Load())? Thanks, Simon
0
by: Microsoft | last post by:
I have a porgram that opens an excel workbook and modifies some data, but almost randomly a box will pop up in the workbook telling me the file is now available and to hit ok or cancel. This...
6
by: Syvman | last post by:
Here's what I've got: I'm trying to grab some data out of an Excel spreadsheet and bring it into Access. I'm able to do it, but only if the Excel spreadsheet is not opened by any other users. I...
1
by: webgirl | last post by:
Hi everyone, I have a weird problem with some Word/Excel automation code that I run from Access (not sure if I should therefore post this in the Access forum..? Thought I'd try here first) ...
11
by: John | last post by:
Access 2003 After my app is finished I'd like to create an mde file. Therefore my db has to be made in the 2002-2003 format. So I've created a new mdb (default in 2000 format), I converted this...
4
NeoPa
by: NeoPa | last post by:
Introduction: Macro Security Levels in MS Office applications are recommended to be set to High. This stops any VBA code associated with a project from running, unless it is signed (with a...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.