VBA Word: Property Revision.Style dosn't work - vba

When I try to change some styles (e.g. textcolor) in a revisions object I get a Run-Time error '5852' (requested object is not available). I've used ActiveDocument.Revisions(i).Style ... Other properties are available (author, creator, type, ...). Btw: I really want change the style from the revision (not the ordinary text). Tested in Word 2010 & 2016
edit: a bit more detailed:
I try to change the color of the revisions that is displayed. Word sets it automically by default. But you can't choose which color Word assigns to reviewers (See this link)
Unfortunately this is exactly what I want to do. So I tried it with VBA and actually there is the property style available for the revision object, but I get an error 5852 in line with ActiveDocument.Revisions(i).Style
Sub test()
i = 1
While i <= ActiveDocument.Revisions.Count
Debug.Print "Revision " & i & " Author: " & ActiveDocument.Revisions(i).Author 'works
Debug.Print "Revision " & i & " Creator: " & ActiveDocument.Revisions(i).Creator 'works
Debug.Print "Revision " & i & " Date: " & ActiveDocument.Revisions(i).Date 'works
Debug.Print "Revision " & i & " FormatDescription: " & ActiveDocument.Revisions(i).FormatDescription 'works...
Debug.Print "Revision " & i & " Range: " & ActiveDocument.Revisions(i).Range 'work
Debug.Print "Revision " & i & " Style: " & ActiveDocument.Revisions(i).Style 'error 5852
Debug.Print "Revision " & i & " Type: " & ActiveDocument.Revisions(i).Type 'works
i = i + 1
Wend
End Sub
Does anyone know why this error occurs?

As you say, it's not possible to set the color for revisions per author, explicitly. It simply is not possible.
I believe that Revisions.Style is not for changing how a revision looks (color or anything else). I believe it's meant to return Style information, such as the name of the style and the various properties (analog to Range.Style). The error is because Word hasn't recorded such information, probably because it doesn't recognize the related style types...
There are two related(?) Revision.Type enums: wdRevisionStyle and wdRevisionStyleDefinition. In a quick test - formatting using a style, creating a style and changing a style definition - I was not able to return either of these revision types. But according to the Language Reference they mean:
wdRevisionStyle 8 Style changed.
wdRevisionStyleDefinition 13 Style definition changed.
In my tests, the type of revision returned for applying a style is wdRevisionParagraphProperty and changing a style definition is not recorded as a Revision.
For Revision.Style to return information I believe you'd need to test whether the Revision.Type is either of the above and only then would there be a valid object in Revision.Style.
I'm looking at Word 2010.

Related

Access VBA SQL Update - Too few Parameters expected 1

I am new to access VBA.
I am trying to add 1 to all numbers in a field that are equal or greater than the value in text box [TP_Bld_OrderNum_Txt].
This is my code:
CurrentDb.Execute "UPDATE TP_Matrix " & _
"SET TP_Matrix.Order_" & Me.TP_Bld_TP_Cbo & " = TP_Matrix.Order_" & Me.TP_Bld_TP_Cbo & "+1 " & _
"WHERE TP_Matrix.Order_" & Me.TP_Bld_TP_Cbo & ">= Me.TP_Bld_OrderNum_Txt"
I get this error:
too few parameters expected 1
I believe it relates to the text box value.
If I replace Me.TP_Bld_OrderNum_Txt with a number, the query works fine.
I have the text box set up as a number.
Why doesn't it recognize its value?
You provided Me.TP_Bld_OrderNum_Txt as a literal (as a fixed string) and not its value:
& ">= Me.TP_Bld_OrderNum_Txt"
Try this instead:
& " >= " & Me.TP_Bld_OrderNum_Txt.Value
Also, it is a good practice to use .Value to explicitely use the value of the control.

Using a saved report template and updating the Recordsource, then save to file

