How to solve run time error method failed? - excel

I'm trying to run a piece of code in vba but I'm still getting this error whenever I try to run it.
When I use debug the cursor on top of the 2nd line says "partDocument1 = Nothing"
Here's the code:
Set partDocument1 = documents1.item(Right(activeComponentWorksheet.cells(R, tem).Value, nameLength))
Set part1 = partDocument1.Part
Set body1 = part1.Bodies.item("PartBody")
selection1.Add body1
I'm just trying to create a part from the excel values by using macros. Could anyone explain why would this error happen?

This error message "Run time error method failed" is a general error message that can occur for a variety of reasons in VBA. It typically indicates that an operation that the code is trying to perform has failed, but without more information about the error and the specific line of code that is causing the issue, it is difficult to determine the exact cause of the problem.
One possible cause of this error in the provided code snippet is that the documents1.item(Right(activeComponentWorksheet.cells(R, tem).Value, nameLength)) method call is returning Nothing. This means that the item being passed as the argument to the method is not found in the documents1 collection. This will cause the subsequent Set part1 = partDocument1.Part and Set body1 = part1.Bodies.item("PartBody") line of code to throw the "Run time error method failed" error.
You can try to debug the error by adding some debug information and check if the variable partDocument1 is not equal to nothing before the error is thrown.
If partDocument1 Is Nothing Then
Debug.Print "partDocument1 is not found"
End If
You can also try to check if the value of the Right(activeComponentWorksheet.cells(R, tem).Value, nameLength) is matching with the value that you are expecting.
It's also possible that the problem is related to the activeComponentWorksheet.cells(R, tem).Value cell value or with the nameLength variable, it may be referencing a non-existent cell or it may contain a value that is not expected.
It's also possible that the problem is with the Bodies.item("PartBody") method, it may be that the body is not present or the name passed to the method is not correct.
These are some possible causes of the problem, but without more information about the specific environment and configuration of the code, it is difficult to provide a more specific solution.

Related

why is pycharm giving 'end of statement expected ' error

unexpected error even though there is no syntax error in the statement.
i have tried restarting Pycharm.
i tried coping and pasting the code into new tab where every word is pasted in a new line somehow.
can anyone help me find the issue?
this is the image of the error. https://i.stack.imgur.com/Gx8qZ.png
Check to make sure that the name of your variable doesn't contain any spaces. I recommend using snake or camel casing.
total_number_of_times = 1 #snake
totalNumberOfTimes = 1 #camel

Able to access variable inside one if-statement but not other

Here's a snippet from my code and it's weird why I'm able to access the variable named 'calBegPoint ' inside 3rd if statement but not the second.
if graph != None:
for trace in graph['data']:
if 'LSR(' in trace['name']:
calBegPoint = trace['x'][0] #Statement 1
if '% change' in trace['name']:
print(calBegPoint) #Statement 2
if 'LSR Extended' in trace['name']:
print(calBegPoint) #Statement 3
When I run this code, I get the following error on Statement 2 but, if I comment it, I do not get the error for Statement 3. To solve, this problem, I tried declaring calBegPoint= "Hi" before the entire snippet. With that I did not get the error but, the value printed on Statement 2 was 'Hi' and not the one I calculated.
I've been trying to debug it for hours. Any direction/pointer will be appreciated
UnboundLocalError: local variable 'calBegPoint' referenced before assignment
The error message tries to tell you that you use calBegPoint before defining it.
In your case, the first execution of the second or third if statement lies before the first execution of the first if statement.

Dynamically passing a row number to Session.findbyid method

I am trying to write a script which posts invoices in SAP through Excel (fairly new to this), and am running into the following error:
"The control could not be found by id".
The error is coming up at the below line:
session.FindById("wnd[0]/usr/subITEMS:SAPLFSKB:0100/tblSAPLFSKBTABLE/ctxtACGL_ITEM-HKONT[1,w_counter]").Text = w_glacc
Here, I am trying to pass the GL account number in the first row. There can be multiple rows, so I was hoping that instead of passing ctxtACGL_ITEM-HKONT[1,0]").Text, ctxtACGL_ITEM-HKONT[1,1]").Text etc. I wanted to initialize a counter and pass that value into this method.
Is there any way this can be achieved?
Found a solution:
Instead of passing just w_counter I had to pass "&w_counter&".
Previously, code was:
session.FindById("wnd[0]/usr/subITEMS:SAPLFSKB:0100/tblSAPLFSKBTABLE/ctxtACGL_ITEM-HKONT[1,w_counter]").Text = w_glacc
Now, it is:
session.FindById("wnd[0]/usr/subITEMS:SAPLFSKB:0100/tblSAPLFSKBTABLE/ctxtACGL_ITEM-HKONT[1,"& w_counter &"]").Text = w_glacc

