Access change memo field from "plain text" to "rich text" using VBScript - vba

i have a question about changing a memofield from "plain text" to "rich text" using VBScript, i found some solutions here and on the internet but all the solutions are for VBScript within access. I try to start an vbscript through Windows, but my script doesn't work. I'm kindly new to VBScripting so i hope you guys can help me. I used an example from the forum for my script:
How to convert a text field in an Access table to a rich text memo using VBA
My Script:
Dim db
Dim tdf
Dim fld1
Dim fld2
Set accessApp = GetObject("D:\test.mdb")
Set accessApp = CreateObject("Access.Application")
accessApp.OpenCurrentDataBase "D:\test.mdb", true
accessApp.visible = false
accessApp.UserControl = true
Set accessApp.db = CurrentDB
Set accessApp.tdf = db.TableDefs("Database")
Set accessApp.fld1 = tdf.Fields("Name_Memofield1")
Set accessApp.fld2 = tdf.Fields("Name_Memofield2")
Debug.Print "acTextFormatPlain: " & acTextFormatPlain & _
"; acTextFormatHTMLRichText: " & acTextFormatHTMLRichText
With fld1.Properties("TextFormat")
Debug.Print "TextFormat: " & .Value
If .Value = acTextFormatPlain Then
.Value = acTextFormatHTMLRichText
Debug.Print "TextFormat changed to: " & .Value
End If
End With
With fld2.Properties("TextFormat")
Debug.Print "TextFormat: " & .Value
If .Value = acTextFormatPlain Then
.Value = acTextFormatHTMLRichText
Debug.Print "TextFormat changed to: " & .Value
End If
End With
The error what occures tells me that the problem is in the "Set accessApp.db = CurrentDB" the error which occured is: "Object doesn't support this prperty or method accessApp.db" If i change "accessApp.db" to "db" an other error occures: "Object required: 'CurrentDB' "

Try something like the code below. It will need tidying.
Option Explicit
Dim accessApp
Dim db
Dim dbname
Dim tdf
Dim fld1
Dim fld2
Dim acTextFormatPlain
Dim acTextFormatHTMLRichText
Dim dbInteger
'acTextFormatPlain=0
'acTextFormatHTMLRichText=1
dbInteger=3
dbname="D:\Test.mdb"
Set accessApp = CreateObject("Access.Application")
accessApp.OpenCurrentDataBase(dbname)
set db=accessapp.CurrentDb
Set tdf = db.TableDefs("2emails")
'The property may not exist
SetFieldProperty tdf.Fields(1), "TextFormat", dbInteger, 0
With tdf.Fields(1).Properties("TextFormat")
If .Value = 0 Then
.Value = 1
msgbox "TextFormat changed to: " & .Value
End If
End With
Sub SetFieldProperty(ByVal fld , ByVal strPropertyName , ByVal iDataType , ByVal vValue )
Dim prp
Set prp = Nothing
On Error Resume Next
Set prp = fld.Properties(strPropertyName)
On Error GoTo 0
If prp Is Nothing Then
Set prp = fld.CreateProperty(strPropertyName, iDataType, vValue)
fld.Properties.Append prp
Else
prp.Value = vValue
End If
End Sub

Related

MS access VBA Field.Properties.Append method fails