I'm trying to output reports. They all follow the same information, just for different managers. I can't seem to get any of the right syntax in to modify the necessary parts (Recordsource, Caption, and 1 field that will contain a set string). I don't know what its asking me and I can get the code to find the next manager name (a recordsource) and cycle through them. I can get the code to output to PDF file no problem. It's the relevant code below that I need to figure out. It's probably completely wrong. I haven't really dealt with reports yet.
DoCmd.OpenReport "rptUsageReportTemplate", acViewReport
Reports("rptUsageReportTemplate").RecordSource = MngrUsgRptStr
Reports("rptUsageReportTemplate").Caption = MngrName & "'s " & Mnth & " Usage Report"
Reports("rptUsageReportTemplate").Controls("fldManagerHeader") = MngrName & "'s " & Mnth & " Usage Report"
Reports("rptUsageReportTemplate").Requery
DoCmd.Close acReport, "rptUsageReportTemplate", acSaveYes
The "docmd.openreport" Is in there because i couldn't stop getting the error 2451 - The report name [...] you entered is misspelled or refers to a report that isn't open or doesn't exist." I know it exists and I know it's spelled correctly. So it must be an open thing. If I can get Access to output reports behind the scenes without needing to see the report open and close, that would be great.
In short what I want is for the Report I saved to be a template and just update the values a bunch of times and save it to file.
Assuming that you have a "Managers" Table or similar (tblManagersOrSuch) the following untested sub should get you going...
Sub DoManagersReport(Mnth As Integer)
Dim rsManagers As DAO.Recordset
Set rsManagers = CurrentDb.OpenRecordset("tblManagersOrSuch")
If Not rsManagers.EOF Then
rsManagers.MoveFirst
Do Until rsManagers.EOF
DoCmd.OpenReport "rptUsageReportTemplate", acViewPreview, , , acHidden
Reports("rptUsageReportTemplate").Caption = rsManagers!ManagerID & "'s " & Mnth & " Usage Report"
Reports("rptUsageReportTemplate").Controls("fldManagerHeader") = rsManagers!ManagerID & "'s " & Mnth & " Usage Report"
Reports("rptUsageReportTemplate").RecordSource = "Select * from MngrUsgRptStr Where ManagerID = " & rsManagers!ManagerID
DoEvents
Reports("rptUsageReportTemplate").Visible = True
DoCmd.OutputTo acOutputReport, "rptUsageReportTemplate", acFormatPDF, "C:\" & rsManagers!ManagerID & " " & Mnth & " Usage Report.pdf"
DoEvents
DoCmd.Close acReport, "rptUsageReportTemplate"
Loop
End If
'add error handling
End Sub
Note that setting the RecordSource forces a requery so you do not require that.
I would also suggest adding a fileSaveAs function to determine the save folder...

Global Variable Lookup

Hopefully, I'll make this question as precise and understandable as possible - but you'll tell me if i don't ! Thanks in advance.
Firstly, a little background and what I've found that DOES work, then on to a small change that I cannot get to work. Rather than use the whole code, I've used snippets that should give enough information for you to understand that I have initiated things correctly first.
I use a menu system (a turbo-charged version of the original MS one) that has additional fields to store information needed to make changes depending upon what wording the user wants to use, so I may name a field Product, whereas a user may want to call it Goods or Items or Stuff or whatever he desires! So I store the user preferences in a separate table (we'll call that tblWORDS). When the menu is populated (remember it operates in a similar fashion to the standard SwitchBoard) with the data from fields: ItemText, Command & Argument the menu normally displays the text from ItemText, which I use. But, I have added a NEW field called CAPTION in SWITCHBOARD table because the VBA code does not allow for formatting the labels as I want them. So, when the VBA code reads ItemText for the label, from the recordset, and it encounters a | (pipe), my added VBA code then looks to the field Caption for a string. Hopefully, enough background info!!??
[SwitchBoard].[Caption] originally contained the following: (EVERYTHING INCLUDED)
"" & dlookup("fldProduct","tblWORDS") & ""
This worked perfectly!! But...
Instead of performing a lookup every time I need the WORD, I decided to create GLOBAL VARIABLES, so I have a Global Variable of glProduct, which obtains the word from the tblWORDS table correctly and retains that just fine. This is then available throughout the session anywhere.
I have substituted the string above to read the Global Variable instead of performing a lookup each time, to: (again, EVERYTHING INCLUDED)
"" & glProduct & ""
So, my code is as follows:
While (Not (rs.EOF))
Me("Option" & rs![ItemNumber]).Visible = True
'MY PIPE DEVIATION
If Left(Trim(rs![ItemText] & ""), 1) = "|" Then
'THIS LINE WORKS JUST FINE
'DISPLAYS CORRECTLY (rs!Caption = "" & dlookup("fldProduct","tblWORDS") & "")
szTemp = DLookup(rs![Caption], "tblWORDS")
'AS DOES THIS (but, I'm explicitly naming the variable in code! Only entered this line to show that the variable is working!)
szTemp = "" & glbProduct & ""
'THIS DOES TOO (Just a BYREF function for testing)
szTemp = fnGetValue(DLookup(rs![Caption], "tblWORDS"))
Me("Option" & rs![ItemNumber]).Caption = szTemp
Else
If rs![ItemNumber] = 0 Then
Me("OptionLabel" & rs![ItemNumber]).Caption = VBA.Trim(rs![ItemText] & " (" & rs![SwitchboardID] & ")")
Me("OptionLabel" & rs![ItemNumber]).Visible = True
Me("Option" & rs![ItemNumber]).Visible = False
Else
Me("Option" & rs![ItemNumber]).Caption = VBA.Trim(rs![ItemText] & "")
End If
End If
rs.MoveNext
Wend
REVISED TO:
While (Not (rs.EOF))
Me("Option" & rs![ItemNumber]).Visible = True
'MY PIPE DEVIATION
If Left(Trim(rs![ItemText] & ""), 1) = "|" Then
'THIS WORKS (but, I'm explicitly naming the variable in code! Only entered this line to show that the variable is working!)
szTemp = "" & glbProduct & ""
'THIS DOES NOT (FYI: rs!Caption = "" & glbProduct & "")
szTemp = rs!Caption
'NOR DOES THIS (Just a BYREF function for testing)
szTemp = fnGetValue(rs!Caption)
Me("Option" & rs![ItemNumber]).Caption = szTemp
Else
If rs![ItemNumber] = 0 Then
Me("OptionLabel" & rs![ItemNumber]).Caption = VBA.Trim(rs![ItemText] & " (" & rs![SwitchboardID] & ")")
Me("OptionLabel" & rs![ItemNumber]).Visible = True
Me("Option" & rs![ItemNumber]).Visible = False
Else
Me("Option" & rs![ItemNumber]).Caption = VBA.Trim(rs![ItemText] & "")
End If
End If
rs.MoveNext
Wend
What IS displayed is the literal string as entered ("" _glProduct "") not Product as it was prior to my revision!
So... the first problem is that you're confusing everybody with what you're doing it and where it's happening - where did rs![Caption] come from???
The second problem is you can't access global variables in your form or control properties.
You need to set the caption via code. I'm going to make some big guesses as to what you're using and what each element is, so let's start here:
First
Replace the Caption property with something else - just "Caption" for example.
Second
Add this code after your loop - I think that will work - hard to really know because you don't show where this code is and when it runs
[SwitchBoard].Caption = "" & glProduct & ""

