Different PPT templates reacting different on the same macros - vba

I'm new here, so I might have not seen a possibility to upload my problem files, which would make it easier to describe the problem.
Edit: The files a here: https://drive.google.com/file/d/0B--IbmtX58h8TnVrdlRyUXZ5a2dEOVJBQkplVjFuVEVMVXhJ/view?usp=docslist_api
and: https://drive.google.com/file/d/0B--IbmtX58h8TFR6d3FkWlZpSGFVUGF5bHVhRTR5ZTlnbXAw/view?usp=docslist_api
(Thank you for the idea, Steve)
What it is about:
I have two documents with different master templates reacting completely different on the same set of macros and I have no idea how and why this can happen and how to repair or avoid it.
Two of the macros just create objects - one is a single textbox, the other one a group of a rectangle and a textbox . The first mentioned appears on the position defined in the code in one of the templates, but a bit below it in the other one. Even more strange is the behavior of the group. The rectangle appears on the correct position in both of the templates, the textbox only in one of it.
Next is a macro for increasing the paragraphing between text lines by 3 pt. It works fine in one template, but in the other template it increases the spacing by 43.2 pt!
Macro number four is made to set back the paragraphing space after back to 0. This one works fine in both templates.
Funny enough, the mistakes appear in opposite to each other. The single textbox and the group produce their error in the template, where the spacing tool works fine, and the spacing tool does strange things in the template where the single textbox and the group work well.
Any idea will be appreciated!
Thanks,
RG
I work with PowerPoint 2010.

Your footnote is getting misplaced because the default text settings in one presentation are different from those in the other; in this case the auto fit setting.
' in this section of your FOOTNOTE routine:
With .TextFrame
' Add this next line and it will work as expected
.AutoSize = ppAutoSizeNone
.TextRange.Text = "Note: " & vbCrLf & "Source: "
.VerticalAnchor = msoAnchorBottom
Likewise, in your SectionMarker subroutine:
With .TextFrame
' add this
.AutoSize = ppAutoSizeNone
' then the rest of your code
Then it all works as you'd expect. Or at least, it works the same with both templates.

Related

Set background color of headers and footers