I have a form that creates a table. Everything works fine except for one point.
I want one field of the created table to appear as a combobox, so I have to change its DisplayControl property to acComboBox.
As far as I know, the property firstly has to exist. If not, then you have to create it and then append it to the collection.
The problem is that when it comes to append the property it throws a Run-time error '3219': Invalid operation..
Here is the code to this point:
Private Sub bInsert_Click()
Dim accApp As Access.Application
Dim DB As DAO.Database
Dim tbl As DAO.TableDef
Dim fld As DAO.Field
Dim indx As DAO.Index
Dim rst As DAO.Recordset
Dim i As Integer, iFields As Integer
Dim sForm As String, str As String
Dim frm As Access.Form
Dim sCtrl() As String
If Not Application.IsCompiled Then _
Application.RunCommand acCmdCompileAndSaveAllModules
'there is a subform for the fields:
Set rst = Me.subfFields.Form.Recordset
rst.MoveFirst
'completion check:
If IsNull(Me.tName) Then
MsgBox "Insert table name."
Exit Sub
ElseIf rst.AbsolutePosition = -1 Then
MsgBox "Insert at least one field."
Exit Sub
End If
'create a db that will use later:
If Dir(Me.tDB) = "" Then
Set accApp = New Access.Application
accApp.NewCurrentDatabase Me.tDB
accApp.Quit
Set accApp = Nothing
End If
'create Table:
Set DB = Application.CurrentDb
Set tbl = DB.CreateTableDef(Me.tName)
'ID as PK:
Set fld = tbl.CreateField("ID", dbLong)
fld.Attributes = dbAutoIncrField
tbl.Fields.Append fld
Set indx = tbl.CreateIndex("IDindex")
indx.Primary = True
Set fld = indx.CreateField("ID")
indx.Fields.Append fld
tbl.Indexes.Append indx
Set indx = Nothing
Set fld = Nothing
'add rest of the fields:
Do Until rst.EOF
i = Me.subfFields.Form!cType
If i = dbText Then
Set fld = tbl.CreateField(Me.subfFields.Form!tName, i, Nz(Me.subfFields.Form!tSize, 255))
Else
Set fld = tbl.CreateField(Me.subfFields.Form!tName, i)
End If
tbl.Fields.Append fld
If Me.subfFields.Form!cControl = 111 Then
SetDAOProperty fld, "DisplayControl", dbInteger, acComboBox
End If
rst.MoveNext
Loop
End Sub
Sub SetDAOProperty(WhichObject As Field, PropertyName As String, PropertyType As Integer, PropertyValue As Variant)
Dim prp As DAO.Property
On Error GoTo ErrorHandler
WhichObject.Properties(PropertyName) = PropertyValue
WhichObject.Properties.Refresh
Cleanup:
Set prp = Nothing
Exit Sub
ErrorHandler:
Select Case Err.Number
Case 3270 ' "Property not found"
Set prp = WhichObject.CreateProperty(PropertyName, PropertyType, PropertyValue)
'=====================================
'the next line throws the error:
'=====================================
WhichObject.Properties.Append prp
WhichObject.Properties.Refresh
Case Else
MsgBox Err.Number & ": " & Err.Description
End Select
Resume Cleanup
End Sub
Can someone explain what is the problem please? Seems like I'm missing something. Is there some kind of syntax error? My native language isn't English.
So as June7 suggested, appending firstly the table and then modifing the field's properties, worked fine.
Here is the final code in case someone needs it:
'create Table:
Set DB = Application.CurrentDb
Set tbl = DB.CreateTableDef(Me.tName)
'ID as PK:
Set fld = tbl.CreateField("ID", dbLong)
fld.Attributes = dbAutoIncrField
tbl.Fields.Append fld
Set indx = tbl.CreateIndex("IDindex")
indx.Primary = True
Set fld = indx.CreateField("ID")
indx.Fields.Append fld
tbl.Indexes.Append indx
Set indx = Nothing
Set fld = Nothing
'add rest of the fields:
Do Until rst.EOF
i = Me.subfFields.Form!cType
If i = dbText Then
Set fld = tbl.CreateField(Me.subfFields.Form!tName, i, Nz(Me.subfFields.Form!tSize, 255))
Else
Set fld = tbl.CreateField(Me.subfFields.Form!tName, i)
End If
tbl.Fields.Append fld
If Me.subfFields.Form!cControl = 111 Then
SetDAOProperty fld, "DisplayControl", dbInteger, acComboBox
End If
rst.MoveNext
Loop
'append table:
DB.TableDefs.Append tbl
'format comboboxes:
rst.MoveFirst
Do Until rst.EOF
If Me.subfFields.Form!cControl = 111 Then
Set fld = tbl.Fields(Me.subfFields.Form!tName)
SetDAOProperty fld, "DisplayControl", dbInteger, acComboBox
SetDAOProperty fld, "RowSourceType", dbText, "Value List"
SetDAOProperty fld, "RowSource", dbText, "Test1;Test2"
SetDAOProperty fld, "ColumnCount", dbInteger, 2
SetDAOProperty fld, "ColumnWidths", dbText, "0;1"
SetDAOProperty fld, "ListRows", dbInteger, 4
SetDAOProperty fld, "LimitToList", dbBoolean, -1
SetDAOProperty fld, "AllowValueListEdits", dbBoolean, 0
SetDAOProperty fld, "ShowOnlyRowSourceValues", dbBoolean, -1
End If
rst.MoveNext
Loop
This answer may be similar to this, but is not a duplicate. The goal is similar but the problem faced(error) is different.

