Excell Print VBA code combine with VBA Custom Footer - vba

I need to create a macro to print two sheets and include a custom footer that references several of the cells.
I have tried so many combinations, but I don't know what I am doing wrong. I get the error Object does not support this property or method.
Sub PrintSummarySheet()
' PrintSummarySheet Macro
Sheets("Project Data Input").Select
With ActiveSheet.PageSetup
.CenterFooter = .Range("C6").Text And .Range("F2").Text _
And .Range("F4").Text And .Range("F5").Text
End With
Sheets(Array("Project Data Input", "Project Estimate Summary")).Select
Sheets("Project Data Input").Activate
ActiveWindow.SelectedSheets.PrintOut Copies:=1, Collate:=True, _
IgnorePrintAreas:=False
Sheets("INSTRUCTIONS").Select
Sheets("Project Data Input").Select
End Sub

You are using With ActiveSheet.PageSetup but on the next line you are trying to refer to the Worksheet and not the PageSetup by doing .Range(...).
You need to replace .Range(...) by ActiveSheet.Range(...).
The Run-time error 13 Type mismatch occurs because you are using And to concatenate text instead of the concatenation operator &
.Range("C6").Text And .Range("F2").Text _
And .Range("F4").Text And .Range("F5").Text
Should be:
.Range("C6").Text & .Range("F2").Text & _
.Range("F4").Text & .Range("F5").Text

Related

Print Macro - issues

