We have a standard form in MS Word to manually fill in details and launch a macro. This macro will open a MS Excel register to auto populate it from the fields in the Word document whilst doing other things.
The current solution has a keyboard shortcut for the macro in the Excel spreadsheet and the Word document macro sends the keys strokes to the opened Excel spreadsheet to launch the macro. This only works for a few people in the office.
Can I call my Excel macro directly from the Word macro while my Excel spreadsheet is in focus or is there a Windows setting that is blocking my send keys to launch the macro?
This is just a hunch, but would it be possible to migrate the Excel macro from the Excel spreadsheet to the Word document? Then you could add a bit of code to launch the correct Excel spreadsheet and perform the necessary field manipulations. So, basically, let your Word document do all the work on the Excel spreadsheet.
Alternately, you could follow the procedure discussed here:
http://support.microsoft.com/kb/177760
Here is the relevant code listing:
Sub XLTest()
Dim XL as Object
Set XL = CreateObject("Excel.Application")
XL.Workbooks.Open "C:\My Documents\ExcelFile.xls"
' If there is more than one macro called TestMacro,
' the module name would be required as in
'
' XL.Run "Module1.TestMacro"
'
' to differentiate which routine is being called.
'
XL.Run "TestMacro"
End Sub
Related
I am looking at inserting/pasting a range of text data (40 columns) from Excel into bookmarks in Word. Most of the answers are done using Excel VBA, which is so not practical for my use case as I will have the Word document open, add a button that would run this 'import data' macro. I actually already have a button in the doc that inserts images into bookmarks, so that's one more reason I don't want to do it via Excel VBA.
I know this is not great code, but for the lack of definite leads, I'm throwing it here and hope that this gives you an idea of what I'm trying to achieve:
Sub ImportData()
Workbooks.Open ("\Book2.xlsm")
ActiveWindow.WindowState = xlMinimized
ThisWorkbook.Activate
Windows("Book2.xlsm").Activate
Range("A1:AF1").Select
Selection.Copy
Documents("test.docm").Activate
Selection.GoTo What:=wdGoToBookmark, Name:="Overlay_1"
Selection.Paste
End Sub
PS: It would be great if I could sort of 'transpose' the 40 columns into rows as it is pasted in Word.
Here's an update to my code based off #Variatus 's advice:
Sub ImportData()
Dim wb As Workbooks
Dim ws As Worksheets
Dim objSheet As Object
Dim objWord As Object
Set objWord = CreateObject("Word.Application")
wb.Open ("C:\Users\pc\Documents\Book2.xlsm")
Set objSheet = CreateObject("Excel.Application")
ActiveWindow.WindowState = xlMinimized
Set ws = Workbooks("Book2.xlsm").Sheets("Sheet1")
ws.Range("A1").Value.Copy
With objWord.ActiveDocument
.Bookmarks("Bookmark_1").Range.Text = ws.Range("A1").Value
End With
End Sub
I'm getting this error:
Runtime Error '91':
Object variable or With block variable not set.
Notice how I stuck with a single cell reference for now (A1). I'll just update my code as I learn along the way :)
When you click the button in your Word document you want the following sequence to be initiated.
Create an Excel application object. Make sure that a reference to Excel has been set (VBE > Tools > References) so that Excel's VBA objects are available.
Using the Excel application object, open the workbook. Create an object. Place the object in an invisible window.
Definitely forget about activating or selecting anything in either the workbook or your Word document. The latter is active and remains active from beginning to end. The bookmarks are points in your document you can reference and manipulate by name without selecting them. The Excel workbook is invisible. You can access any part of it using the Range object.
The data you want from your workbook are contained in Worksheets. Be sure to create an object for the worksheet you are about to draw data from.
Excel tables don't translate very well into Word tables. If you do want to go that way I suggest that you use VBA to create the table you want in Excel (transpose the data before you import them into Word). However, you may find it easier to first create the tables you want in Word and then just copy values from your Excel source into the word tables. That would involve taking one cell value at a time and placing it into one Word table cell. Transposing would be done by the algorithm you employ.
Close the workbook. Quit the Excel application. Set the Excel application = Nothing. At the end of your macro everything is as it was before except that your document has data in it which it didn't have before.
Each of the above six points will lead you to at least one question which you can ask here after you have googled the subject and written some code. In fact, I strongly urge you to create one Main procedure (the one which responds to your button click) and let that procedure call various subs which carry out the individual tasks and functions to support the subs. The smaller the parts you create the easier it is to write the code, to find questions to ask and get answers to them. If you plan your project well expect to have about 12 procedures in it by the time you are done. Good luck!
At this moment, I have a document where you enter several output from other programs, this data is converted into some statistics and graphs per person depending on who you select in a combobox (form control). this works fine at this moment.
The excel is connected to a MS Word document which displays the graphs and statistics from the person selected in the excel document.
I would like to have a button that automatically saves the individual document as PDF with a different name.
manual: open both documents
manual: click on the macro
macro: go to first of the combobox list (this can be done by changing output of combobox to 1)
loop
macro: open word and safe as pdf
macro: if number of people that have to be done is same as output combobox, end
macro: go to the next of the list (change output combobox by +1)
end loop
I have tried it for a long time but cannot manage it, I would be very thankful if somebody could help!
I use office 2010
To save a document as PDF you just need to run this line
objWordDocument.SaveAs "C:\TEMP\Doc1.pdf", 17
The complete code to a button sabe a opened Word document is below.
Sub SaveWordAsPDF()
Dim wordObj
Dim objWordDocument As Object
Set wordObj = GetObject(, "Word.Application")
Set objWordDocument = wordObj.Documents(1) '1 is the reference index to the documente, if there are more than 1 opened you need to see wich one is the one you want
objWordDocument.SaveAs "C:\TEMP\Doc1.pdf", 17
End Sub
While generating hundreds of Office Excel spreadsheets with Office Access is certainly possible, it would be great to add macros to the generated workbooks.
I would like to add the functions to the object "ThisWorkbook" in the VBA project for each spreadsheet on generation. How would one go about doing this?
Thank you in advance!
Under the assumption that the macro's in all generated workbooks are the same,
create a template containing all VBA code (and optionally constant text like headers, footers, print range definitions, etc. - i.e. "everything except data")
create any new workbook from the template
insert your data into the WB object
save as macro enabled worksheet (Excel 2007/2010)
close it
example
Sub CreateWB()
Dim WB As Workbook
Set WB = Workbooks.Add("MacroTemp.xltm") ' contains VBA, ActiveX, etc.
WB.Worksheets("Sheet1").[A1] = "co-cooo!" ' adding data
WB.SaveAs "MyGenWB", xlOpenXMLWorkbookMacroEnabled
WB.Close
End Sub
In Excel 2007/2010 do not forget to save the template as macro enabled template (*.xltm").
I have a macro in Outlook, and I have it opening an Excel file that is saved on my desktop. Once the file is open, I would like to run a macro that I have written in excel, but none of my excel macros are available. The macros are available whenever I open excel any other way, and macros are enabled when I open excel through outlook vba. My question is, how do I make these macros available when I open Excel via the Outlook macro? Please reference my code below.
'Pre:None
'Post:Excel will have been opened, and the macro "CreatePowerPoint()"
' will have been run on the excel document
Sub Gimba()
Dim xlApp As Object, xlWkb As Object
'open excel
Set xlApp = CreateObject("Excel.Application")
xlApp.Visible = True ' can be False if you do not wont see reaction,
' byt make sure is not fail
'Do not show any alerts
xlApp.DisplayAlerts = False
'open excel document
Set xlWkb = xlApp.Workbooks.Open(file path goes here)
'call macro on excel document
Call xlApp.Run("CreatePowerPoint")
End Sub
I assume that the macros that you have normally available are stored in your personal.xls workbook? If so, then you just need to load that up before you try and launch your CreatePowerPoint macro.
Try something like (depends on where your personal workbook is stored):
xlApp.Workbooks.Open ("C:\Documents and Settings\YourUserNameHere\Application Data\Microsoft\Excel\XLSTART\personal.xlsb")
As an aside, you might find it easier to write the VBA code if you use early binding. To do this, you need to add a reference to the Excel object model, then instead of using CreateObject, you could use Set xlApp = new Excel.Application. That way you get all the nice Intellitype assistance.
I am new to VB 6.0 ,I am trying to process text and xls files and placing the result in to Excel workbook.
Could you please anyone tell me how to create tabs in Excel sheet and place the content in that.
Regards,
Raju
Here is the code to create a new sheet in VBA:
Dim oSheet As Worksheet Variant
Set oSheet = Worksheets.Add
If you need to manipulate the sheet (rename...), see this link
A quick and easy way would be to open Excel, record a macro and select a sheet and enter some data. The VB 6 code will pretty much be the same code as in the recorded macro once You've set a reference to Excel from VB 6 (or you can use late binding and the createObject function).