I have created Word template with Repeating Section Rontent Control (RSCC) containing other Content Controls. Also I have excel workbook with information which should go to mentioned Word template. The thing I am trying to do is to create macro which would fill Word template with information from selected rows in Excel workbook (each row to new RSCC section).
I have quite good idea how to do that, except one thing- I cannot figure out how to write macro which would add another section to Repeating Section Content Control.
I am adding illustrations of what I am trying to do:
Problem is, that I cannot find code to do the same thing with VBA. I have tried recording process, but recorded macro is empty (?!).
Looking for answer I have found this thread in StackOverflow, it ask similar question to mine, but it was more or less unanswered to my understanding. Comment in this thread forwarded to old thread in Microsoft forum, but I did not find solution to this problem (or at least I did not understand clearly how should I approach it).
Since one thread is almost 5 years old, another 2 years old. My question is it even possible to add another section to RSCC with VBA? Maybe somebody found a way to do this in the past year or so?
The Word object model has a collection and object for a repeating section content control: RepeatingSectionItems and RepeatingSectionItem. The latter has two insert methods, to insert before or after the RepeatingSectionItem.
Here's a sample that shows how to reference a repeating section content control in a document, get the first or last item and insert a new one after it.
Sub AddRepeatingSection()
Dim cc As Word.ContentControl
Dim repCC As Word.RepeatingSectionItem
Set cc = ActiveDocument.SelectContentControlsByTitle("RepCC").Item(1)
Set repCC = cc.RepeatingSectionItems.Item(1)
'Or to get the last one:
'Set repCC = cc.RepeatingSectionItems.Item(cc.RepeatingSectionItems.Count)
repCC.InsertItemAfter
End Sub
Related
I am in the process of creating a new version of the Template. Excel 2019 Windows 10 64 bit. I have already gone through the exercise of creating a new template and copying the worksheets one at a time. There were a number of references that I found and deleted. Many of them were in the Names dictionary. I am now down to 2 that should not have been present in the first place because they are only to be used in the workbooks that are created from the templates.
I have tried and exhausted all of the samples in questions that I have found. It is distilled into the following code:
`Public Sub FindLinks()
Dim i As Integer
Dim olinks As Object
Dim v() As Variant
Dim strLink As String
v = ActiveWorkbook.LinkSources(xlExcelLinks)
For i = 1 To UBound(v)
strLink = v(i)
Debug.Print i, "Link = "; strLink
' Now break the link
ActiveWorkbook.BreakLink Name:=strLink, Type:=xlLinkTypeExcelLinks
Next i
End Sub`
The above example finds two links and displays them but will not break the links - just does nothing.
Anyone have a notion of what I could try next?
The standard "Break Links" route does not capture links that are referenced through:
Named ranges
Conditional formatting
Data validation
Graph sources
Pivot Table sources
If you have any of these components or functionalities, look through their corresponding interfaces (e.g. the Name Manager for named ranges, etc.) and check for linked content.
SIE_Vict0ria - Thank you. I had actually fixed all the references in the Name Manager, but I discovered the two references that were "invalid" were in combo-boxes. I deleted them from the combo boxes and voila, no more external references at all. So the problem is fixed.
One thing I do not know, however, is why those references were bogus. The spreadsheets being references were just where they were supposed to be and were shared and marked full control to everyone.
I did not find any documentation on why a reference would be marked as "invalid".
I hope this helps others when they are chasing this kind of problem.
I am trying to copy some charts (embedded) in different worksheets of an excel workbook using vbscript into different slides of a powerpoint. I would like to keep the link between the excel sheet and the powerpoint while doing so and therefore I used the below piece of code that allows me to keep formatting and link (instead of a simple paste or PasteSpeical. Is there any other way?) :
For i = 1 to TotalNumWorkSheets 'I iterated with indices.
Set pptSlide = pptPres.Slides.Add(i, 11) 'There is one opening slide before this.
set ws = wb.Worksheets(i)
ws.ChartObjects(1).Chart.ChartArea.Copy
pptApp.CommandBars.ExecuteMso("PasteExcelChartSourceFormatting")
pptApp.CommandBars.ReleaseFocus
With pptSlide
.Shapes.Title.TextFrame.TextRange.Text = objCurSheet.Name
'Adding some more textboxes here. Working fine. Position checks out in PPT.
.Shapes(.Shapes.Count).Left = 20 'Doesn't work for all slides.
End With
Next
The code works and copies all the charts, creates titles, adds new text as expected, but I am not able to position the charts on individual slides because after the ExecuteMso command, I don't know how to access the reference to the chart. I read in one of the SO posts that pasting using this method looses the chart selection but you can access the last .Shapes object since pasting always adds the object to the end of the list. Is that always the case? I tried accessing and positioning my chart by accessing the last object but it only works for the first slide of the loop (i.e. the first chart pasted is shifted to Left=20). The rest all charts in other slides are centered. Can someone explain where and how to add the formatting chart options? I ran into an even weirder problem. If I increase the number of worksheets, even the first plot looses it's Left formatting. The above code is the only place where I add formatting so I don't know what is happening. I am sure I am not formatting it correctly.
Many thanks for your suggestions.
Edit: One additional thing which I tested. I am using
WScript.Sleep 500
code before the For loop ends since it gives enough time for earlier operations to finish (at least that's what I understood from many other google searches).
So after some more searching and testing, I found a solution (for my case at least).
Moved the WScript.Sleep 1000 (500 didn't work for me) statement just below CommandBars.ReleaseFocus.
It makes some sense now since it is the Chart copying, pasting and linking from excel that needs time. Especially with source formatting. After that there is only text generation which I believe is not so heavy.
Cleared all the set Object variables when not used. Especially the ones associated with the "With" keyword.
set obj = CreateObject()
With obj
'Do something here.
End With
set obj = Nothing
Not clearing them, apparently, can also prevent you from closing the applications even after you use the .Close and .Quit method. At least that's what I observed. I found PowerPoint.exe running in the task manager when nothing was opened.
After doing the above, I am able to copy with format the charts and also able to set the position of the charts. The weird problem of larger number of worksheets also disappeared. Hopefully, it might help others. If someone thinks the observations are incorrect or troublesome, please correct.
I have experienced the exact same problem as mentioned in this post.
Excel thinking cells are locked when they aren't
I tried to ask for clarification by responding / reviving the thread but it appears this is not the correct protocol. I only came across this post after numerous search string searches and this is the only relevant "hit" I could find.
My spreadsheet is similarly complex and has been running flawlessly through Excel 98, 2003 and 2010. When I use Excel 2013 (and I believe same problem happens with Excel 2016) I find cells which are unprotected (for data entry) become locked following use of a macro button, typically navigating to view a graph and then returning. Properties of the cell confirms the cell is not protected.
Reading the above post and the Microsoft links, I have only a shallow understanding of the problem i.e. SDI / Single Document Interface.
The above referenced post received a solution that was acknowledged as having resolved the issue. Perhaps I did not fully understand but no matter where I placed the code snipped it did not help me.
The code offered was:
Private Sub UserForm_Layout()
Static fSetModal As Boolean
If fSetModal = False Then
fSetModal = True
Me.Hide
Me.Show 1
End If
End Sub
There was another suggestion to try: MyForm.Show False I did not know where to pace this similarly to the main suggested code to resolve.
VBA did not like me using "Me" (Me.Hide / Me.Show 1). I tried place the code snippet in several places ... the main opening macro, the navigation macro to and from the sheet that is affected, changing "Me" to the sheet name all to no avail. It is a sheet but since the unique variables are gathered here, it may loosely be viewed as a "Form".
I used to be a competent amateur but never an expert. I am appealing for a kind person to help direct me to overcome this "Excel 2013" problem. I have tried and failed many times and spent many hours trying to fix only to find the problem is because I'm using Excel 2013. There are even now no issues using Excel 2010 which I am able to access via a friend.
I'm sorry my first post is so detailed and hopefully not over-complex. I only wanted to give the correct information about the problem.
I know this is probably simple, but I have spent 2 hours Googling for the answer and I can't find it. I am using the Office Web Components 2003 (v11) Spreadsheet Control on an Excel 2007 userform. I am using the BeforeContextMenu event to create a custom context (right-click) menu. I'm using the code sample from the Spreadsheet Control's help file to create the menu. The code is:
Sub Spreadsheet1_BeforeContextMenu(x, y, Menu, Cancel)
Dim cmContextMenu(4)
Dim cmClearSubMenu(2)
cmClearSubMenu(0) = Array("&All", "ClearAll")
cmClearSubMenu(1) = Array("&Formats", "ClearFormats")
cmClearSubMenu(2) = Array("&Values", "ClearValues")
cmContextMenu(0) = Array("Cu&t", "owc2")
cmContextMenu(1) = Array("&Copy", "owc3")
cmContextMenu(2) = Array("&Paste", "owc4")
cmContextMenu(3) = Empty
cmContextMenu(4) = Array("Clea&r", cmClearSubMenu)
Menu.Value = cmContextMenu
End Sub
The custom menu appears perfectly when the spreadsheet is right-clicked. What I can't figure out is how to capture the menu item that I select. Can someone please point me to VBA code that will enable me to capture and use the selection?
Thanks In Advance -
After more Googling I found the answer to this problem in the Help for the OCW11 Spreadsheet Control. The BeforeContextMenu event lists the code shown in my original post. I could see that the menu items were defined with statements like:
cmContextMenu(0) = Array("Cu&t", "owc2")
The "Cu&t" is the menu item label, and the "owc2" is the constant (I assume) that is supposed to execute the Cut command (commands are known as "OCCommands" in OWC11). The problem is that the "owc2" constant wasn't firing the Cut command. I checked the "Programming Information" section of the Help, underneath "Enumerations," in the entry titled "OCCommandID" for the OCCommand ID constants. That's exactly where they are located. I looked up the Cut constant, which is 1001, and replaced the "owc2" with 1001, and the Cut command worked perfectly! I did the same thing for the "&Copy" and "&Paste" commands, with 1002 and 1003, respectively, and they worked as well.
My problem may be that I didn't set up the proper references to the OWC constants. Or it could be that the constants changed and weren't updated correctly in the Help documentation. Whichever may be the culprit, the above method fixed the problem.
I am trying to look up some data in an excel sheet to populate a field in a Lotus Notes app on demand. I am using an Action button with LotusScript like so:
Dim v As String
Dim v2 As String
'Open XL sheet for access to data
Set oExcel = CreateObject ( "Excel.Application" )
v="\\msp2\mi\CSD\Confidential\IT and PPST budget.xlsm"
Msgbox("opening " & v)
Set oWorkbook = oExcel.workbooks.open(v)
Set oWorkSheet= oWorkbook.worksheets (4)
v2=Cstr(oWorkSheet.Cells(1,1).value)
Messagebox(v2)
This code does work in that it pulls data from cell A1 - but which sheet?
The sheet containing the data I want is "Sheet4" renamed as "Logic-Detail" but if I use 4 as a parameter as above I get data from the 4th sheet from the left. I need to be able to cope with sheets being hidden as well. I spent 20 minutes on MSDN's excel object model "help" getting nowhere :-(
I feel sure it must be dead easy when you know the answer !
Guessing at the correct syntax is frustrating, isn't it? When referring to sheets in Excel-VBA there are several options:
You can list them by index-number, as you already did in the code-sample in your question
You can refer to it by its name, in your case this would probably be oWorkbook.Worksheets("Logic-Detail")
You can refer to it by its codename, in your case this would probably be oWorkbook.Sheet4. The codename can be changed when you view the properties of the worksheet in the VBA editor.
There may be even more ways to refer to the sheet, but these are the ones which come to mind at the moment. As we know from the question and comments, at least the two first options also work in LotusScript.