Reference Excel cell in PowerPoint macro for filename - vba

I can't get SaveAsFixedFormat working from Excel VBA to export a PowerPoint file as PDF. I have resorted to starting a macro in the preset-powerpoint from Excel VBA that exports the presentation as pdf directly from PowerPoint.
Is there any way to reference a cell in the Excel file in this macro that is running in PowerPoint to get the filename?
Sub pppdf()
ActivePresentation.ExportAsFixedFormat "M:\random\test.pdf", 32
End Sub
I can save the PowerPoint file as .pptx from Excel and use varying filenames and paths but now I would like to reference those same paths and filenames in the PowerPoint macro that is exporting to pdf.
In the end I'd like the code to look somewhat like this but this obviously needs some work to function from PowerPoint:
Dim FName As String
Dim FPath As String
FPath = Range("SavingPath").Value
FName = Sheets("randomworksheet").Range("A1").Text
ActivePresentation.ExportAsFixedFormat FPath & FName & " Development" & ".pdf", 32
This PowerPoint macro would be started from Excel and both the PowerPoint file and the Excel Workbook and sheet will be open when this is executed.

Why not open the presentation and save it as a PDF from Excel if the main bulk of the code is in Excel anyway?
Sub SavePPTXasPDF()
Dim PPT As Object
Dim PP As Object
Set PPT = CreatePPT
Set PP = PPT.Presentations.Open("<FullPathToPresentation>")
PP.SaveAs ThisWorkbook.Path & Application.PathSeparator & "ABC", 32 'ppSaveAsPDF
End Sub
Public Function CreatePPT(Optional bVisible As Boolean = True) As Object
Dim oTmpPPT As Object
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'Defer error trapping in case Powerpoint is not running. '
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
On Error Resume Next
Set oTmpPPT = GetObject(, "Powerpoint.Application")
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'If an error occurs then create an instance of Powerpoint. '
'Reinstate error handling. '
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
If Err.Number <> 0 Then
Err.Clear
On Error GoTo ERROR_HANDLER
Set oTmpPPT = CreateObject("Powerpoint.Application")
End If
oTmpPPT.Visible = bVisible
Set CreatePPT = oTmpPPT
On Error GoTo 0
Exit Function
ERROR_HANDLER:
Select Case Err.Number
Case Else
MsgBox "Error " & Err.Number & vbCr & _
" (" & Err.Description & ") in procedure CreatePPT."
Err.Clear
End Select
End Function
or if you want to run the code in Powerpoint:
Public Sub Test()
Dim oXL As Object
Dim oWB As Object
Dim FName As String
Dim FPath As String
Set oXL = CreateXL
Set oWB = oXL.workbooks.Open("<Path&FileName>")
'Or if Workbook is already open:
'Set oWB = oXL.workbooks("<FileName>")
FPath = oWB.worksheets("Sheet1").Range("A1")
FName = oWB.worksheets("Sheet1").Range("A3")
ActivePresentation.ExportAsFixedFormat FPath & FName & " Development" & ".pdf", 32
End Sub
Public Function CreateXL(Optional bVisible As Boolean = True) As Object
Dim oTmpXL As Object
'''''''''''''''''''''''''''''''''''''''''''''''''''''
'Defer error trapping in case Excel is not running. '
'''''''''''''''''''''''''''''''''''''''''''''''''''''
On Error Resume Next
Set oTmpXL = GetObject(, "Excel.Application")
'''''''''''''''''''''''''''''''''''''''''''''''''''''''
'If an error occurs then create an instance of Excel. '
'Reinstate error handling. '
'''''''''''''''''''''''''''''''''''''''''''''''''''''''
If Err.Number <> 0 Then
Err.Clear
On Error GoTo ERROR_HANDLER
Set oTmpXL = CreateObject("Excel.Application")
End If
oTmpXL.Visible = bVisible
Set CreateXL = oTmpXL
On Error GoTo 0
Exit Function
ERROR_HANDLER:
Select Case Err.Number
Case Else
MsgBox "Error " & Err.Number & vbCr & _
" (" & Err.Description & ") in procedure CreateXL."
Err.Clear
End Select
End Function
Or you could, as you requested, open the presentation from within Excel and execute code stored in the presentation:
Sub SavePPTXasPDF()
Dim PPT As Object
Dim PP As Object
Set PPT = CreatePPT
Set PP = PPT.Presentations.Open("<FullPath>")
PPT.Run PP.Name & "!Test"
End Sub
This would use the Test macro and use the Set oWB = oXL.workbooks("<FileName>") line of code which is currently commented out in my example above.