How to add a comment using comment boxes in Access?

I am building an Access database for work. I have set up a report to open upon click for a certain record. So only the information of that record is to appear on the report. However, I would like to add a comment box in the report where you can add comments. The new comments are stamped and added to the previous comments already showing in the report. I was able to program the commenting function in a separate report. However, for the reports that show only specific records it won't work. I know it is because I have to somehow add each comment to my database, but I just can not figure out how to do it. I used the following code that I found online in another article. It works fine when your comments are not tied to a certain record.
Private Sub cmdAppendComment_Click()
If (IsNull(txtNewComment.Value)) Then
MsgBox ("Please enter a comment before clicking" & _
"on the Append Comment button.")
Exit Sub
End If
' These commented lines will never be reached:
' If (IsNull(txtComment.Value)) Then
' Table.tblmain.User_comment.Value = txtNewComment.Value & " ~ " & _
' VBA.DateTime.Date & " ~ " & VBA.DateTime.Time
' Else
Table.tblmain.User_comment.Value = txtComment.Value & _
vbNewLine & vbNewLine & _
txtNewComment.Value & " ~ " & _
VBA.DateTime.Date & " ~ " & VBA.DateTime.Time
' End If
' txtNewComment.Value = ""
' Use Null:
txtNewComment.Value = Null
End Sub
You would use a form, not report, for this.
Bind this to the table, add the new comment to the existing comment bound to texbox, and save the record.

vbmsgBox prompt is a mix of RTL(Persian) and LTR(English) but text is displayed in wrong order

I am designing a vba userform for excel 2013, in which I use English and Persian text, the problem is when I use a mix of both languages in a msgBox the bits of text come out in the wrong order, the number is supposed to be displayed at the end. here is the code:
Private Sub CommandButton1_Click()
MsgBox "نام" & " - " & "نام" & " - " & "VF" & " - " & 52 & " ." _
, 1 + 1048576 + 524288, "نام برگه"
End Sub
The parts in double quotes and the number are supposed to come from listBoxes.(I just replaced them here with examples, the code behaves the same)
I tried spacing the bits out(works in windows), rearranging the bits and changing msgBox's alignment and reading order, but the result was the same. How to fix this thing?
Could you put all the persian into a string. Have the message box output the string and then have the message box output the numbers?
So it would be something like
aString = ListBox1.value & "-" & listbox2.Value ....
msgbox aString & 52 etc...