VBA - Searching for a SPECIFIC file in a folder and attaching it in an Outlook Mail

I am working with VBA that would send error logs to multiple user. This error log can be found in a folder together with a process log file. These files have dates on their names and are not dependent on Now().
I only want to attach the error log and disregard the process log. I have done multiple research with similar topics and was able to made this code:
Sub SendEmailFail()
Dim OutlookApp As Outlook.Application
Dim OutlookMail As Outlook.MailItem
Dim RecipientF As Object
Dim myRecipientF As Outlook.Recipient
Dim sToF As Object
Dim CCf As Object
Dim myCCf As Outlook.Recipient
Dim sCcF As Object
Dim FilesF As VBA.Collection
Dim mDoneF As String
Dim FileF As Scripting.File
Dim AttsF As Outlook.Attachments
Application.ScreenUpdating = False
Set OutlookApp = New Outlook.Application
Set OutlookMail = OutloookApp.CreateItem(0)
Set FilesF = GetFilesF
mDoneF = Environ("userprofile") & _
"\Documents\Automation Anywhere Files\Automation Anywhere\My Reports\ABC\Logs\Done"
'=========================================START========================================='
Workbooks("ConfigFile.xlsm").Activate
Sheets("Sheet1").Activate
Range("C2").Select
Set RecipientF = Range(ActiveCell, ActiveCell.End(xlDown))
ActiveCell.Offset(0, 1).Select
Set CCf = Range(ActiveCell, ActiveCell.End(xlDown))
On Error Resume Next
With OutlookMail
.Display
End With
With OutlookMail
'Get all recipients from Column C
For Each sToF In RecipientF
Set myRecipientF = OutlookMail.Recipients.Add(sToF)
myRecipientF.Type = olTo
myRecipientF.Resolve
If Not myRecipientF.Resolved Then
myRecipientF.Delete
End If
Next sToF
'Get all CCs from Column D
For Each sCcF In CCf
Set myCCf = OutlookMail.Recipients.Add(sCcF)
myCCf.Type = olCC
myCCf.Resolve
If Not myCCf.Resolved Then
myCCf.Delete
End If
Next sCcF
.Body = ThisWorkbook.Sheets("Sheet1").Range("F2").Value & vbNewLine & _
vbNewLine & ThisWorkbook.Sheets("Sheet1").Range("F3").Value & vbNewLine & _
vbNewLine & ThisWorkbook.Sheets("Sheet1").Range("F4").Value & vbNewLine & _
vbNewLine & "Thank You!"
'Adding Error Logs
If FilesF.Count Then
Set AttsF = OutlookMail.Attachments
For Each File In Files
AttsF.Add FileF.Path
Next
End If
End With
On Error GoTo 0
Set OutlookMail = Nothing
Set OutlookApp = Nothing
Application.ScreenUpdating = True
End Sub
Function GetFilesF() As VBA.Collection
Dim FolderF As Scripting.Folder
Dim FsoF As Scripting.FileSystemObject
Dim FilesF As Scripting.Files
Dim FileF As Scripting.File
Dim ListF As VBA.Collection
Dim mSendF As String
Dim mDoneF As String
Dim StrFileF As String
mSendF = Environ("userprofile") & _
"\Documents\Automation Anywhere Files\Automation Anywhere\My Reports\ABC\Logs\Send"
mDoneF = Environ("userprofile") & _
"\Documents\Automation Anywhere Files\Automation Anywhere\My Reports\ABC\Logs\Done"
Set ListF = New VBA.Collection
Set FsoF = New Scripting.FileSystemObject
Set FolderF = FsoF.GetFolder(mSendF)
Set FilesF = FolderF.FilesF
For Each FileF In FilesF
'Return only visible files
If (FileF.Attributes Or Hidden) <> FileF.Attributes Then
StrFileF = Dir(Environ("userprofile") & _
"\Documents\Automation Anywhere Files\Automation Anywhere\My Reports\ABC\Logs\Send\*Error Log*")
If Len(StrFileF) > 0 Then
List.Add FileF
End If
End If
Next
Set GetFilesF = ListF
End Function
However, I encountered a run time error "424" : object required. This MsgBox only has an OK and HELP Button and a little bit small compared to the usual MsgBox size for errors. I do not know where the error is even though I can the macro using F8 since it doesn't highlight the line after the error was displayed.
EDITED
Changed some declarations and I was able to completely run the macro. Yet, Error logs AND process logs were both attached. I know there is a problem with my codes in searching for a file with "ERROR LOG" on its filename. The modified code was as follows:
Sub SendEmailFail()
Dim OutlookApp As Outlook.Application
Dim OutMail As Outlook.MailItem
Dim RecipientF As Object
Dim myRecipientF As Outlook.Recipient
Dim sToF As Object
Dim CCf As Object
Dim myCCf As Outlook.Recipient
Dim sCcF As Object
Dim Files As VBA.Collection
Dim mDoneF As String
Dim FileF As Scripting.File
Dim AttsF As Outlook.Attachments
Application.ScreenUpdating = False
Set OutlookApp = New Outlook.Application
Set OutMail = OutlookApp.CreateItem(olMailItem)
Set Files = GetFilesF
mDoneF = Environ("userprofile") & _
"\Documents\Automation Anywhere Files\Automation Anywhere\My Reports\AccentureCIO\Logs\Done"
'=========================================START========================================='
Workbooks("ConfigFile.xlsm").Activate
Sheets("Sheet1").Activate
Range("C2").Select
Set RecipientF = Range(ActiveCell, ActiveCell.End(xlDown))
ActiveCell.Offset(0, 1).Select
Set CCf = Range(ActiveCell, ActiveCell.End(xlDown))
On Error Resume Next
With OutMail
.Display
End With
With OutMail
'Get all recipients from Column C
For Each sToF In RecipientF
Set myRecipientF = OutMail.Recipients.Add(sToF)
myRecipientF.Type = olTo
myRecipientF.Resolve
If Not myRecipientF.Resolved Then
myRecipientF.Delete
End If
Next sToF
'Get all CCs from Column D
For Each sCcF In CCf
Set myCCf = OutMail.Recipients.Add(sCcF)
myCCf.Type = olCC
myCCf.Resolve
If Not myCCf.Resolved Then
myCCf.Delete
End If
Next sCcF
.Body = ThisWorkbook.Sheets("Sheet1").Range("F2").Value & vbNewLine & _
vbNewLine & ThisWorkbook.Sheets("Sheet1").Range("F3").Value & vbNewLine & _
vbNewLine & ThisWorkbook.Sheets("Sheet1").Range("F4").Value & vbNewLine & _
vbNewLine & "Thank You!"
'Adding Error Logs
If Files.Count Then
Set AttsF = OutMail.Attachments
For Each FileF In Files
AttsF.Add FileF.Path
Next
End If
End With
On Error GoTo 0
Set OutMail = Nothing
Set OutlookApp = Nothing
Application.ScreenUpdating = True
End Sub
Function GetFilesF() As VBA.Collection
Dim FolderF As Scripting.Folder
Dim FsoF As Scripting.FileSystemObject
Dim FilesF As Scripting.Files
Dim FileF As Scripting.File
Dim ListF As VBA.Collection
Dim mSendF As String
Dim mDoneF As String
Dim StrFileF As String
mSendF = Environ("userprofile") & _
"\Documents\Automation Anywhere Files\Automation Anywhere\My Reports\AccentureCIO\Logs\Send"
mDoneF = Environ("userprofile") & _
"\Documents\Automation Anywhere Files\Automation Anywhere\My Reports\AccentureCIO\Logs\Done"
Set ListF = New VBA.Collection
Set FsoF = New Scripting.FileSystemObject
Set FolderF = FsoF.GetFolder(mSendF)
Set Files = FolderF.Files
For Each FileF In Files
'Return only visible files
If (FileF.Attributes Or Hidden) <> FileF.Attributes Then
StrFileF = Dir(Environ("userprofile") & _
"\Documents\Automation Anywhere Files\Automation Anywhere\My Reports\AccentureCIO\Logs\Send\*Error Log*")
If Len(StrFileF) > 0 Then
ListF.Add FileF
End If
End If
Next
Set GetFilesF = ListF
End Function

