With VBA, is it possible to open the window displayed when adding a new IEEE reference in Word (2013-2019), and access the fields' filled in values on closing it?
It's the window which asks for the publication type (book, journal...) and based on that presents different TextBoxes to be filled in (authors, title, publisher...)
CodePudding user response:
Perhaps along these lines (i.e. not working code, just an outline):
' Declare some variables
Dim result As Long
Dim sourceCount As Long
Dim mySource As Source
' record the Source count pre-dialog
sourceCount = ActiveDocument.Bibliography.Sources.Count
' Set the Bibliographic Style to IEEE
ActiveDocument.Bibliography.BibliographyStyle = "IEEE"
' Display or Show the Create Source Dialog box
result = Dialogs(wdWordDialog.wdDialogCreateSource).Display
' .Show and .Display *should in theory * return different values
' depending on whther the user clicked OK, Cancel, the dialog's
' Close box etc.but on Windows Word the returned value
' always seems to be 0. On Mac Word that seems to work OK.
' So actually you might as well get rid of 'result' and use
' Dialogs(WdWordDialog.wdDialogCreateSource).Display
' so see if any new entries have been added
If ActiveDocument.Bibliography.Sources.Count > sourceCount Then
' There's a new entry. For the sake of argument, assume that the newest Source is the last in the list
Set mySource = ActiveDocument.Bibliography.Sources(ActiveDocument.Bibliography.Sources.Count)
' At this point, it probably helps to look at the XML of the new source, e.g.
Debug.Print mySource.XML
' Because the XML only contains the elements corresponding to the
' fields that the user filled in.
' Perhaps the Tag field is always present - I have not checked.
' You can try to retrieve the value of a particular field using e.g.
Dim author As String
author = mySource.Field("author")
' but if there is no author element in the XML, that will raise an error
; i.e. you'll probably need some On Error Resume Next handling.
' To use that approach you have to know the field names for every
' possible field in any type of IEEE source, and/or what fields are
' allowed in each type of source.
' Not sure the Word object model will help you there.
' **new material**
' All the possible field names and the corresponding XML
' element names are listed in a file called bibform.xml
' in the Microsoft Office program folder structure.
' On my Windows system, that's in
' C:\Program Files\Microsoft Office\root\Office16\1033\Bibliography
' On Mac, it's inside the app's package, e.g. the US English
' version is in
' Application/Microsoft Word/Contents/Resources/en.lproj
' There are different bibform.xml files for different
' (human) languages but the XML element names are the same
' for every language.
' So perhaps a better approach would be to parse the XML for
' the new Source and discover what fields are actually in there.
' I'm not going to try that here.
Set mySource = Nothing
End If
If it turns out that the most recently added Source is not always this one
ActiveDocument.Bibliography.Sources(ActiveDocument.Bibliography.Sources.Count)
then you might have to do something like
- create a list of Sources prior to displaying the dialog box
- display the dialog box
- if there's a new Source, work out which one it is by comparing with the existing list
Without further testing, it's not even obvious that the new Source's Tag/Tag Name is unique.
Notes:
- MacWord seems to work much the same way except as commented above.
- ActiveDocument.Bibliography.BibliographyStyle = "IEEE" assumes you have an unmodified Office/Word installation with the standard IEEE Style installed.