Updating textbox fill color using DocumentFormat.OpenXml from VB.Net - openxml

I have a request where I need to update a textbox fill color and border where specific text is found.
the following is part of my code.
For Each shape As DocumentFormat.OpenXml.Wordprocessing.Drawing In mainPart.Document.Body.Descendants.OfType(Of DocumentFormat.OpenXml.Wordprocessing.Drawing)()
'Replace date stamp with current date
'Find all placeholders that have mm/dd/yyyy as text.
If shape.Descendants.OfType(Of DocumentFormat.OpenXml.Wordprocessing.Text).Count() > 0 Then
If shape.Descendants.OfType(Of DocumentFormat.OpenXml.Wordprocessing.Text).First().InnerText = "mm/dd/yyyy" Then
LogVerboseMessage("Updating all date placeholders within document to current date")
Dim curDate As Date = Date.Now()
Dim curDateOnly As Date = curDate.Date
LogVerboseMessage("Shape has been found! Prepare to replace date.")
shape.Descendants.OfType(Of DocumentFormat.OpenXml.Wordprocessing.Text).First().Text = curDateOnly.ToString()
Dim color As String = shape.Descendants.OfType(Of DocumentFormat.OpenXml.Drawing.SolidFill).First.RgbColorModelHex.Val.ToString()
End If End If Next
When I run the above code in debug mode I get
"Sequence contains no elements"
error message.
I need to be able to get the current value and update it to a new value. Anyone have any suggestions?

Related

How do I access a date picker user selected value, from within a word document, to use in in the same document using VBA?

I need to add Excel style automatic date creation, based on a user input in Word. After accessing development menu I can insert a date picker menu control from the dev menu. This provides a simple and easy to enter date format. I then wish to take the user selected value and add additional dates based on that date selection, increased by a number of days, and have these dates automatically appear into another area of the document. I have not been successful in searching online. Does anyone know of a solution to this?
For that, you'll need a ContentControlOnExit macro coded along the lines of:
Private Sub Document_ContentControlOnExit(ByVal CCtrl As ContentControl, Cancel As Boolean)
Dim DtVal As Date, Fmt As String
With CCtrl
If .Type = wdContentControlDate Then
Fmt = .DateDisplayFormat
.DateDisplayFormat = "MMM-DD-YYYY"
DtVal = CDate(.Range.Text)
.DateDisplayFormat = Fmt
With ActiveDocument
.SelectContentControlsByTitle("Date1")(1).Range.Text = Format(DtVal + 5, "MMM D, YYYY")
.SelectContentControlsByTitle("Date2")(1).Range.Text = Format(DtVal + 21, "MMM D, YYYY")
.SelectContentControlsByTitle("Date3")(1).Range.Text = Format(DtVal + 90, "MMM D, YYYY")
End With
End If
End With
End Sub
and inserted into the 'ThisDocument' code module of the document or its template, where the output text content controls are titled Date1, Date2, Date3, etc.

VB Scripting - comparing time values, one coming from a text file?

I'm attempting to pull some information out of text file that is updated after I query a piece of equipment. The text file contains lines such as shown here (abbreviated):
05-Nov-13 11:11:54.3496 ( -1 7020 10244) scpeng.exe:Automation Server...
05-Nov-13 14:10:54.3496 ( -1 7020 10244) scpeng.exe:Automation Server...
05-Nov-13 14:10:54.3496 ( -1 7020 10244) scpeng.exe:Automation Server...
05-Nov-13 14:10:56.3496 ( -1 7020 10244) scpeng.exe:CServer.cpp,....
The text file can contain up to several weeks of information. I have a subroutine that will run a few seconds after I query the equipment which should allow for the reply and the applicable line to be present in the text file. In the routine, I am trying to scroll through the lines examining the date to arrive at the date of the subroutine call followed by the time (or a time ~10 seconds prior the the current time) to arrive at the lines pertinent to where the information could be found.
do
msg = msgstream.ReadLine
logdate = mide(msg,1,9)
logday = Cdate(logdate)
loop while logday < date
do
msg = msgstream.Readline
logtime = mid(msg,12,8)
'logtime = CDate(logtime) This mod is not working
loop while logtime < time
The date loop appears to work however the time is giving me problems. It does not error out but I can't get it to run beyond one line of text. Can anyone suggest a fix or better option? I have read that the built-in Date function can include the time but I do not believe this version I'm using does. Also, the text file contains times in a 24 hour format where I believe the time function returns values in a 12 hr format ie "12:43:27 PM ST".
You're making this way too complicated. Simply parse the whole date string into a datetime value:
refdate = Now
Do
msg = msgstream.ReadLine
logdate = CDate(Mid(msg, 1, 19))
Loop While logdate < refdate
You can extract date and time portions from the value later, e.g. like this:
WScript.Echo DateValue(logdate)
WScript.Echo TimeValue(logdate)
Also, Time returns the current (unformatted) system time. Whether it's displayed in 12 hour or 24 hour format depends on your system's region settings. However, you can always get the hour (0-23) by using the Hour function.
Parse each line with a regex to get the correct date and time part. I prefer a regexp above string manipulation functions because you can separate format and code.
Reassemble the date from the two parts and see if the date is smaller than yesterday at this time.
Option Explicit
dim strTest, re, matches, myDatePart, myTimePart, logDate
' teststring
strTest = "08-Nov-13 14:10:56.3496 ( -1 7020 10244) scpeng.exe:CServer.cpp,...."
Set re = new regexp
' This pattern extracts two part, the date as (dd-www-dd) and the time as (hh:mm:ss)
re.pattern = "(\d{2}-\w{3}-\d{2}) +(\d{2}:\d{2}:\d{2})"
Set matches = re.Execute(strTest)
' Get the first and second submatch to define the date and time
myDatePart = matches(0).submatches(0)
myTimePart = matches(0).submatches(1)
' datevalue and timevalue automatically tranforms to Date type
logDate = datevalue(myDatePart) + timevalue(myTimePart)
' See if the date is smaller than yesterday exactly this time
msgbox (logDate < (DateAdd("d", -1, now))) ' Returns True, because 08 Nov is earlier than yesterday.