Export Each Access Table To Individual Workbook

I have been using this syntax which will export every table in a database to ONE excel workbook, but now my needs are to export every table to it's own workbook. How could this be tweaked to export each table to it's own workbook?
Sub ExportToExcel()
Dim td As DAO.TableDef, db As DAO.Database
Dim out_file As String
out_file = "C:\fromaccess.xlsx"
Set db = CurrentDb()
For Each td in db.TableDefs
If Left(td.Name, 4) = "MSys" Then
'Do Nothing
Else
DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9, td.Name, out_file, True, Replace(td.Name, "dbo_","")
End If
Next
End Sub
EDIT
I tried the suggestion by #HA560 but get an error of
Run-time error '91':
Object variable or With block variable not set
This is updated code:
Sub ExportToExcel()
Dim td As DAO.TableDef, db As DAO.Database
Dim out_file As String
Dim xl As Excel.Application
out_file = "C:\fromaccess.xlsx"
Set db = CurrentDb()
For Each td in db.TableDefs
xl.Workbooks.Add
If Left(td.Name, 4) = "MSys" Then
'Do Nothing
Else
DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9, td.Name, out_file, True, Replace(td.Name, "dbo_","")
End If
Next
End Sub
Bit of a long one which includes a three procedures. After running you should have a list of table names and TRUE/FALSE in the immediate window saying whether the export was successful.
ExportAll - The main procedure.
CreateXL - this creates an instance of Excel. It uses late binding, so no need to set references.
QueryExportToXL - this is the code to export the table. I haven't used TransferSpreadsheet as I like more control.
You need to pass a worksheet reference to the function.
You can pass either a query name or a recordset to the function.
You can pass an alternative sheet name.
The default cell to paste into is A1, but you can change this.
By default it adjusts the column widths to fit.
You can pass a collection of heading names to use instead of the field names.
There's not much error handling in there - such as passing a different number of heading names than there are fields, giving illegal sheet names.
It needs work :)
Public Sub ExportAll()
Dim db As DAO.Database
Dim tdf As DAO.TableDef
Dim rst As DAO.Recordset
Dim oXL As Object
Dim oWrkBk As Object
Set db = CurrentDb
'Create instance of Excel.
Set oXL = CreateXL
For Each tdf In db.TableDefs
If Left(tdf.Name, 4) <> "MSys" Then
'Create workbook with single sheet.
Set oWrkBk = oXL.WorkBooks.Add(-4167) 'xlWBATWorksheet
'Open the table recordset.
Set rst = tdf.OpenRecordset
'In the immediate window display table name and TRUE/FALSE if exported successfully.
Debug.Print tdf.Name & " - " & QueryExportToXL(oWrkBk.worksheets(1), , rst, tdf.Name)
'Save and close the workbook.
oWrkBk.SaveAs "<path to folder>" & tdf.Name
oWrkBk.Close
End If
Next tdf
End Sub
'----------------------------------------------------------------------------------
' Procedure : CreateXL
' Author : Darren Bartrup-Cook
' Date : 02/10/2014
' Purpose : Creates an instance of Excel and passes the reference back.
'-----------------------------------------------------------------------------------
Public Function CreateXL(Optional bVisible As Boolean = True) As Object
Dim oTmpXL As Object
'''''''''''''''''''''''''''''''''''''''''''''''''''''
'Defer error trapping in case Excel is not running. '
'''''''''''''''''''''''''''''''''''''''''''''''''''''
On Error Resume Next
Set oTmpXL = GetObject(, "Excel.Application")
'''''''''''''''''''''''''''''''''''''''''''''''''''''''
'If an error occurs then create an instance of Excel. '
'Reinstate error handling. '
'''''''''''''''''''''''''''''''''''''''''''''''''''''''
If Err.Number <> 0 Then
Err.Clear
On Error GoTo ERROR_HANDLER
Set oTmpXL = CreateObject("Excel.Application")
End If
oTmpXL.Visible = bVisible
Set CreateXL = oTmpXL
On Error GoTo 0
Exit Function
ERROR_HANDLER:
Select Case Err.Number
Case Else
MsgBox "Error " & Err.Number & vbCr & _
" (" & Err.Description & ") in procedure CreateXL."
Err.Clear
End Select
End Function
'----------------------------------------------------------------------------------
' Procedure : QueryExportToXL
' Author : Darren Bartrup-Cook
' Date : 26/08/2014
' Purpose : Exports a named query or recordset to Excel.
'-----------------------------------------------------------------------------------
Public Function QueryExportToXL(wrkSht As Object, Optional sQueryName As String, _
Optional rst As DAO.Recordset, _
Optional SheetName As String, _
Optional rStartCell As Object, _
Optional AutoFitCols As Boolean = True, _
Optional colHeadings As Collection) As Boolean
Dim db As DAO.Database
Dim prm As DAO.Parameter
Dim qdf As DAO.QueryDef
Dim fld As DAO.Field
Dim oXLCell As Object
Dim vHeading As Variant
On Error GoTo ERROR_HANDLER
If sQueryName <> "" And rst Is Nothing Then
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'Open the query recordset. '
'Any parameters in the query need to be evaluated first. '
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Set db = CurrentDb
Set qdf = db.QueryDefs(sQueryName)
For Each prm In qdf.Parameters
prm.Value = Eval(prm.Name)
Next prm
Set rst = qdf.OpenRecordset
End If
If rStartCell Is Nothing Then
Set rStartCell = wrkSht.cells(1, 1)
Else
If rStartCell.Parent.Name <> wrkSht.Name Then
Err.Raise 4000, , "Incorrect Start Cell parent."
End If
End If
If Not rst.BOF And Not rst.EOF Then
With wrkSht
Set oXLCell = rStartCell
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'Paste the field names from the query into row 1 of the sheet. '
'Or the alternative field names provided in a collection. '
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
If colHeadings Is Nothing Then
For Each fld In rst.Fields
oXLCell.Value = fld.Name
Set oXLCell = oXLCell.Offset(, 1)
Next fld
Else
For Each vHeading In colHeadings
oXLCell.Value = vHeading
Set oXLCell = oXLCell.Offset(, 1)
Next vHeading
End If
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'Paste the records from the query into row 2 of the sheet. '
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Set oXLCell = rStartCell.Offset(1, 0)
oXLCell.copyfromrecordset rst
If AutoFitCols Then
.Columns.Autofit
End If
If SheetName <> "" Then
.Name = SheetName
End If
'''''''''''''''''''''''''''''''''''''''''''
'TO DO: Has recordset imported correctly? '
'''''''''''''''''''''''''''''''''''''''''''
QueryExportToXL = True
End With
Else
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'There are no records to export, so the export has failed. '
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
QueryExportToXL = False
End If
Set db = Nothing
On Error GoTo 0
Exit Function
ERROR_HANDLER:
Select Case Err.Number
Case Else
MsgBox "Error " & Err.Number & vbCr & _
" (" & Err.Description & ") in procedure QueryExportToXL."
Err.Clear
Resume
End Select
End Function
After for each use workbooks.add()method...out_file=activeworkbook.path