I have been able to change the background color of Word tables in the body of the document with: oSourceTable.Shading.BackgroundPatternColor = wdRed. However, the same code does not work for header or footer Word tables. I have also tried setting the Section back color as well, but to no avail. The code runs, but the Headers and Footers are always displayed with white backgrounds.
I have done mostly Excel VBA, and only a little bit of Word VBA so maybe I am missing something obvious here. Thanks in advance for any ideas and/or suggestions.
As requested, here is the code I am using. vTableBackColors is just an array of colors. This code is for the body tables and is working perfectly.
For Each oWordTable In oWordDoc.Tables
lIndex = lIndex + 1
oWordTable.Shading.BackgroundPatternColor = vTableBackColors(lIndex)
Next
I tried to do the same thing for the header and footer tables, but it does not work. I tried using the Header/Footer tables as below.
For Each oWordSection In oWordDoc.Sections
For Each oWordTable In oWordSection.Headers.Item(wdHeaderFooterPrimary).Range.Tables
' I selected this one to see if it would make a difference.
oWordTable.Select
oWordTable.Shading.BackgroundPatternColor = m_HeaderBackColor
Next
For Each oWordTable In oWordSection.Footers.Item(wdHeaderFooterPrimary).Range.Tables
oWordTable.Shading.BackgroundPatternColor = m_FooterBackColor
Next
Next
I also tried using the Section Headers / Footers directly. Before and after checking shows that the BackgroundPatternColor has changed as desired, but it is not displayed.
oWordSection.Headers.Item(wdHeaderFooterPrimary).Range.Shading.BackgroundPatternColor = m_HeaderBackColor
Here is a screenshot showing the colorized body tables and the unchanged header.
Is it possible that, unlike the Body table colors, the Header / Footer colors are never displayed as such by the Word designer, but are only true at runtime?
Thanks for any additional information.
«I have been able to change the background color of Word tables in the body of the document with: oSourceTable.Shading.BackgroundPatternColor = red.» That would not work unless you have defined 'red' as an RGB value.
As for the header/footer issue, this works for me:
For Each oWordSection In oWordDoc.Sections
For Each oWordTable In oWordSection.Headers.Item(wdHeaderFooterPrimary).Range.Tables
oWordTable.Shading.BackgroundPatternColorIndex = wdRed
Next
For Each oWordTable In oWordSection.Footers.Item(wdHeaderFooterPrimary).Range.Tables
oWordTable.Shading.BackgroundPatternColorIndex = wdRed
Next
Next
Perhaps your underlying problem is in the value you have assigned to 'm_HeaderBackColor' and 'm_FooterBackColor' - which your posted code doesn't show. It's also possible your code is addressing the wrong headers/footers (i.e. maybe it's not the primary ones you need to address).

Formatting issues duplicating table

Forward
I am making a "label program" that will print waybill information. Consists of a table in Word on a custom 3x5 inch document with 0 margins.
I currently have a simple form that, if you need copies, it will edit one of the cells so that each time it prints the "pieces count" is incremented. 1of10, 2of10, 3of10.....
While that worked the code submitted a separate print job for each "label". That created a problem for the end user where they would have to wait about 5-10 seconds between print jobs. When printing a couple of hundred of these at a time those seconds can add up.
Corrective solution
To try an alleviate this problem I wanted to make copies of the table so that 100 labels would be printed as one 100 page document. Found several solutions for copying pages of text but I have tables which complicated things. The closest solution I have found was:
With ActiveDocument
.Tables(1).Range.Copy
.Range.Select
'.Range.InsertAfter (Chr(11))
Selection.Collapse wdCollapseEnd
Selection.Paste
End With
And this does make a perfect copy of the table however it is merging the tables together. So if I wanted to loop this to create several more labels it would be doubling up the results every time since the code above just copies the first tabel.
To try and fix that issue I added a line break (vertical tab) before the paste. You will see that as the commented out line in the above snippet. This breaks up the tables but adds too much whitespace in between.
Page breaks seems like the solution here. While those did make the table break up they ended up creating a blank page in between each label which I was having enough of a time clearing from the GUI let alone in VBA.
The actual question
How can I take a table that is perfectly designed to fit on one 3x5 inch page and duplicate it X times. The caveat is I need to be able to find the cell programically that contains the pieces text. Currently I can use these absolute reference for the first table
ActiveDocument.Tables(1).Cell(5, 1).Range.Text
So if I had 3 tables for instance I need to be able to call each table and edit the text of the Cell(5, 1).
In case you ask
I know this functionality is better placed inside actual label programs like Bartender but those cost money that the company will not allocate for the only label my company uses.
The right concept was there. We needed to add a break in between the tables to disassociate them. Adding the vertical tab Chr(11) was obviously not the correct way to do it.
A proper section break would be the route to go here. Looking at MSDN you can see there are multiple types of section breaks. After testing a desirable outcome was acheived from using wdSectionBreakNextPage which is a "Section break on next page."
Basically just needed to add one line to the above code.
With ActiveDocument
.Tables(1).Range.Copy
.Range.Select
End With
With Selection
.Collapse wdCollapseEnd
.InsertBreak (wdSectionBreakNextPage)
.Paste
End With
I hate the use of Selection and I am going to look into that but for now this does function properly.
Since the tables are not single units we are able to query .Tables in order to edit each page individually.

How to make a whole worksheet use solid fill in the data bars in excel

I'm having a problem with my excel 2010 (and 2013 also) that I have a page
worksheet with data bars, and I have set them to solid fill, but every time
I close and open the document, it has changed to gradient again, quite annoying (since you can't really see the progress when it is fading into white, which is the same as the background...)
Checked the Office Homepage, but couldn't find this issue anywhere mentioned.
Also tried to Google it, just found a few who had similar problem, but no solution.
For the purpose of presentation I could change the background etc. But what is the fun in that, I would like to try to make sure it is solid fill every time I open this file. And it looks nice when it is solid fill.
I see 2 ways this could be achieved:
Would be if one could change the default settings (since I guess that is what happens every time I re-open the file). Tried to Google it but couldn't find it.
Another way would be to have a VBA script that says "hey, all you data bars in this range, you should have solid bars".
I tried option 2 on my own, but since I'm quite new to scripting in VBA, I didn't succeed, but it looked like this my try:
Private Sub Workbook_Open()
dad = Range("A1:Z100")
Dim b As Databar
b = dad.FormatCondition.AddDatabar
b.BarFillType = xlDataBarFillSolid
End Sub
But that didn't work, I guess one should create the bars and then use this options on those bars, but I already have a document with maybe 200 bars, which looks perfectly fine, except for them loosing the solid option.
And since they depend on different numbers (the data bars), I can't have one "rule to rule them all" (conditioning formatting).
Update: 2015-05-22
Managed to create new Databars to my liking, by this code below
Sub TestDatabars()
' column B real values
Dim B1rv As Databar
Range("B6:B10, B14:B37,B42:B43").Select
' remove old settings (gradient fill)
With Selection
.FormatConditions(1).Delete
End With
' add a new one
Set B1rv = Selection.FormatConditions.AddDatabar
With B1rv
.BarFillType = xlDataBarFillSolid
End With
With B1rv.BarColor
.Color = RGB(146, 208, 80)
End With
B1rv.MinPoint.Modify newtype:=xlConditionValueNumber, newvalue:=0
B1rv.MaxPoint.Modify newtype:=xlConditionValueNumber, newvalue:=Range("B1")
end sub
But I guess the question remains if I need to change all my old databars (around 100 databars) or if there is a more common way.
Will test some thing with making a range selection and see if I get edit those allready there.
Try this:
Set b = dad.FormatCondition.AddDatabar
Notice the keyword Set - you need to use that when assigning an object.

VB 2010: ScrollToCaret won't work on plain textbox but will with rich text box

I have written an application in VB 2010 that generates log files, and I wanted to add a feature to view the log files without leaving the app. This was done, naturally, by adding a form with a text box and loading in the file.
Sometimes these files get plenty big, so I was looking for a way to just show the most recent entries -- i.e., the last few lines. I got that figured out (using a pretty cool method that works really fast without iterating through all the lines of text).
Just to make it convenient, I wanted to make it display the very bottom of the text box, so ScrollToCaret() was employed. However, in this particular text box, the ScrollToCaret() function didn't work, no matter how I tried to implement it. If I use a RichTextBox, it works fine, and if that's what I need to do, that's how I'll do it. But I have to know why it wouldn't work in a regular text box.
Here's my code:
Dim intLineCount As Int64 = IO.File.ReadAllLines(strFileName).Length
Dim intStartPosition As Int64 = intLineCount - 1000
Dim intPositionLoop As Int64
Try
Dim strAllLines() As String = File.ReadAllLines(strFileName)
For intPositionLoop = intStartPosition To intLineCount
txtViewLogs.AppendText(strAllLines(intPositionLoop - 1) & vbNewLine)
Next
Catch ex As Exception
End Try
txtViewLogs.Focus()
txtViewLogs.ScrollToCaret()
As this shows, this will display the last 1000 lines of the log file. When the file is completely loaded, the caret is already at the end of the file, so there was no need to set the SelectionStart point to the end of the file, but I even tried that, and no luck.
In order to eliminate the possibility that some property of the text box could have been accidentally changed, I deleted the text box and added another one, taking care not to make any accidental changes to the properties.
Anyone have any idea why it's like this? As I said, if I have to stick with a rich text box, that's what I'll do, but it kills me not knowing why this is doing that. I know I've used this in other applications, but I can't see why it won't work with this one.

Hiding Page Breaks in Excel

Following on from my previous questions:
I have implemented a series of checkboxes with which users can hide/unhide ranges of data. This is great - it works. The problem is when the users go to print - the hidden data is hidden but the pages are still there but blank.
If the page breaks are left to their own devices then everything is fine - there are no blank sections. When manual page breaks are used then you can see where the data was.
I've tried everything to get rid of the blank areas.
Copying the ranges and recalculating the page breaks is a no-go as the page breaks take upwards of 2 minutes for a fairly small report.
So, how can I print only the ranges that aren't hidden?
Thanks, G.
Haven't tested it, but you could change the print range to only the visible cells:
Public Sub SetPrintRangeToVisible(ByRef ws As Excel.Worksheet)
ws.PageSetup.PrintArea = ws.UsedRange.SpecialCells(xlCellTypeVisible).Address
End Sub
Excel, by default prints only visible cells. You wouldnt have to set the Print area to visible cells as previously suggested.
I suppose you have un-checked the Print Object option in all of the Check-boxes. And that area appears blank, when you print the sheet. If that is the case, read on:
I suggest writing a simple macro that hides the rows that contain the check boxes. Set the visible property of the check boxes to false. Print the sheet, and then return everything back to the original state.
Alternatively, you could just select the rows above the check boxes, then the entire area below the check boxes, and then set that as the PrintArea. That should solve the problem you are having.
Also, I may have wildly misunderstood the situation, in which case, I am sorry I wasted your time.