I am using the below mentioned code to print a sheet. My task is complete, however I get the error message stating "Run-time error '-2147024773 (8007007b)': Document not saved."
Also, in the below code, can I add a text to the file name (other than cell A1 text?). I would like the file name to be name (which is on cell A1) and add a text "- Workpaper" in the end.
Can some one help?
Sub PrintFile()
With ActiveSheet
.ExportAsFixedFormat Type:=xlTypePDF, _
Filename:="C:\Foldername\" & Range("A1").Text, _
Quality:=xlQualityStandard, _
IncludeDocProperties:=True, _
IgnorePrintAreas:=False, _
OpenAfterPublish:=False
End With
End Sub
Try changing "Range("A1").Text" to "Range("A1").Value"
Text vs Value
Also, you should be checking for a valid filename prior to using the value.
Function ValidateFileName(ByVal name As String) As Boolean
' Check for nothing in filename.
If name Is Nothing Then
ValidateFileName = False
End If
' Determines if there are bad characters.
For Each badChar As Char In System.IO.Path.GetInvalidPathChars
If InStr(name, badChar) > 0 Then
ValidateFileName = False
End If
Next
' If Name passes all above tests Return True.
ValidateFileName = True
End Function
First of all type e.g. "test" into A1 cell - probably there is something wrong with the filename. You can also use Excels's data validation or some VBA code to sanitizate the filename. You can also add some check if directory exists to make sure it isn't a problem.
Sub PrintFile()
' check if folder exists
If Dir("C:\Foldername\", vbDirectory) = "" Then
MkDir "C:\Foldername\"
End If
' check if name in A1 is not blank
If IsEmpty(Range("A1")) Then
MsgBox "Fill A1 Cell with name of the pdf file first"
Else
With ActiveSheet
.ExportAsFixedFormat Type:=xlTypePDF, _
Filename:="C:\Foldername\" & Range("A1").Value & "- Workpaper", _
Quality:=xlQualityStandard, _
IncludeDocProperties:=True, _
IgnorePrintAreas:=False, _
OpenAfterPublish:=False
End With
End If
End Sub
So there is a problem with your path or/and filename. Maybe it is a Mac and C:\ is not proper address?
The reason for this error might be insufficient privileges or any invalid characters in the name of the file.
Could you try to save it in a different drive than C and see if that works?
Replace this line of your code with below line to add -Workpaper at the end of your file name.
Assuming that you're trying to save this in D drive in a temp folder.
Filename:="D:\temp\" & Range("A1").Text & "-Workpaper", _

Excel VBA error 1004 when trying to set multiple PageSetup footer values

I am trying to use Excel-VBA to set the PageSetup information for my worksheet, but it is giving me the following error:
Run-time error ‘1004’:
Unable to set the LeftFooter property of the PageSetup class
The relevant example code follows:
Set WS = ActiveWorkbook.ActiveSheet
WS.DisplayPageBreaks = False
With WS.PageSetup
.PrintArea = "$A$1:$L$32"
.Orientation = xlLandscape
.CenterHeader = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" & Chr(10) & _
"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
.CenterFooter = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" & Chr(10) & _
"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
.RightFooter = "XXXXXXXXXXXXXXXXXXXXXXX" & Chr(10) & _
"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
.LeftFooter = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" & Chr(10) & _
"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" & Chr(10) & _
"XXXXXXXXXXXXXXXXXXXXXXXXXX"
.CenterHorizontally = True
.Zoom = False
End With
Strangely, if I comment out any one of the three .xxxFooter lines, the code works fine. If I rearrange them into any possible different order, they all work with the exception of the last one which will throw the same 1004 error but for the new .xxxFooter.
This user seems to have a highly related problem, but did not ever resolve it: Error when setting Worksheet.PageSetup.XxxFooter. As a comment to the questions there, yes, I do have a printer installed.
Edit:
Another discovery: if I remove some of the characters, then it will work again. Adding in additional characters manually eventually caused this error box to pop up:
The text string you entered is too long. Reduce the number of characters used.
I can temporarily work around by abbreviating a lot of things in the footer, but is there any other way to solve this problem, and can anyone explain why there is a character limit in the footers? That seems arcane in Excel 2016...

Word - Save As object

How can I save a Word document as the value in a table? I've tried this and get a "Type mismatch" error:
Sub saveas_cell()
ActiveDocument.SaveAs FileName:= _
"c:\mydocuments" ActiveDocument.Tables(1).Cell(1, 2) & ".doc"
End Sub
I've also tried an object reference (activedocument.table1.("text3"))
Thanks for the help!
You get an error message because Table.Cell does not return a string, but a cell object. Instead, use this:
"c:\mydocuments" ActiveDocument.Tables(1).Cell(1, 2).Shape.TextFrame.TextRange.Text & ".doc"
You can find more information here.

How do I insert the text of the Split function into different lines inside a table in MS Word

I need help with some code. I built a User Form in word using Bookmarks as references to where the text in each TextBox should go, I was able to accomplish that. The challenge I'm having now is using the Split Function. I want to grab each word from TextBox3 and place them in a table, then I want to search each word in an excel database and retrieve the info on the cell next to it (in a Vlookup sort of way). Each word from the TextBox3 should be in a different line.
Here is the code:
Private Sub CommandButton1_Click()
With ActiveDocument
.Bookmarks("bmCN").Range _
.InsertBefore TextBox1
.Bookmarks("bmOriJob").Range _
.InsertBefore TextBox2
.Bookmarks("bmOptJob").Range _
.InsertBefore TextBox3
.Bookmarks("bmJobD").Range _
.InsertBefore TextBox4
.Bookmarks("bmJobRes").Range _
.InsertBefore TextBox5
.Bookmarks("bmJobR").Range _
.InsertBefore TextBox6
.Bookmarks("bmBen").Range _
.InsertBefore TextBox7
.Bookmarks("bmTag").Range _
.InsertBefore TextBox8
End With
UserForm1.Hide
Selection.WholeStory
Selection.Fields.Update
Selection.Collapse Direction:=wdCollapseEnd
End Sub
Any help would be appreciated.
Try something like this with array and loop:
(see some comments inside the code below)
'let's create temporary array
Dim tmpArray As Variant
tmpArray = Split(TextBox3, " ")
Dim i As Integer
For i = 1 To UBound(tmpArray)
'we will load values to first column in first table in yur document
ActiveDocument.Tables(1).Cell(i, 1).Range = tmpArray(i - 1)
'here do your stuff with excel- load what you need
'ActiveDocument.Tables(1).Cell(i, 2).Range = something from excel
Next i

Run Time Error 5 - Invalid Procedure Call or Argument

I need help with this macro. Every time I run it, I get the error below. I thought it was a simple macro that I could have anybody on my team use to make it take a less time than they were taking to manually create this PivotTable every time they ran the report. However, it's not working. Please see error below and advise. I emboldened and italicized the error.
Sub LEDOTTR()
'
' LEDOTTR Macro
'
'
Range("A87").Select
Range(Selection, ActiveCell.SpecialCells(xlLastCell)).Select
***ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
"Sheet1!R87C1:R8214C25", Version:=xlPivotTableVersion14).CreatePivotTable _
TableDestination:="LED OTTR!R1C1", TableName:="PivotTable6", _
DefaultVersion:=xlPivotTableVersion14***
Sheets("LED OTTR").Select
Cells(1, 1).Select
With ActiveSheet.PivotTables("PivotTable6").PivotFields("LED")
.Orientation = xlPageField
.Position = 1
End With
With ActiveSheet.PivotTables("PivotTable6").PivotFields("Hierarchy name")
.Orientation = xlRowField
.Position = 1
End With
ActiveSheet.PivotTables("PivotTable6").PivotFields("LED").CurrentPage = "(All)"
With ActiveSheet.PivotTables("PivotTable6").PivotFields("LED")
.PivotItems("LED Marine").Visible = False
.PivotItems("LL48 Linear LED").Visible = False
.PivotItems("Other").Visible = False
End With
ActiveSheet.PivotTables("PivotTable6").PivotFields("LED"). _
EnableMultiplePageItems = True
ActiveSheet.PivotTables("PivotTable6").AddDataField ActiveSheet.PivotTables( _
"PivotTable6").PivotFields(" Late " & Chr(10) & "Indicator"), "Sum of Late " & Chr(10) & "Indicator", _
xlSum
ActiveSheet.PivotTables("PivotTable6").AddDataField ActiveSheet.PivotTables( _
"PivotTable6").PivotFields("Early /Ontime" & Chr(10) & " Indicator"), _
"Sum of Early /Ontime" & Chr(10) & " Indicator", xlSum
End Sub
The answer to your problem is located here.
Your sheet name in TableDestination:="LED OTTR!R1C1" needs to be surrounded with single quotes in order for it to work TableDestination:="'LED OTTR'!R1C1"
You will also have problems with the duplicated name if you do not delete this PivotTable before rerunning the code.
In my case the trouble was related to the region settings in Windows. I downloaded a (protected) xlsm file from the internet and always got the "Run Time Error 5 - Invalid Procedure Call or Argument" error when opening it. The other answer hinted to me that it may have to do with the language settings. The scripts in the file were obviously programmed in German while my Windows was set to an English region.
In Windows 10 Settings > Time & Language > Region I changed Region as well as Regional format to German. After restarting Excel, the file worked.
I've run into this, and the error was caused by calling the Worksheet.ExportAsFixedFormat method on a hidden worksheet.
I just added the if worksheet.Visible Then condition to my code to prevent getting the Invalid Procedure Call or Argument error.
While installing macro file to excel 2016 ribbon window getting below error when I debug the error. Below section was highlighed in yellow collor when clicked on debug
Set one = Application.CommandBars("Worksheet Menu Bar").Controls.Add(Type:= _
msoControlPopup, before:=onecount)