Fortran error check on formatted read

In my code I am attempting to read in output files that may or may not have a formatted integer in the first line of the file. To aid backwards compatibility I am attempting to be able to read in both examples as shown below.
head -n 3 infile_new
22
8
98677.966601475651 -35846.869655806520 3523978.2959464169
or
head -n 3 infile_old
8
98677.966601475651 -35846.869655806520 3523978.2959464169
101205.49395364164 -36765.047712555031 3614241.1159234559
The format of the top line of infile_new is '(i5)' and so I can accommodate this in my code with a standard read statement of
read(iunit, '(I5)' ) n
This works fine, but if I attempt to read in infile_old using this, I as expected get an error. I have attempted to get around this by using the following
read(iunit, '(I5)' , iostat=ios, err=110) n
110 if(ios == 0) then
print*, 'error in file, setting n'
naBuffer = na
!rewind(iunit) #not sure whether to rewind or close/open to reset file position
close(iunit)
open (iunit, file=fname, status='unknown')
else
print*, "Something very wrong in particle_inout"
end if
The problem here is that when reading in either the old or new file the code ends up in the error loop. I've not been able to find much documentation on using the read statement in this way, but cannot determine what is going wrong.
My one theory was my use of ios==0 in the if statement, but figured since I shouldn't have an error when reading the new file it shouldn't matter. It would be great to know if anyone knows a way to catch such errors.
From what you've shown us, after the code executes the read statement it executes the statement labelled 110. Then, if there wasn't an error and iostat==0 the true branch of the if construct is executed.
So, if there is an error in the read the code jumps to that statement, if there isn't it walks to the same statement. The code doesn't magically know to not execute the code starting at label 110 if there isn't an error in the read statement. Personally I've never used both iostat and err in the same read statement and here I think it's tripping you up.
Try changing the read statement to
read(iunit, '(I5)' , iostat=ios) n
You'd then need to re-work your if construct a bit, since iostat==0 is not an error condition.
Incidentally, to read a line which is known to contain only one integer I wouldn't use an explicit format, I'd just use
read(iunit, * , iostat=ios) n
and let the run-time worry about how big the integer is and where to find it.

IMAP fetch() returns command error: BAD [b' Command Argument Error. 12']

I'm having trouble finding examples/troubleshooting tips online, and am not quite sure that I'm interpreting the documentation correctly. Any assistance would be greatly appreciated.
I'm connecting to an e-mail server, and want to read the e-mail subjects, and bodies. I first make my connection like so:
import imaplib
c = imaplib.IMAP4_SSL(hostname, port)
c.login(username, password)
foldername = 'INBOX/SSR'
c.select(str.encode(foldername), readonly = True)
today = datetime.date.today().strftime('%d-%b-%Y')
searchcriteria = '(SENTON '{}')'.format(today)
typ, msg_ids = c.search(None, searchcriteria)
msg_ids = [s.decode('ascii') for s in msg_ids]
for idnumber in msg_ids:
print(c.fetch(idnumber, "(BODY.PEEK[HEADER])"))
The code and works and output looks as expected, up until the last line, at which point, I get
imaplib.error: FETCH command error: BAD [b' Command Argument Error. 12']
My line of thought, and subsequent testing examined the following possible issues:
bytes vs. string. I converted input back to bytes, but the error remained constant
improper syntax: I tried other commands, such as BODY, SUBJECT, and ENVELOPE but still got the same message.
I'm not sure how to interpret the error, and don't really know where to start. Referencing https://www.rfc-editor.org/rfc/rfc3501.html from pp. 102+, I noticed that the values are labeled differently, but don't understand what the issue is with my implementation. How should I interpret the error? What is wrong with my syntax?
P.S. Correct me if I'm wrong, but the c.search shouldn't change my directory, yes? As in, by selecting foldername, I "navigate" to the selected folder, but just searching only returns values and shouldn't change my location?
I encountered the same problem while I tried to list or select a new mailbox - BAD [b' Command Argument Error. 12'], in my case, it didn't work with “Sent Box”, but it worked well with “Outbox”, so the space symbol is the point.
So it worked with c.select('"{}"'.format("Sent Box")...
Hope this information could help you.
Your last line is not correct msg_ids = [s.decode('ascii') for s in msg_ids]
msg_ids is a list with bytes string, not with elements of a list - example: [b'123 124 125']
Change the last line into msg_ids = msg_ids[0].split(b' ') and it will work as expected.

Resources