What problem are you facing using ExportAsFixedFormat directly from the Excel VBE? According to the documentation (which seems to be incorrect) and the PowerPoint VBE IntelliSense, the second argument, FixedFormatType can only be one of two values:
ExportAsFixedFormat(Path, FixedFormatType, Intent, FrameSlides, _
HandoutOrder, OutputType, PrintHiddenSlides, PrintRange, _
RangeType, SlideShowName, IncludeDocProperties, KeepIRMSettings)
FixedFormatType:
ppFixedFormatTypePDF = 2
ppFixedFormatTypeXPS = 1

Related

HOW To manipulate an ALREADY open word document from excel vba

I am new to VBA and obviously I am missing something. My code works for opening a word doc and sending data to it BUT does NOT for an ALREADY OPEN word doc. I keep searching for an answer on how to send info from Excel to an OPEN Word doc/Bookmark and nothing works.
I hope it is okay that I added all the code and the functions called. I really appreciate your help!
What I have so far
Sub ExcelNamesToWordBookmarks()
On Error GoTo ErrorHandler
Dim wrdApp As Object 'Word.Application
Dim wrdDoc As Object 'Word.Document
Dim xlName As Excel.Name
Dim ws As Worksheet
Dim str As String 'cell/name value
Dim cell As Range
Dim celldata As Variant 'added to use in the test
Dim theformat As Variant 'added
Dim BMRange As Object
Dim strPath As String
Dim strFile As String
Dim strPathFile As String
Set wb = ActiveWorkbook
strPath = wb.Path
If strPath = "" Then
MsgBox "Please save your Excel Spreadsheet & try again."
GoTo ErrorExit
End If
'GET FILE & path of Word Doc/Dot
strPathFile = strOpenFilePath 'call a function in MOD1
If strPathFile = "" Then
MsgBox "Please choose a Word Document (DOC*) or Template (DOT*) & try again." 'strPath = Application.TemplatesPath
GoTo ErrorExit
End If
If FileLocked(strPathFile) Then 'Err.Number = 70 if open
'read / write file in use 'do something
'NONE OF THESE WORK
Set wrdApp = GetObject(strPathFile, "Word.Application")
'Set wrdApp = Word.Documents("This is a test doc 2.docx")
'Set wrdApp = GetObject(strPathFile).Application
Else
'all ok 'Create a new Word Session
Set wrdApp = CreateObject("Word.Application")
wrdApp.Visible = True
wrdApp.Activate 'bring word visiable so erros do not get hidden.
'Open document in word
Set wrdDoc = wrdApp.Documents.Open(Filename:=strPathFile) 'Open vs wrdApp.Documents.Add(strPathFile)<=>create new Document1 doc
End If
'Loop through names in the activeworkbook
For Each xlName In wb.Names
If Range(xlName).Cells.Count = 1 Then
celldata = Range(xlName.Value)
'do nothing
Else
For Each cell In Range(xlName)
If str = "" Then
str = cell.Value
Else
str = str & vbCrLf & cell.Value
End If
Next cell
'MsgBox str
celldata = str
End If
'Get format and strip away the spacing, negative color etc etc
'I know this is not right... it works but not best
theformat = Application.Range(xlName).DisplayFormat.NumberFormat
If Len(theformat) > 8 Then
theformat = Left(theformat, 5) 'was 8 but dont need cents
Else
'do nothing for now
End If
If wrdDoc.Bookmarks.Exists(xlName.Name) Then
'Copy the Bookmark's Range.
Set BMRange = wrdDoc.Bookmarks(xlName.Name).Range.Duplicate
BMRange.Text = Format(celldata, theformat)
'Re-insert the bookmark
wrdDoc.Bookmarks.Add xlName.Name, BMRange
End If
Next xlName
'Activate word and display document
With wrdApp
.Selection.Goto What:=1, Which:=2, Name:=1 'PageNumber
.Visible = True
.ActiveWindow.WindowState = wdWindowStateMaximize 'WAS 0 is this needed???
.Activate
End With
GoTo WeAreDone
'Release the Word object to save memory and exit macro
ErrorExit:
MsgBox "Thank you! Bye."
Set wrdDoc = Nothing
Set wrdApp = Nothing
Exit Sub
'Error Handling routine
ErrorHandler:
If Err Then
MsgBox "Error No: " & Err.Number & "; There is a problem"
If Not wrdApp Is Nothing Then
wrdApp.Quit False
End If
Resume ErrorExit
End If
WeAreDone:
Set wrdDoc = Nothing
Set wrdApp = Nothing
End Sub
file picking:
Function strOpenFilePath() As String
Dim intChoice As Integer
Dim iFileSelect As FileDialog 'B
Set iFileSelect = Application.FileDialog(msoFileDialogOpen)
With iFileSelect
.AllowMultiSelect = False 'only allow the user to select one file
.Title = "Please... Select MS-WORD Doc*/Dot* Files"
.Filters.Clear
.Filters.Add "MS-WORD Doc*/Dot* Files", "*.do*"
.InitialView = msoFileDialogViewDetails
End With
'make the file dialog visible to the user
intChoice = Application.FileDialog(msoFileDialogOpen).Show
'determine what choice the user made
If intChoice <> 0 Then
'get the file path selected by the user
strOpenFilePath = Application.FileDialog( _
msoFileDialogOpen).SelectedItems(1)
Else
'nothing yet
End If
End Function
checking if file is open...
Function FileLocked(strFileName As String) As Boolean
On Error Resume Next
' If the file is already opened by another process,
' and the specified type of access is not allowed,
' the Open operation fails and an error occurs.
Open strFileName For Binary Access Read Write Lock Read Write As #1
Close #1
' If an error occurs, the document is currently open.
If Err.Number <> 0 Then
' Display the error number and description.
MsgBox "Function FileLocked Error #" & str(Err.Number) & " - " & Err.Description
FileLocked = True
Err.Clear
End If
End Function
ANSWER BELOW. Backstory... So, after input from you guys and more research I discovered that I needed to set the active word document by using the file selection the user picked and that is then passed via late binding to the sub as an object to process. NOW it works if the word file is not in word OR if it is currently loaded into word AND not even the active document. The below code replaces the code in my original question.
Set Object app as word.
grab the file name.
Make the word doc selected active to manipulate.
Set the word object to the active doc.
THANK YOU EVERYONE!
If FileLocked(strPathFile) Then 'Err.Number = 70 if open
'read / write file in use 'do something
Set wrdApp = GetObject(, "Word.Application")
strPathFile = Right(strPathFile, Len(strPathFile) - InStrRev(strPathFile, "\"))
wrdApp.Documents(strPathFile).Activate ' need to set picked doc as active
Set wrdDoc = wrdApp.ActiveDocument ' works!
This should get you the object you need.
Dim WRDFile As Word.Application
Set WRDFile = GetObject(strPathFile)
'Have Microsoft Word 16.0 Object Library selected in your references
Dim wordapp As Object
Set wordapp = GetObject(, "Word.Application")
wordapp.Documents("documentname").Select
'works if you only have one open word document. In my case, I'm trying to push updates to word links from excel.

Please close Excel application - Excel is open

I am not a VB person but I am asked to troubleshoot this issue. We have an Access database that is exporting two Access reports to an Excel workbook. It has been working for years. Recently we are getting an error message that the Excel application is open and must be closed. Both the database and Access template are on a network share drive. From what I can see we are not getting past this point. The server does not show Excel as being opened at the time of the error. I thank you in advance for your assistance.
Here is my code:
Private Sub ExportCounts_Excel()
Dim excelname As String
Dim AppExcel As New Excel.Application
Dim Wkb As Workbook
Dim Wksh As Worksheet
Dim Wksh1 As Worksheet
Dim Wksh2 As Worksheet
Dim obj As AccessObject
Dim dbs As Object
Dim rs As Object
Dim rstable As Object
Dim tempTable As String
Dim data As String
Dim Agent As String
Dim Name As String
Dim newfile As String
Dim tic As String
Dim lastrow As Long
Dim count As Integer
Dim recount As Integer
On Error GoTo Errorcatch
DoCmd.SetWarnings False
'*****************************************************************************
Dim fso As Object
Set fso = VBA.CreateObject("Scripting.FileSystemObject")
Call fso.CopyFile("\\cfbf-sql\mbdb\Counts Reports Template.xlsm", "\\cfbf-sql\itdb\IT-Test DBs\counts\Counts Reports.xls")
'see if the excel app is running
Dim MyXL As Object 'Variable to hold reference
Dim ExcelWasNotRunning As Boolean 'Flag for final release
On Error Resume Next
Set MyXL = GetObject(, "Excel.Application")
If Err.Number <> 0 Then
ExcelWasNotRunning = True
End If
'Check if the Excel Application is running
If ExcelWasNotRunning = True Then
'If Excel is running then.............
MsgBox "Please Close your Excel Application" & vbCrLf _
& "and save your files before attempting" & vbCrLf _
& "to run the report", vbInformation, _
"Microsoft Excel is open"
Set MyXL = Nothing
Exit Sub
Else 'Excel is not running
'Optional - to storage the file name entered by user
Dim Message, Title, Default, MyValue
Message = "Enter a name for the file" ' Set prompt.
Title = "Assign File Name" ' Set title.
'Format date to use it as file name and report title
Dim varMonthNum As Variant
Dim varDayNum As Variant
Dim varYear As Variant
Dim varFileDate As Variant
'Get the month, day, and year from LastFriday text box
varMonthNum = Month(LastFriday.Value)
varDayNum = Day(LastFriday.Value)
varYear = Year(LastFriday.Value)
'Format the date to assign it as part of the file name
varFileDate = varMonthNum & "-" & varDayNum & "-" & varYear
'use the following variable to format the file name
Default = Me.CurrentYear.Value & " CFBF Membership Report as of " & varFileDate ' Set default.
' Display message, title, and default value.
MyValue = InputBox(Message, Title, Default)
If StrPtr(MyValue) = 0 Then 'IF the vbCancel Button is selected by the user
'Exit the procedure
Exit Sub
Else 'Create the excel report
'*****************************************************************************
'excelname = "\\member2\MBDB\Counts Reports Template.xls"
excelname = "\\cfbf-sql\MBDB\Counts Reports Template.xls"
'For the new fiscal year 2014
'newfile = "\\web3\FBMNData\WEEKLY COUNTY REPORTS 2011\" & MyValue & ".xls"
'newfile = "\\web3\FBMNData\WEEKLY COUNTY REPORTS 2013\" & MyValue & ".xls"
'newfile = "\\web3\FBMNData\WEEKLY COUNTY REPORTS 2014\" & MyValue & ".xls"
'newfile = "\\web3\FBMNData\WEEKLY COUNTY REPORTS 2015\" & MyValue & ".xls"
'==============================================================================
'**** Comments by: Armando Carrasco - 11/21/2014 ***
'**** MMR - Kate Tscharner - requested to stop posting excel file in ***
'**** the counties FTP site and to place the file in the everyone folder ***
'**** MMR also requested to move all "WEEKLY COUNTY REPORTS YYYY" folders ***
'**** from WEB3 to "\\cfbf-fp\Everyone\MembershipReports\" ***
'newfile = "\\cfbf-fp\Everyone\MembershipReports\WEEKLY COUNTY REPORTS 2015\" & MyValue & ".xls"
'==============================================================================
'**** Comments by: Armando Carrasco - 01/21/2014 ***
'**** MMR - Kate Tscharner - WO 1284 - Comments ***
'**** We have had the request from several county Farm Bureaus to restore ***
'**** Placing the old network directory location in WEB3. ***
newfile = "\\cfbf-reports\FBMNData\WEEKLY COUNTY REPORTS 2017\" & MyValue & ".xls"
'==============================================================================
I'd suggest re-organizing a bit:
Dim MyXL As Object 'Variable to hold reference
Dim ExcelWasRunning As Boolean 'Flag for final release
On Error Resume Next '<< ignore error if Excel not running
Set MyXL = GetObject(, "Excel.Application")
On Error Goto 0 '<< cancel the On Error Resume Next so you
' don't miss later (unexpected) issues
ExcelWasRunning = Not MyXL Is Nothing '<< If Excel was running then MyXL
' is set to the Excel instance
If ExcelWasRunning Then
MsgBox "Please Close your Excel Application" & vbCrLf _
& "and save your files before attempting" & vbCrLf _
& "to run the report", vbInformation, _
"Microsoft Excel is open"
Set MyXL = Nothing
Exit Sub '<< Shouldn't really need this, since the rest of your code
' is in the Else block...
Else
'Excel is not running
'Rest of your code here
End If

How to remove missing references?

I am using Outlook 2016 and Word 2016.
I have users with Outlook and Word 2013 which requires them to have a reference to the Outlook Library.
I have code that should check for and remove broken references and then add the references that I specified.
It does not remove the missing references so I remove the missing libraries manually and then run the code to add them.
This is the code, found on a MS Community Forum, which works under other circumstances:
Sub AddReference()
Dim strGUID(1 To 7) As String, theRef As Variant, i As Long
strGUID(1) = "{00062FFF-0000-0000-C000-000000000046}" ' Reference for Outlook library (see below reference printer to get more codes)
strGUID(2) = "{00020905-0000-0000-C000-000000000046}" ' Reference for Word library (see below reference printer to get more codes)
strGUID(3) = "{000204EF-0000-0000-C000-000000000046}" ' Reference for VBA library (see below reference printer to get more codes)
strGUID(4) = "{00020813-0000-0000-C000-000000000046}" ' Reference for Excel library (see below reference printer to get more codes)
strGUID(5) = "{2DF8D04C-5BFA-101B-BDE5-00AA0044DE52}" ' Reference for Office library (see below reference printer to get more codes)
strGUID(6) = "{0D452EE1-E08F-101A-852E-02608C4D0BB4}" ' Reference for MS Forms (see below reference printer to get more codes)
strGUID(7) = "{420B2830-E718-11CF-893D-00A0C9054228}" ' Reference for scripting (see below reference printer to get more codes)
On Error Resume Next
'Remove any missing references
For i = ThisWorkbook.VBProject.References.Count To 1 Step -1
Set theRef = ThisWorkbook.VBProject.References.Item(i)
If theRef.isbroken = True Then
ThisWorkbook.VBProject.References.Remove theRef
End If
Next i
For i = 1 To 7
'Clear any errors so that error trapping for GUID additions can be evaluated
Err.Clear
'Add the reference
ThisWorkbook.VBProject.References.AddFromGuid _
GUID:=strGUID(i), Major:=1, Minor:=0
'If an error was encountered, inform the user
Select Case Err.Number
Case Is = 32813
'Reference already in use. No action necessary
Case Is = vbNullString
'Reference added without issue
Case Else
'An unknown error was encountered, so alert the user
MsgBox "A problem was encountered trying to" & vbNewLine _
& "add or remove a reference in this file" & vbNewLine & "Please check the " _
& "references in your VBA project!", vbCritical + vbOKOnly, "Error!"
End Select
Next i
On Error GoTo 0
End Sub
This isn't the answer you're after as it doesn't deal with removing VBA references, etc.
It does show how to get MS Applications talking to each other without setting references though.
I've tested this on Word 2010, Outlook 2010 (had to change Application.PathSeparator to \), Excel 2003 and Excel 2010.
'Create an instance of Word & Outlook.
'Create a Word document and save it.
'Create an email and attach Word document to it.
Public Sub Test()
Dim oL As Object
Dim oW As Object
Dim nS As Object
Dim oMsg As Object
Dim oDoc As Object
Dim sDesktop As String
'Find the desktop.
sDesktop = CreateObject("WScript.Shell").specialfolders("Desktop")
'Create and save a Word document to the desktop.
Set oW = CreateWD
Set oDoc = oW.Documents.Add(DocumentType:=0) 'wdNewBlankDocument
oDoc.SaveAs sDesktop & Application.PathSeparator & "TempDoc"
'Create and save an email message, attach the Word doc to it.
Set oL = CreateOL
Set nS = oL.GetNamespace("MAPI")
Set oMsg = oL.CreateItem(0)
With oMsg
.To = "someaddress#somedomain"
.Body = "My Message"
.Subject = "My Subject"
.Attachments.Add sDesktop & Application.PathSeparator & "TempDoc.docx"
.Display 'or .Send
.Save
End With
End Sub
' Purpose : Creates an instance of Outlook and passes the reference back.
Public Function CreateOL() As Object
Dim oTmpOL As Object
On Error GoTo ERROR_HANDLER
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'Creating an instance of Outlook is different from Word. '
'There can only be a single instance of Outlook running, '
'so CreateObject will GetObject if it already exists. '
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Set oTmpOL = CreateObject("Outlook.Application")
Set CreateOL = oTmpOL
On Error GoTo 0
Exit Function
ERROR_HANDLER:
Select Case Err.Number
Case Else
MsgBox "Error " & Err.Number & vbCr & _
" (" & Err.Description & ") in procedure CreateOL."
Err.Clear
End Select
End Function
' Purpose : Creates an instance of Word and passes the reference back.
Public Function CreateWD(Optional bVisible As Boolean = True) As Object
Dim oTmpWD As Object
'''''''''''''''''''''''''''''''''''''''''''''''''''''
'Defer error trapping in case Word is not running. '
'''''''''''''''''''''''''''''''''''''''''''''''''''''
On Error Resume Next
Set oTmpWD = GetObject(, "Word.Application")
'''''''''''''''''''''''''''''''''''''''''''''''''''''''
'If an error occurs then create an instance of Word. '
'Reinstate error handling. '
'''''''''''''''''''''''''''''''''''''''''''''''''''''''
If Err.Number <> 0 Then
Err.Clear
On Error GoTo ERROR_HANDLER
Set oTmpWD = CreateObject("Word.Application")
End If
oTmpWD.Visible = bVisible
Set CreateWD = oTmpWD
On Error GoTo 0
Exit Function
ERROR_HANDLER:
Select Case Err.Number
Case Else
MsgBox "Error " & Err.Number & vbCr & _
" (" & Err.Description & ") in procedure CreateWD."
Err.Clear
End Select
End Function

get a handle of the already open instance of Excel and run a macro inside, from Outlook

This is not a duplicate of:
Can I set a Excel Application Object to point to an already open instance of Excel?
The idea is to execute a VBA sub contained in an Excel instance that is already open from Outlook
I am running the VBA sub as part of a rule in Outlook.
This is my code:
On Error Resume Next
Dim tPath As String
tPath = "X:\Lucas\LucasSheet.xlsm"
Dim exApp As New Excel.Application
Dim wb As Excel.Workbook
wb = System.Runtime.InteropServices.Marshal.BindToMoniker(tPath)
Unfortunately at this point, when running in debug mode I can see that wb is equal to Nothing
Set exApp = wb.Parent
usedSub = "PrintSingle"
exApp.Run usedSub
wb.Close False
Is it possible to make this code work in Outlook 2010?
Instead of creating a new Excel Application in the code:
Dim exApp As New Excel.Application
You need to get the running Excel instance :
exApp = System.Runtime.InteropServices.Marshal.GetActiveObject("Excel.Application");
See Access running instances of Excel in VB for more information and sample code in VB.NET.
Use the Run method of the Application class to run the VBA macro programmatically.
First you need a function that will attach to a running instance of Excel and then look for a workbook by name.
'#Description "Return the open Excel workbook"
Public Function GetHandleForExistingWorkbook(ByVal fullPath As String) As Object
On Error GoTo openExcel
Dim excelApp As Object
Set excelApp = GetObject(, "Excel.Application")
Dim wb As Object
For Each wb In excelApp.Workbooks
If wb.FullName Like fullPath & "*" Then
Set GetHandleForExistingWorkbook = wb
Exit For
End If
Next wb
Exit Function
openExcel:
If Err.Number = 429 Then
' Open it if it wasn't already open
Set excelApp = CreateObject("Excel.Application")
GetHandleForExistingWorkbook = excelApp.Workbooks.Open(fullPath)
Else
Debug.Print "Unhandled exception: " & Err.Number & " " & Err.Description
End If
End Function
Once you know you are in the correct place you can run the macro by calling on the Excel Application object:
Public Sub RunMacroInOpenWorkbook(ByVal fullPath As String, ByVal macroName As String, _
Optional ByVal macroParameters As String = "")
Dim theWorkBook As Object
Set theWorkBook = GetHandleForExistingWorkbook(fullPath)
theWorkBook.Application.Run "'" & theWorkBook.Name & "'!" & macroName, macroParameters
theWorkBook.Close False
End Sub
Then your code to use this would look like this:
Dim tPath As String
tPath = "X:\Lucas\LucasSheet.xlsm"
usedSub = "PrintSingle"
RunMacroInOpenWorkbook tPath, usedSub
You need to include the workbook name when using the Application.Run command.
Try using this:
exApp.Run wb.Name & "!" & usedSub ' must include workbook name

Add new sheet to existing Excel workbook with VB code

This code creates an Excel file with one sheet. This sheet contains the code of an item like (ASR/Floor/Dept./Item_Name/Item_details/1) which I created and works fine, but I want to add a sheet into this Excel file to create another item code, and then save this file.
Dim xlApp As Excel.Application
Dim wb As Workbook
Dim ws As Worksheet
Dim var As Variant
Dim code As String
Dim i, nocode As Integer
Dim fname, heading As String
code = "ASR/" & Text1.Text & "/" & Text2.Text & "/" & Text3.Text & "/" & Text4.Text
Set xlApp = New Excel.Application
Set wb = xlApp.Workbooks.Add ' Create a new WorkBook
Set ws = wb.Worksheets("Sheet1") 'Specify your worksheet name
nocode = txtnocode.Text
heading = Text6.Text
For i = 2 To nocode + 1
ws.Cells(i, 1).Value = code & "/" & i - 1 '"ORG"
Next i
fname = "c:\" & Text5.Text & ".xls"
wb.SaveAs (fname)
wb.Close
xlApp.Quit
Set ws = Nothing
Set wb = Nothing
Set xlApp = Nothing
The Worksheets.Add method is what you are looking for:
wb.WorkSheets.Add().Name = "SecondSheet"
See MSDN(scroll down and expand Sheets and Worksheets) for the different parameters you can give to .Add including being able to add the sheet before or after a specific one.
Set ws = wb.Sheets("Sheet1")
Set ws = wb.Sheets.Add
ws.Activate
This is some standard code I use for this type of problem
Note: This code is VBA, to run from within the Excel document itself
Option Explicit
Private m_sNameOfOutPutWorkSheet_1 As String
Sub Delete_Recreate_TheWorkSheet()
On Error GoTo ErrorHandler
'=========================
Dim strInFrontOfSheetName As String
m_sNameOfOutPutWorkSheet_1 = "Dashboard_1"
strInFrontOfSheetName = "CONTROL" 'create the new worksheet in front of this sheet
'1] Clean up old data if it is still there
GetRidOf_WorkSheet_IfItExists (m_sNameOfOutPutWorkSheet_1)
CreateNewOutputWorkSheet m_sNameOfOutPutWorkSheet_1, strInFrontOfSheetName
'Color the tab of the new worksheet
ActiveWorkbook.Sheets(m_sNameOfOutPutWorkSheet_1).Tab.ColorIndex = 5
'Select the worksheet that I started with
Worksheets(strInFrontOfSheetName).Select
'=========================
Exit Sub
ErrorHandler:
Select Case Err.Number
Case Else
MsgBox "One_Main - Error: " & Err.Number & " " & Err.Description
End Select
End Sub
Sub GetRidOf_WorkSheet_IfItExists(sWorkSheetName_ForInitalData As String)
On Error GoTo ErrorHandler
'=========================
If fn_WorkSheetExists(sWorkSheetName_ForInitalData) Then
'Sheet Exists
Application.DisplayAlerts = False
Worksheets(sWorkSheetName_ForInitalData).Delete
Application.DisplayAlerts = True
End If
'=========================
Exit Sub
ErrorHandler:
Select Case Err.Number
Case Else
MsgBox "GetRidOf_WorkSheet_IfItExists - Error: " & Err.Number & " " & Err.Description
End Select
End Sub
Function fn_WorkSheetExists(wsName As String) As Boolean
On Error Resume Next
fn_WorkSheetExists = Worksheets(wsName).Name = wsName
End Function
Sub CreateNewOutputWorkSheet(sWorkSheetName_ForOutputData As String, strInFrontOfSheetName As String)
On Error GoTo ErrorHandler
'=========================
If fn_WorkSheetExists(sWorkSheetName_ForOutputData) Then
'Sheet Exists
Application.DisplayAlerts = False
Worksheets(sWorkSheetName_ForOutputData).Delete
Application.DisplayAlerts = True
End If
Dim wsX As Worksheet
Set wsX = Sheets.Add(Before:=Worksheets(strInFrontOfSheetName))
wsX.Name = sWorkSheetName_ForOutputData
'=========================
Exit Sub
ErrorHandler:
Select Case Err.Number
Case Else
MsgBox "CreateNewOutputWorkSheet - Error: " & Err.Number & " " & Err.Description
End Select
End Sub