Is it possible to call a function from one Module to another?
I have the following code:
Sub MAIN()
Call IDLE
End Sub
MAINis located inModule1IDLEis located inModule2and defined as:Sub IDLE()
Prefix the call with Module2 (ex. Module2.IDLE). I'm assuming since you asked this that you have IDLE defined multiple times in the project, otherwise this shouldn't be necessary.
I agree with the recommendation to use the module name as part of the Call operation. While not strictly necessary if the name happens to be unique today within the project, it probably makes your code more robust against future changes if you are more explicit.
Main in Module1:
Public Sub MAIN()
Call Module2.IDLE
End Sub
The original problem statement did not specify. but did imply both Module1 and Module2 would be in the same project. Assuming these modules are both in the same project it may be a good idea to add "Option Private Module" in Module2. When using VBA in an application like Word this can prevent IDLE from showing up as a user selectable/runnable macro in the UI.
IDLE in Module2:
Option Explicit
Option Private Module
Public Sub IDLE()
' Do something ... being idle requires being proactive.
' Add a delay here
End Sub
Extrapolating a little on why you might want to do that... One Word related use case is to have Module2 in a .dotm file, reference that .dotm file from several other projects (including the one containing Module1), and call code from Module2 from those other files. This facilitates writing/maintaining code in one place and reusing it where needed. If that is your scenario then you would not want to add "Option Private Module" because it would hide Module2 from Module1.
As mentioned in the comments, the use of Public is optional. My example above reflects me favoring being explicit about things like that.
Public ... Subs are Public by default. 3. You can use Option Private Module to hide the Sub from the user interface. 4. You could use a Class Module (instead of a Standard Module) ... but you haven't explained how to then access the Sub in that Class Module.Sub CreateWanderLensSlide()
Dim pptApp As Object
Dim pptPresentation As Object
Dim slide As Object
Dim shape As Object
' Initialize PowerPoint application
Set pptApp = CreateObject("PowerPoint.Application")
pptApp.Visible = True
Set pptPresentation = pptApp.Presentations.Add
' Add a new slide
Set slide = pptPresentation.Slides.Add(1, ppLayoutText)
slide.Design = pptPresentation.Designs(1)
' Set slide title
Set shape = slide.Shapes.AddTextbox(msoTextOrientationHorizontal, 100, 50, 600, 50)
shape.TextFrame.TextRange.Text = "WanderLens: See the World Through a New Lens 🌍✨"
With shape.TextFrame.TextRange
.Font.Name = "Arial Black"
.Font.Size = 36
.ParagraphFormat.Alignment = ppAlignCenter
End With
' Add subheader
Set shape = slide.Shapes.AddTextbox(msoTextOrientationHorizontal, 150, 120, 500, 30)
shape.TextFrame.TextRange.Text = "Homepage Design Concept"
With shape.TextFrame.TextRange
.Font.Name = "Arial"
.Font.Size = 20
.Font.Color = RGB(0, 128, 255) ' Blue color
.ParagraphFormat.Alignment = ppAlignCenter
End With
' Add main content box
Set shape = slide.Shapes.AddTextbox(msoTextOrientationHorizontal, 50, 180, 650, 300)
shape.TextFrame.TextRange.Text = _
"1. Navigation Bar: Home | Destinations | About Us | Contact" & vbCrLf & vbCrLf & _
"2. Hero Section: 'Explore Now' button with immersive VR of a tropical beach." & vbCrLf & vbCrLf & _
"3. Destination Previews: Interactive cards showcasing popular locations like Paris, Tokyo, and Bali." & vbCrLf & vbCrLf & _
"4. Testimonials: Customer feedback carousel, e.g., 'Thanks to WanderLens, I explored Bali before booking!'" & vbCrLf & vbCrLf & _
"5. Partner Offers: Exclusive travel discounts and partner promotions." & vbCrLf & vbCrLf & _
"6. Footer: Privacy Policy, Social Media Links, Contact Info."
With shape.TextFrame.TextRange
.Font.Name = "Calibri"
.Font.Size = 16
.Font.Color = RGB(50, 50, 50) ' Dark gray
.ParagraphFormat.Alignment = ppAlignLeft
End With
' Add image placeholder for homepage screenshot
Set shape = slide.Shapes.AddPicture("C:\Path\To\Your\Image.png", _
msoFalse, msoCTrue, 50, 500, 650, 300)
' Finalize slide layout
With slide.Shapes
' Align and distribute objects as needed
End With
MsgBox "Slide created successfully for WanderLens Homepage Concept!", vbInformation
End Sub