VBA - Word - Bookmarks disappearing when I try to write text on them

I am trying to make a macro in Excel, which takes a sample Word file with some bookmarks on it and writes something on the bookmarks. It works for one bookmark, but for the second, third, etc it simply deletes the other entries.
E.g. after the running of my code, I have only written "Info4". I see Info1, Info2 and Info 3 being written and deleted while the macro is run.
Any ideas? Here comes the code:
Option Explicit
Public Sub Main()
If [set_in_production] Then On Error GoTo Main_Error
Dim word_obj As Object
Dim word_doc As Object
Dim obj As Object
Dim rng_range As Variant
Dim obj_table As Object
Dim origDoc$
Dim l_row&: l_row = 2
On Error Resume Next
Set word_obj = GetObject(, "Word.application.14")
If Err.Number = 429 Then
Set word_obj = CreateObject("Word.application.14")
Err.Number = 0
End If
If [set_in_production] Then On Error GoTo Main_Error Else On Error GoTo 0
origDoc$ = ActiveWorkbook.Path & "\" & CStr(Replace(Time, ":", "_")) & "_" & generate_name & ".docx"
word_obj.Visible = True
word_obj.DisplayAlerts = False
Set word_doc = word_obj.Documents.Open(ActiveWorkbook.Path & "\SAMPLE_2.docx")
word_obj.activedocument.SaveAs Filename:=origDoc
'after the saveas -> write
Dim obj_BMRange As Object
Set obj_BMRange = word_obj.activedocument.Bookmarks("Info1").Range
obj_BMRange.Text = "Info1" & vbCrLf
Set obj_BMRange = Nothing
Set obj_BMRange = word_obj.activedocument.Bookmarks("Info2").Range
obj_BMRange.Text = "Info2" & vbCrLf
Set obj_BMRange = Nothing
Set obj_BMRange = word_obj.activedocument.Bookmarks("Info3").Range
obj_BMRange.Text = "Info3" & vbCrLf
Set obj_BMRange = Nothing
Set obj_BMRange = word_obj.activedocument.Bookmarks("Info4").Range
obj_BMRange.Text = "Info4" & vbCrLf
Set obj_BMRange = Nothing
word_obj.DisplayAlerts = False
Set word_obj = Nothing
Set word_doc = Nothing
Set rng_range = Nothing
Set obj = Nothing
Set obj_table = Nothing
On Error GoTo 0
Exit Sub
Main_Error:
MsgBox "Error " & Err.Number & " (" & Err.Description & ") in procedure Main of Sub mod_main"
End Sub
I have tried to rewrite the bookmarks, once they are deleted, but the success was no different. Thus, waiting for ideas! :D
The following approach works for me. (Note that I had to remove the lines of code specific to your workbook and files since I don't have access to any of that. But it doesn't (shouldn't) change anything relevant to the problem you present.)
Something that makes no sense in the code you posted is declaring a word_doc variable, then not using it, instead relying on ActiveDocument. I substituted word_doc as appropriate.
I also inserted On Error GoTo 0 to re-instate normal error handling. When you use On Error Resume Next normal error handling is deactivated, which you need for your approach with GetObject. But once the Word application is accessed it needs to be turned back on. Using it at the end of the routine makes no sense.
As mentioned by others, Word removes a bookmark when content is written to it if the bookmark already has content (you see [square brackets]). To get around this, the bookmark needs to be recreated around the content assigned to the Range. Since this involves a couple of steps I wrote a separate function for writing to the bookmark - WriteToBookmarkRetainBookmark.
When I test this from Excel the information is written to each bookmark and the bookmarks exist at the end.
Option Explicit
Public Sub Main()
Dim word_obj As Object
Dim word_doc As Object
Dim obj As Object
Dim rng_range As Variant
Dim obj_table As Object
Dim origDoc$
Dim l_row&: l_row = 2
On Error Resume Next
Set word_obj = GetObject(, "Word.application.14")
If Err.Number = 429 Then
Set word_obj = CreateObject("Word.application.14")
Err.Number = 0
End If
On Error GoTo 0
word_obj.Visible = True
word_obj.DisplayAlerts = False
Set word_doc = word_obj.ActiveDocument
' word_obj.ActiveDocument.SaveAs Filename:=origDoc
'after the saveas -> write
Dim obj_BMRange As Object
Set obj_BMRange = word_doc.Bookmarks("Info1").Range
WriteToBookmarkRetainBookmark obj_BMRange, "Info1" & vbCrLf
Set obj_BMRange = Nothing
Set obj_BMRange = word_doc.Bookmarks("Info2").Range
WriteToBookmarkRetainBookmark obj_BMRange, "Info2" & vbCrLf
Set obj_BMRange = Nothing
Set obj_BMRange = word_doc.Bookmarks("Info3").Range
WriteToBookmarkRetainBookmark obj_BMRange, "Info3" & vbCrLf
Set obj_BMRange = Nothing
word_obj.DisplayAlerts = False
Set word_obj = Nothing
Set word_doc = Nothing
Set rng_range = Nothing
Set obj = Nothing
Set obj_table = Nothing
Exit Sub
Main_Error:
MsgBox "Error " & Err.Number & " (" & Err.Description & ") in procedure Main of Sub mod_main"
End Sub
Function WriteToBookmarkRetainBookmark(rng As Object, content As String)
Dim sBkmName As String
sBkmName = rng.Bookmarks(1).Name
rng.Text = content
rng.Document.Bookmarks.Add sBkmName, rng
End Function
Just some workaround I found - using replace in Word - the code is a little "ugly", not dry, but it works:
With word_obj.ActiveDocument.Content.Find
.Text = "Info001"
.Replacement.Text = "VITYA1"
.Execute Replace:=wdReplaceAll
.Text = "Info002"
.Replacement.Text = "VITYA2"
.Execute Replace:=wdReplaceAll
.Text = "Info003"
.Replacement.Text = "VITYA3"
.Execute Replace:=wdReplaceAll
.Text = "Info004"
.Replacement.Text = "VITYA4"
.Execute Replace:=wdReplaceAll
End With
With word_obj.ActiveDocument.Shapes(1).TextFrame.TextRange.Find
.Text = "Info005"
.Replacement.Text = "VITYATA5"
.Execute Replace:=wdReplaceAll
.Text = "Info006"
.Replacement.Text = "VITYATA6"
.Execute Replace:=wdReplaceAll
.Text = "Info007"
.Replacement.Text = "VITYATA7"
.Execute Replace:=wdReplaceAll
.Text = "Info008"
.Replacement.Text = "VITYATA8"
.Execute Replace:=wdReplaceAll
End With
Still, if someone has an idea about how to solve the original issue, I would like to see it :)

DAO RecordSet error 3251 when using FindFirst

I am getting "Run-time error '3251': Operation is not supported for this type of object." when I am trying to use the "FindFirst" on my DAO RecordSet. Can someone explain to me what I am doing wrong for this not to search for the record. The variable I am using is returning the correct value for the search.
Private Sub ctrSend_Click()
Dim intI As Integer
Dim lst As ListBox
Dim varItem As Variant
Dim rst As DAO.Recordset
Dim rst2 As DAO.Recordset
Dim qtySum As Variant
Dim qtyDiff As Variant
Dim rowMax As Variant
Dim rowUpdate As Variant
Set lst = Me![lstShipping]
Set rst = CurrentDb.OpenRecordset("ShipInv", dbOpenTable)
Set rst2 = CurrentDb.OpenRecordset("Storage", dbOpenTable)
qtySum = 0
rowMax = 0
With lst
If .ItemsSelected.count = 0 Then Exit Sub
For Each varItem In .ItemsSelected
qtySum = qtySum + .Column(3, varItem)
Next
If Me.[ctrQtyProd] = qtySum Then
MsgBox "Qty Selected EQUALS Qty Being Shipped.", vbOKOnly, "Confirmation Message"
ElseIf Me.[ctrQtyProd] > qtySum Then
MsgBox "Qty Selected LESS THAN Qty Being Shipped, please select more Inventory.", vbOKOnly, "Confirmation Message"
Else
qtyDiff = qtySum - Me.[ctrQtyProd]
rowMax = lst.Column(3, lst.ItemsSelected.count)
rowUpdate = rowMax - qtyDiff
rst2.FindFirst "[BIN] = '" & lst.Column(0, lst.ItemsSelected.count) & "'"
rst2![QtyProd] = lst.Column(3, lst.ItemsSelected.count)
rst2.Update
rst2.Close
MsgBox "Storage Successfully Updated.", vbOKOnly, "Confirmation Message"
End If
End With
With lst
For Each varItem In .ItemsSelected
rst.AddNew
rst!Order = Me.[ctrSOrder]
rst!EntDate = Date
rst!ShipDate = Me.[ctrSDate]
rst!BIN = .Column(0, varItem)
rst!SKU = .Column(1, varItem)
rst!Lot = .Column(2, varItem)
rst!QtyProd = rowUpdate
rst.Update
Next
End With
rst.Close
Set rst = Nothing
Set rst2 = Nothing
MsgBox "Shipping List Successfully Updated.", vbOKOnly, "Confirmation Message"
End Sub
Did you try opening the recordset as a dbOpenDynaset (default without the enum specified in the open statement) or dbOpenSnapshot instead? The documentation on the Microsoft MSDN site specifies it takes one of those two recordsets.
Locates the first record in a dynaset- or snapshot-type Recordset object that satisfies the specified criteria and makes that record the current record (Microsoft Access workspaces only).
Microsoft MSDN article on Recordset.FindFirst