Converting Date in Extjs

I have a Date string of following format:
'31-OCT-2013'
How do I convert this into Date of following format using Extjs 4:
'08/31/2013'
I am using IE8.
If you have string "31-OCT-2013", you need to:
Convert it into date object
var myDate = Ext.Date.parse("31-OCT-2013", 'd-M-Y');
Format it to as you want
Ext.Date.format(myDate, 'm/d/Y');
Try something like this:
If your day representation would be 2 digit with leading zero, then apply this
var date = Ext.Date.parse("31-OCT-2013", 'd-M-Y');
console.log(Ext.Date.format(date, 'm/d/Y'));
But if your day representation would be without a leading zero, then apply this
var date = Ext.Date.parse("31-OCT-2013", 'j-M-Y');
console.log(Ext.Date.format(date, 'm/d/Y'));
Check the docs for Ext.Date

Excel VBA - the date format changes automatically

I'm trying to enter the Date value by adding a month to the date in Sheets("Sheet1").Cells(17, 3).Value which is 01/10/2011 but format as Oct-11. Then return in Sheets("Sheet1").Cells(17, 4).Value = LDate, to show Nov-11
sDate = Sheets("Sheet1").Cells(17, 3).Value --> this shows 01/10/2011 when I hove over sDate
LDate = DateAdd("m", 1, sDate) --> this shows 01/11/2011 when I hove over LDate
I then want to enter that value 01/11/2011 in to the following cell
Sheets("Sheet1").Cells(17, 4).Value = LDate
Selection.NumberFormat = "mmm-yy"
But in the cell it shows 11/01/2011 (Jan-11), why is it doing this and how can I fix this issue?
Minor issue
Selection.NumberFormat = "mmm-yy"
You have not selected anything so this format is placed wherever you left the cursor.
Major issue
You have hit the Excel bug that it will interpret dates as being in American (middle endian) format if it can when transferring data to a worksheet. "1/11/2011" would be "11 Jan 11" to an American so it is to Excel. "20/11/2011" is not a valid American date so to Excel it is "20 Nov 11".
I can duplicate your problem by declaring sDate and LDate as strings. DateAdd works correctly with strings so LDate is correct but when placed in a cell it is misinterpreted.
I can fix your problem by declaring sDate and LDate as dates:
Dim sdate As Date
Dim Ldate As Date
Selection.NumberFormat = "mmm-yy"
Above line is changing the number format. Actually When am running through your steps, I have come across the same issue.
But Number format is doing all this auto change.
Example:
While am running the macro, assume that selected cell is (17,3).
Before running macro: cell value is 01/11/2012
After running macro: cell value will be changed/formatted as "Jan-12". (Since number format is applied as "mmm-yy", so it consider as 01/11/2012 is consider as Jan-12)
Second time, if you select the cell (17,4)
Before running macro: cell value is 2/11/2012
After running macro: cell value will be changed / formatted as "Feb-12"
Instead of trying
Cells(row2,col2) = Cells(row1,col1)
Try this
Cells(row1,col1).Copy
Cells(row2,col2).PasteSpecial Paste:=xlPasteValuesAndNumberFormats

set Datagirdviewcombobox column value in Vb.net

I have datagrid with two datagridviewcombo column, one column is dynamic fill and one has fixed hardcoded values.
The problem is I can't set the value of dynamic GridViewComboBox, when I try to set it generates continues errors.
System.FormateException: DataGridViewComboBoxCell Value is not valid.
My code to load the grid is
Dim dt As DataTable
dt = GetDataTable("Select valuecol,displayCol From mytable") 'GetDataTable gives me datatable
cmbAntibiotics.DataSource = dt
cmbAntibiotics.DisplayMember = "Antibiotics"
cmbAntibiotics.ValueMember = "AntibioticsID"
Dim Index As Integer
Dim dgr As DataGridViewRow
For Each dr As DataRow In dtFromDB.Rows 'This datatable is filled with database
Index = dtFromDB.Rows.Count - 1
GRDAntimicrobials.Rows.Add()
GRDAntimicrobials.Rows(Index).Cells("cmbAntibiotics").Value = dr("AntibioticsID").ToString 'At this point it shows value (1,2,3) rather then showing its display members
GRDAntimicrobials.Rows(Index).Cells("AntibioticsStatus").Value = dr("AntibioticsStatus").ToString
Next
Pls help with me
It seems like you're trying to assign the value to whatever there is on the cell rather than instantiate the object that resides in the cell and then assign its value. I would try something like this:
Dim vComboBoxColumn As DropDownList = DirectCast(GRDAntimicrobials.Rows(index).Cells("cmbAntibiotics"))
vComboBoxColumn.Value = dr("AntibioticsStatus").ToString